Skip to content

Commit fd349a9

Browse files
Fix flaky tampered-signature test: flip first sig char instead of last to avoid Base64URL padding-bit ambiguity
1 parent 73b661d commit fd349a9

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

tests/Jwt.Tests.ps1

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,16 @@ Describe 'Test-Jwt validation' {
201201
It 'returns $false for a tampered signature' {
202202
$jwt = New-Jwt -Payload $script:basePayload -Key $script:hmacSecret -Algorithm HS256
203203
$compact = $jwt.ToString()
204-
$lastChar = $compact[$compact.Length - 1]
205-
$replacement = if ($lastChar -eq 'A') { 'B' } else { 'A' }
206-
$tampered = $compact.Substring(0, $compact.Length - 1) + $replacement
204+
# Tamper with the first character of the signature segment.
205+
# Changing the last character can fall on Base64URL padding bits
206+
# (e.g., for 32-byte HMAC-SHA256 the last char carries only 4 data
207+
# bits), producing identical decoded bytes and a false pass.
208+
$parts = $compact.Split('.')
209+
$sig = $parts[2]
210+
$firstChar = $sig[0]
211+
$replacement = if ($firstChar -eq 'A') { 'B' } else { 'A' }
212+
$parts[2] = $replacement + $sig.Substring(1)
213+
$tampered = $parts -join '.'
207214
Test-Jwt -Token $tampered -Key $script:hmacSecret -Audience 'api://test' | Should -BeFalse
208215
}
209216

0 commit comments

Comments
 (0)