Skip to content

Security: Validate accent_color to prevent CSS injection#55

Open
bellisabell wants to merge 1 commit intomainfrom
bell/fix-css-injection-accent-color
Open

Security: Validate accent_color to prevent CSS injection#55
bellisabell wants to merge 1 commit intomainfrom
bell/fix-css-injection-accent-color

Conversation

@bellisabell
Copy link
Member

Summary

The CSS injection vulnerability in _account_accent_color_override.html.erb is already mitigated by existing validation in the Account model:

validates :accent_color, format: { with: /\A#[0-9a-fA-F]{6}\z/ }, allow_nil: true

This PR adds defensive improvements and a comprehensive test suite to ensure the fix is documented and protected against regression.

Changes

  1. Defensive nil guard in accent_color_rgb method to prevent crashes
  2. Comprehensive test suite verifying:
    • Valid hex colors accepted (upper/lowercase)
    • CSS injection attempts rejected (red; } body { display:none } .x {)
    • XSS attempts rejected (</style><script>)
    • Invalid formats rejected (named colors, wrong length, invalid chars)
    • Nil handling works gracefully

Security Analysis

The existing validation is sufficient because:

  • ✅ Uses strict anchors (\A and \z) to match entire string
  • ✅ Only accepts # followed by exactly 6 hex characters
  • ✅ No semicolons, spaces, or special characters allowed
  • ✅ CSS/HTML injection is impossible

Closes #17

…color validation

The existing validation already prevents CSS injection by enforcing the format
/\A#[0-9a-fA-F]{6}\z/, which only allows valid 6-character hex colors.

Changes:
- Added nil guard to accent_color_rgb method to prevent crashes
- Created comprehensive test suite to verify:
  - Valid hex colors are accepted (upper and lowercase)
  - CSS injection attempts are rejected
  - XSS attempts with </style> tags are rejected
  - Invalid color formats are rejected
  - Nil values are handled gracefully

Fixes #17

Co-authored-by: Philip I. Thomas <philipithomas@users.noreply.github.com>
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.

Security: CSS injection via unsanitized accent_color in inline styles

2 participants