Skip to content

fix: accept valid rgba/hsla alpha values and reject malformed ones - #1654

Open
breken-ai wants to merge 1 commit into
go-playground:masterfrom
breken-ai:fix/rgba-hsla-alpha-values
Open

breken-ai wants to merge 1 commit into
go-playground:masterfrom
breken-ai:fix/rgba-hsla-alpha-values

Conversation

@breken-ai

Copy link
Copy Markdown

Fixes Or Enhances

The alpha channel in the rgba and hsla regexes is (?:0.[1-9]*)|[01]. The . is not escaped, so it matches any character, and [1-9]* does not allow a zero anywhere in the fraction. As a result, valid CSS colours fail and malformed ones pass:

value tag before after
rgba(0,0,0,0.05) rgba rejected valid
rgba(0,0,0,0.50) rgba rejected valid
rgba(0,0,0,0.0) rgba rejected valid
rgba(0,0,0,1.0) rgba rejected valid
hsla(0,0%,0%,0.05) hsla rejected valid
rgba(0,0,0,0a) rgba accepted invalid
hsla(0,0%,0%,0x) hsla accepted invalid

A semi-transparent shadow such as rgba(0,0,0,0.05) is a very common value, and it currently fails rgba and iscolor validation.

The fix changes the alpha part in both regexes to 0(?:\.\d+)?|1(?:\.0+)?. That accepts 0, 1, 0.<digits> and 1.<zeros>, and still rejects values above 1. Nothing else in the regexes changes.

TestRgbaHslaAlphaValue covers valid and invalid alpha values for rgba (numeric and percent forms) and hsla. On the current master it fails with rgba: "rgba(0,31,255,0.05)" should be valid. With this change it passes, and so does go test ./....

This PR was AI-assisted: the bug was found, and the fix and test were written, with Claude Opus 5.5. I checked the failing and passing test runs above.

Make sure that you've checked the boxes below before you submit PR:

  • Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

The alpha part of the rgba and hsla regexes was `(?:0.[1-9]*)|[01]`.
The unescaped `.` matches any character and `[1-9]*` forbids zeros in
the fraction, so common CSS values such as `rgba(0,0,0,0.05)`,
`0.50`, `0.0` and `1.0` were rejected while junk like `0a` or `0x`
was accepted.

Match the alpha as `0`, `0.<digits>`, `1` or `1.<zeros>` instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@breken-ai
breken-ai requested a review from a team as a code owner September 30, 2026 07:22
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.

1 participant