-
Notifications
You must be signed in to change notification settings - Fork 2
feat(badges): accept URL-safe base64 (base64url) badge artifacts #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smarcet
wants to merge
4
commits into
main
Choose a base branch
from
feat/base64url-badge-artifact
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e07d794
feat(badges): accept URL-safe base64 (base64url) badge artifacts
smarcet d8b24db
fix(badges): reject malformed base64 padding in looksLikeBase64
smarcet 105fab9
fix(badges): make tryBase64Decode agree with looksLikeBase64
smarcet 339a6de
docs(badges): note URL-safe base64 accepted on the validateBadge badg…
smarcet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php namespace Tests; | ||
|
|
||
| /** | ||
| * Copyright 2026 OpenStack Foundation | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| **/ | ||
|
|
||
| use App\Utils\Base64; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Class Base64Test | ||
| * Badge/ticket QR artifacts travel base64-encoded as a URL path segment; the standard | ||
| * alphabet's "/" can never survive path routing (Laravel rawurldecodes the path before | ||
| * matching), so the helper must also accept the URL-safe alphabet (base64url, RFC 4648 §5: | ||
| * "-" for "+", "_" for "/"). Standard base64 must keep decoding byte-identical. | ||
| */ | ||
| final class Base64Test extends TestCase | ||
| { | ||
| public function testStandardAlphabetStillDecodes() | ||
| { | ||
| // "+/+/" = indices 62,63,62,63 = bytes FB FF BF (hand-derived from the RFC 4648 table) | ||
| $this->assertTrue(Base64::looksLikeBase64("+/+/")); | ||
| $this->assertSame("\xfb\xff\xbf", Base64::tryBase64Decode("+/+/")); | ||
| } | ||
|
|
||
| public function testUrlSafeAlphabetIsAccepted() | ||
| { | ||
| // same payload as "+/+/", url-safe spelling | ||
| $this->assertTrue(Base64::looksLikeBase64("-_-_")); | ||
| $this->assertSame("\xfb\xff\xbf", Base64::tryBase64Decode("-_-_")); | ||
| } | ||
|
|
||
| public function testUrlSafeAndStandardSpellingsDecodeToTheSameBytes() | ||
| { | ||
| $standard = Base64::tryBase64Decode("QUFB/QkJC+Q0PT0="); | ||
| $urlSafe = Base64::tryBase64Decode("QUFB_QkJC-Q0PT0="); | ||
| $this->assertNotNull($standard); | ||
| $this->assertSame($standard, $urlSafe); | ||
| } | ||
|
|
||
| public function testUrlSafeWithoutPaddingIsPadded() | ||
| { | ||
| // "-_" = indices 62,63 = 11111011 = byte FB after padding to "-_==" | ||
| $this->assertTrue(Base64::looksLikeBase64("-_")); | ||
| $this->assertSame("\xfb", Base64::tryBase64Decode("-_")); | ||
| } | ||
|
|
||
| public function testNonBase64InputIsRejected() | ||
| { | ||
| $this->assertFalse(Base64::looksLikeBase64("BADGE_X|123|a@b.com|Ada Lovelace")); | ||
| $this->assertFalse(Base64::looksLikeBase64("")); | ||
| $this->assertNull(Base64::tryBase64Decode("!!!")); | ||
| // tryBase64Decode must agree with looksLikeBase64: base64_decode('', true) "succeeds" | ||
| // with '' but an empty artifact is not a decodable payload | ||
| $this->assertNull(Base64::tryBase64Decode("")); | ||
| } | ||
|
smarcet marked this conversation as resolved.
|
||
|
|
||
| public function testMalformedPaddingIsRejected() | ||
| { | ||
| // padding-only, under-padded and over-padded inputs are not RFC 4648 base64: the data | ||
| // length (minus padding) decides how much padding is allowed - none, or exactly enough | ||
| // to reach a multiple of 4 | ||
| $this->assertFalse(Base64::looksLikeBase64("==")); | ||
| $this->assertFalse(Base64::looksLikeBase64("A=")); | ||
| $this->assertFalse(Base64::looksLikeBase64("QQ=")); | ||
| $this->assertFalse(Base64::looksLikeBase64("QUFB==")); | ||
| // the decode agrees with the sniff: what looksLikeBase64 rejects, tryBase64Decode | ||
| // rejects too (no silent repair of under-padded input) | ||
| $this->assertNull(Base64::tryBase64Decode("QQ=")); | ||
| // exact RFC padding stays accepted | ||
| $this->assertTrue(Base64::looksLikeBase64("QQQ=")); | ||
| $this->assertSame("A\x04", Base64::tryBase64Decode("QQQ=")); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.