Skip to content

Commit

Permalink
fix(backend): Handle "Cannot convert argument to a ByteString" errors (
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis authored Sep 30, 2024
1 parent 7f87391 commit b579c36
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-lobsters-smile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@clerk/backend": patch
---

Handle "Cannot convert argument to a ByteString" errors
10 changes: 10 additions & 0 deletions packages/backend/src/tokens/__tests__/authStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export default (QUnit: QUnit) => {
assert.strictEqual(authObject.headers.get('x-clerk-auth-reason'), 'auth-reason');
assert.strictEqual(authObject.headers.get('x-clerk-auth-message'), 'auth-message');
});

test('handles debug headers containing invalid unicode characters without throwing', assert => {
const headers = new Headers({ 'custom-header': 'value' });
const authObject = signedOut({} as any, 'auth-reason+RR�56', 'auth-message+RR�56', headers);

assert.strictEqual(authObject.headers.get('custom-header'), 'value');
assert.strictEqual(authObject.headers.get('x-clerk-auth-status'), 'signed-out');
assert.strictEqual(authObject.headers.get('x-clerk-auth-reason'), null);
assert.strictEqual(authObject.headers.get('x-clerk-auth-message'), null);
});
});

module('handshake', () => {
Expand Down
20 changes: 17 additions & 3 deletions packages/backend/src/tokens/authStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,27 @@ const withDebugHeaders = <T extends RequestState>(requestState: T): T => {
const headers = new Headers(requestState.headers || {});

if (requestState.message) {
headers.set(constants.Headers.AuthMessage, requestState.message);
try {
headers.set(constants.Headers.AuthMessage, requestState.message);
} catch (e) {
// headers.set can throw if unicode strings are passed to it. In this case, simply do nothing
}
}

if (requestState.reason) {
headers.set(constants.Headers.AuthReason, requestState.reason);
try {
headers.set(constants.Headers.AuthReason, requestState.reason);
} catch (e) {
/* empty */
}
}

if (requestState.status) {
headers.set(constants.Headers.AuthStatus, requestState.status);
try {
headers.set(constants.Headers.AuthStatus, requestState.status);
} catch (e) {
/* empty */
}
}

requestState.headers = headers;
Expand Down

0 comments on commit b579c36

Please sign in to comment.