Skip to content

[patch] Reject whitespace in [IsBase64] regardless of total length - #400

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/316-base64-rejects-whitespace
Oct 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/316-base64-rejects-whitespace

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #316

What was wrong

[IsBase64] checked two things: that value.Length % 4 == 0, and that Convert.FromBase64String(value) didn't throw. FromBase64String silently skips spaces, tabs, CR and LF, but the length check counts them. So a value containing whitespace passed whenever the whitespace made the total length a multiple of 4, and it was stored verbatim. The existing IsBase64Attribute_Base64WithSpaces_ShouldThrow only passed because its sample happened to be 19 characters long.

Change (Semantics.Strings/Validation/Attributes/Text/IsBase64Attribute.cs)

  • After the length check, the value must use only the Base64 alphabet (A-Z, a-z, 0-9, +, /), followed by at most two = at the end. Anything else fails with the same "must be a valid Base64 string" message.
  • Convert.FromBase64String still runs after that to confirm the padding.

Tests (TextValidationAttributesTests)

  • The whitespace test is now data-driven, and its samples no longer depend on length parity. The original 19-character sample is kept, plus the three 20-character values from the issue that used to be accepted (trailing spaces, embedded spaces plus a trailing space, and CRLF), and trailing tabs.
  • IsBase64Attribute_MisplacedPadding_ShouldThrow: = mid-value, a non-= after =, and three =.
  • IsBase64Attribute_PlusAndSlash_ShouldPass: confirms + and / are still accepted.
  • The existing valid vectors (including the empty string) still pass, unchanged.

With the fix reverted, 4 of the whitespace cases fail. With the fix, the full Semantics.Test suite passes: 1489 passed, 0 failed, 8 skipped. Semantics.Strings builds for every target framework.

🤖 Generated with Claude Code

https://claude.ai/code/session_019RYN9jT6szUiySkywFKCA1


Generated by Claude Code

@sonarqubecloud

sonarqubecloud Bot commented Oct 8, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit df4ca90 into main Oct 10, 2026
14 of 15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/316-base64-rejects-whitespace branch October 10, 2026 12:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[IsBase64] accepts values containing whitespace whenever the total length is a multiple of 4

2 participants