Reject javascript: and vbscript: URI schemes in href#4113
Merged
Conversation
🦋 Changeset detectedLatest commit: 534698a The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
joshblack
approved these changes
Jun 30, 2026
Member
|
Just wanted to add @jonrohan on here too for this kind of change 👀 |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds a defense-in-depth guardrail to prevent XSS via href values by centrally rejecting javascript: and vbscript: URI schemes when Primer components render as anchors.
Changes:
- Introduces
Primer::SafeHrefHelperto detect disallowedhrefschemes, including common whitespace/control-character bypasses. - Hooks
sanitize_href!intoPrimer::BaseComponentinitialization to raise in non-production when invalid options are enforced and otherwise neutralize by dropping thehref. - Adds unit + component regression tests for both raising and neutralization paths, plus a changeset entry.
Show a summary per file
| File | Description |
|---|---|
app/lib/primer/safe_href_helper.rb |
Adds scheme detection with normalization to catch control/whitespace bypasses. |
app/components/primer/component.rb |
Adds sanitize_href! and wires it to the existing should_raise_error? behavior. |
app/components/primer/base_component.rb |
Applies sanitize_href! centrally to validated system arguments. |
test/lib/safe_href_helper_test.rb |
Unit tests for detection behavior and bypass cases. |
test/components/base_component_test.rb |
Regression tests for raising vs neutralization and safe href allowance. |
test/components/beta/label_test.rb |
Ensures label-as-anchor rejects/neutralizes unsafe href. |
test/components/primer/beta/button_test.rb |
Ensures button-as-anchor rejects/neutralizes unsafe href. |
.changeset/sanitize-href-uri-scheme.md |
Documents the patch-level behavior change for consumers. |
Review details
- Files reviewed: 8/8 changed files
- Comments generated: 0
- Review effort level: Low
jonrohan
approved these changes
Jun 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes https://github.com/github/primer/issues/6760
Summary
Defense in depth: rejects
javascript:andvbscript:URI schemes in a component'shrefargument, applied centrally inPrimer::BaseComponent. Covers every component that routes throughBaseComponent(Label,Button,Link, action list items, action menus, etc.) when rendered as an anchor.Scheme detection strips ASCII whitespace and control characters (
\u0000–\u0020) before matching, per the WHATWG URL spec, so bypasses like\tJaVaScRiPt:orj\u0000avascript:are caught too.Behavior
raise_on_invalid_options = true): raisesArgumentErrorso misuse is caught early.hreftonilso the anchor renders without anhrefattribute rather than crashing the request.data:is intentionally not blocked.Changes
Testing
bundle exec rake test— green across all affected files.hrefrenders without anhrefattribute and is inert on click; safe values are preserved.