File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments