Skip to content

fix(tus): accept base64-encoded checksums in Upload-Checksum header - #883

Open
mmattel wants to merge 2 commits into
mainfrom
fix/tus-checksum-base64
Open

mmattel wants to merge 2 commits into
mainfrom
fix/tus-checksum-base64

Conversation

@mmattel

@mmattel mmattel commented Dec 3, 2026

Copy link
Copy Markdown

Why

Follows up on #879, which re-enabled Upload-Checksum validation for TUS uploads
but introduced a silent regression: the TUS spec requires the checksum value to be
base64-encoded, while checkHash() compared it against hex.EncodeToString().
Since base64 ≠ hex the comparison always failed, returning HTTP 460 on every upload
that sent a checksum.

Root cause

TUSContext.php (the PHP acceptance-test client) calls setChecksumAlgorithm('sha1'),
so every TUS upload sends Upload-Checksum: sha1 <base64-sha1>. checkHash() calls
hex.EncodeToString(h.Sum(nil)) and compares directly — base64 never equals hex, so
every upload failed with 460 Checksum Mismatch.

Failing test suites (detected via owncloud/ocis PR #13666):

  • apiSpaces/tusUpload.feature, apiSpaces/filePreviews.feature
  • coreApiWebdavUploadTUS/uploadToShare.feature, uploadFileMtimeShares.feature
  • Cascade: apiSearch1, natsJwt-events-stores-file, cliCommands/uploadSessions.feature

Fix

In checkHash, try base64.StdEncoding.DecodeString(expected) first. If it
succeeds and the decoded length matches h.Size(), convert to hex before
comparing. Plain hex values (legacy / direct API callers) fall through unchanged,
so backward compatibility is preserved.

References

🤖 Generated with Claude Code

The TUS protocol spec requires the Upload-Checksum header value to be
base64-encoded.  The TusPhp client (used in acceptance tests) always
sends base64.  checkHash() was comparing the raw base64 string against
hex.EncodeToString(), which never matches, so every upload that sent a
checksum received HTTP 460 Checksum Mismatch.

Decode from base64 first (using h.Size() to verify the decoded length
matches the expected hash); if decoding succeeds, convert to hex before
comparing.  Plain hex values (from direct API callers) fall through
unchanged, so backward compatibility is preserved.

Relates to #879 (which re-enabled checksum validation but missed the
encoding difference between TUS clients and the internal hex format).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Signed-off-by: Martin Mattel <github@diemattels.at>
@mmattel
mmattel requested a review from a team as a code owner December 3, 2026 17:21
@kw-security

kw-security commented Dec 3, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

mmattel added a commit to owncloud/ocis that referenced this pull request Dec 3, 2026
The TUS protocol spec requires the Upload-Checksum header value to be
base64-encoded.  The TusPhp client (used in acceptance tests) always
sends base64.  checkHash() was comparing the raw base64 string against
hex.EncodeToString(), which never matched, so every upload that sent a
checksum received HTTP 460 Checksum Mismatch.

Decode from base64 first (using h.Size() to verify the decoded length
matches the expected hash size); if decoding succeeds, convert to hex
before comparing.  Plain hex values fall through unchanged.

Upstream fix: owncloud/reva#883

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Signed-off-by: Martin Mattel <github@diemattels.at>
For multi-chunk TUS uploads each PATCH carries the checksum of that
chunk only, not the whole-file checksum. Storing a per-chunk checksum
for FinishUpload whole-file validation caused every chunked upload with
per-chunk checksums to fail with 460 Checksum Mismatch.

Restrict SetChecksum() to single-chunk uploads: offset == 0 and
Content-Length equals the declared upload size. For multi-chunk uploads
the Upload-Checksum header is silently ignored, preserving the existing
behaviour that tusd v2 established when it dropped per-chunk validation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Signed-off-by: Martin Mattel <github@diemattels.at>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants