Skip to content

Harden existing Jwt parsing and crypto edge cases found during PR review #19

Description

During review of PR #18, Copilot review comments identified concrete bugs and security hardening gaps that go beyond the original publication-continuity scope of issue #17. The pull request now fixes malformed JWT and Base64URL handling, sensitive verbose logging, HS256 signature comparison, and modern RSA certificate-key access while preserving the existing command surface.

Request

What happens

Malformed token and Base64URL inputs can currently produce confusing downstream failures or data-altering behavior instead of targeted invalid-input errors. Verbose output can expose sensitive token material. HS256 verification relies on comparison behavior that should be hardened for signature checks. RSA certificate-key access uses obsolete certificate key accessors that should be replaced with modern APIs.

What is expected

JWT parsing and cryptographic verification should fail closed with clear errors for malformed inputs, avoid exposing sensitive token material in verbose output, compare HS256 signatures without plain string equality, and use modern RSA certificate extension APIs without changing public commands or aliases.

Environment

  • Repository context: PSModule/Jwt
  • Regression: Unknown; identified during review before the review-driven hardening work was split from issue #17
  • Related pull request: PR #18

Known workarounds

Callers can pre-validate token shape before invoking module commands and avoid verbose output when handling sensitive tokens, but these mitigations are incomplete because the module should enforce safe behavior consistently.

Acceptance criteria

  • Existing public commands and aliases are preserved
  • Malformed JWT shapes are treated as invalid input with targeted errors
  • Invalid Base64URL length is treated as malformed input without trimming or silently changing data
  • Verbose output does not include complete JWT strings or payload JSON
  • HS256 signature verification compares signature bytes without plain string equality
  • RS256 signing and verification use modern RSA certificate extension APIs
  • Pester coverage exercises malformed JWT shapes, invalid Base64URL length, invalid HS256 signature segments, and verbose redaction

Review references


Technical decisions

Scope relationship: This issue tracks review-driven hardening already addressed in PR #18. It is separate from issue #17, which covers publication continuity rather than the parsing, logging, and cryptographic edge cases identified during review.

Command compatibility: Keep the existing public commands and aliases. The work changes validation and implementation behavior only, so callers do not need to adopt new command names or parameters.

Malformed JWT handling: Treat malformed compact JWT shapes as invalid input with targeted errors before indexing token segments or attempting downstream decoding. This keeps failures understandable and avoids accidental reliance on PowerShell indexing behavior.

Base64URL handling: Treat Base64URL input whose length cannot be padded correctly as malformed input rather than trimming data. Base64URL follows RFC 4648 section 5, and invalid length should not be repaired by dropping user-provided characters.

Verbose output: Avoid logging complete JWT strings or payload JSON in verbose output. Verbose messages should report non-sensitive metadata such as algorithm, claim count, segment count, or operation phase.

HS256 comparison: Compare decoded HS256 signature bytes with a PowerShell-compatible fixed-work byte comparison and avoid plain string equality for signatures. Length mismatches should fail without disclosing token material.

RSA certificate access: Use RSACertificateExtensions.GetRSAPrivateKey and RSACertificateExtensions.GetRSAPublicKey instead of obsolete certificate key accessors.

Test approach: Add focused Pester coverage for the review findings rather than broadening the public API surface. Tests should prove safe failure modes and redaction behavior around the existing commands.


Implementation plan

Core changes

  • Add invalid Base64URL length validation to ConvertFrom-Base64UrlString
  • Add compact JWT segment-count validation to Get-JwtHeader, Get-JwtPayload, and Test-Jwt
  • Ensure malformed JWT handling returns targeted errors instead of confusing indexing or decoding failures
  • Update New-Jwt verbose messages to report only non-sensitive metadata
  • Update Test-Jwt verbose messages to report only non-sensitive metadata
  • Update HS256 verification to compare decoded signature bytes without plain string equality
  • Update RS256 signing to use modern RSA certificate extension APIs
  • Update RS256 verification to use modern RSA certificate extension APIs

Tests

  • Add Pester coverage for malformed JWT shapes
  • Add Pester coverage for invalid Base64URL length
  • Add Pester coverage for invalid HS256 signature segments
  • Add Pester coverage proving verbose output redacts complete JWT strings
  • Add Pester coverage proving verbose output redacts payload JSON

Review closure

  • Confirm all referenced PR #18 review threads are addressed by the implementation
  • Confirm the hardening scope remains compatible with the existing public command surface

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions