Fix: cloudflared access token invalidates JWT signature by re-serializing the header#1630
Open
AdrianBannister wants to merge 1 commit intocloudflare:masterfrom
Open
Conversation
getTokenIfExists now returns the original raw token string from disk alongside the parsed JWS object. Callers use the raw string instead of CompactSerialize(), which re-marshals the header with Go's alphabetical key ordering and changes the base64url bytes the signature covers. Co-authored-by: Forge <forge-canva@users.noreply.github.com>
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
getTokenIfExistsreads the cached JWT from disk, parses it withjose.ParseSigned(), and callers (GetAppTokenIfExists/GetOrgTokenIfExists) returntoken.CompactSerialize(). This re-serializes the JWT header with Go's default alphabetical key ordering (documented behavior ofjson.Marshalon maps), which differs from the original key order. Since the JWT signature covers the exact base64url-encoded header bytes, the re-ordered header invalidates the signature.Root cause in go-jose
CompactSerializedelegates to compact serialization, andcompactSerializecallsmustSerializeJSON(obj.Signatures[0].protected).mustSerializeJSONin turn callsjson.Marshal.The protected header is stored as
rawHeader, which is amap[HeaderKey]*json.RawMessage. Go'sjson.Marshalsorts map keys alphabetically, so{"typ":"JWT","alg":"RS256"}becomes{"alg":"RS256","typ":"JWT"}— different base64url bytes, broken signature.Notably, go-jose does preserve the original protected-header bytes when parsing:
signature.originalis assigned fromparsed.Protected. And during signature verification,computeAuthDatacorrectly checkssignature.original.Protectedand uses the original bytes via.base64(). ButcompactSerializeignoressignature.originalentirely and re-marshals from the map.Changes
getTokenIfExists: Returns(string, *jose.JSONWebSignature, error)instead of(*jose.JSONWebSignature, error). The raw token string (trimmed) is read from disk and returned alongside the parsed JWS (still needed for expiry checking).GetAppTokenIfExists/GetOrgTokenIfExists: Return the raw string instead of callingtoken.CompactSerialize().Tests
Four new tests in
token/token_test.go:TestGetTokenIfExists_PreservesOriginalTokenStringCompactSerialize()produces a different (broken) stringTestGetTokenIfExists_TrimsWhitespaceTestGetTokenIfExists_FileNotFoundTestGetTokenIfExists_InvalidTokenThe core regression test (
PreservesOriginalTokenString) crafts a JWS with non-alphabetical header key order and asserts thatCompactSerialize()would produce a different string — directly demonstrating the bug.