fix: accept base64url in auth header validation (browser uploads return 400)#10
Merged
Conversation
The browser SPA emits the auth key with base64url alphabet ('-', '_',
no padding) via app/utils.js arrayToB64. The strict format check
landed in #1 used a standard-base64 regex (+ and / only), so every
real browser upload was rejected with {"error": 400} from
server/routes/ws.js, and every fetch-based upload via the upload.js
HTTP route hit the same rejection.
Both regexes now accept the base64url alphabet too:
- '+/' and '-_' character classes
- 0..2 trailing '=' optional
Node's Buffer.from(value, 'base64') accepts both alphabets, so the
downstream HMAC verifier in server/middleware/auth.js is unaffected
and meta.auth storage stays in whatever alphabet the client sent.
Also adds log.error() on the 400 rejection branches in upload.js and
ws.js. Previously the rejection paths were silent and operators had
no signal that a client-side validation mismatch was happening.
Repro: python urlsafe_b64encode(...).rstrip("=") via the WS sends
{"error": 400} on master before this fix and {"url": ...} after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every browser-initiated upload against `ghcr.io/tarnover/send:latest` fails with `{"error": 400}` and the SPA navigates to `/error`. Root cause: the strict-format check I added in #1 used a standard-base64 regex (`+/`), but the SPA encodes the per-share auth key with base64url (`-_`, no padding) via `app/utils.js::arrayToB64`. The two have always disagreed in this fork — no browser upload could ever have succeeded.
Root cause
`app/utils.js`:
```js
function arrayToB64(array) {
return b64.fromByteArray(array)
.replace(/+/g, '-') // base64url
.replace(///g, '_')
.replace(/=/g, ''); // no padding
}
```
`server/routes/ws.js` and `server/routes/upload.js` (before this PR):
```js
!/^[A-Za-z0-9+/]+={0,2}$/.test(authParts[1]) // <-- rejects '-', '_', and missing padding
```
Fix
Two character-class edits, no behavior change for valid keys:
```diff
```
Applied identically in both `server/routes/ws.js` and `server/routes/upload.js`. Node's `Buffer.from(value, 'base64')` decodes both alphabets, so the downstream HMAC verifier in `server/middleware/auth.js` is unaffected and `meta.auth` storage stays in whatever alphabet the client sent.
Observability bonus
Previously the 400 rejection branches in both routes were silent — no `log.error()` — and `docker logs send` showed nothing useful, which is what made this take so long to track down. This PR adds:
So next time the WS / HTTP handler rejects a client at the 400 gate, an operator sees it.
Repro (Python, no browser)
```python
import json, base64, ssl
from websocket import create_connection
key = b"\xff" * 32
ws = create_connection("wss://snd.dx.pe/api/ws",
sslopt={"cert_reqs": ssl.CERT_NONE})
ws.send(json.dumps({
"fileMetadata": base64.b64encode(b"meta").decode(),
"authorization": "send-v1 " + base64.urlsafe_b64encode(key).decode().rstrip("="),
"timeLimit": 3600,
"dlimit": 1,
}))
print(ws.recv())
```
Same script with standard `base64.b64encode` already worked before this PR — that's how the reporter narrowed it down.
Reported by
Bug report from an operator running `ghcr.io/tarnover/send:latest` against `/version` reporting `v3.4.27` commit `f4e63ed` (current master tip).
Test plan
Follow-up (not in this PR)
We should add a backend regression test for the auth format gate. The current test suite (`test/backend/`) doesn't exercise either route handler's argument validation, which is how this regression made it to a release. Tracked separately.