Skip to content

[SECURITY] Fix XSS sanitization gaps in DeviceCard — closes #8 - #38

Merged
elizabetheonoja-art merged 3 commits into
Utility-Protocol:mainfrom
spektre-labs:security/xss-sanitize-issue-8
Jun 23, 2026
Merged

[SECURITY] Fix XSS sanitization gaps in DeviceCard — closes #8#38
elizabetheonoja-art merged 3 commits into
Utility-Protocol:mainfrom
spektre-labs:security/xss-sanitize-issue-8

Conversation

@spektre-labs

Copy link
Copy Markdown
Contributor

Summary

Fixes #8 — DOM XSS sanitization for device meter labels.

Changes:

  • Extract sanitize() and validateLabel() to src/utils/sanitize.ts (shared, testable)
  • Update DeviceCard.tsx to import from the shared utility
  • Apply sanitize() to metrics values (previously unsanitized — String(value) was passed raw)
  • Add 14 unit tests in tests/unit/sanitize.test.ts covering all XSS vectors from the issue

Security impact

Before this fix, the metrics record values were rendered via {String(value)} without sanitization. While React's JSX escaping prevents direct script injection, applying sanitize() provides defense-in-depth against:

  • <script> / <img onerror=> payloads
  • javascript: URI schemes
  • on*= event handler injections

All three entry points (label, location, metrics.key, metrics.value) are now sanitized.

Test coverage

sanitize()
  ✓ strips < and > characters
  ✓ removes javascript: URI scheme (case-insensitive)
  ✓ strips onerror= and other on* event handlers
  ✓ leaves safe strings unchanged
  ✓ handles empty string
  ✓ handles combined payloads
  ✓ removes javascript: even when embedded in larger string

validateLabel()
  ✓ truncates to 64 characters
  ✓ leaves short labels unchanged
  ✓ sanitizes before truncating
  ✓ returns empty string for pure XSS label

Checklist

  • No external dependencies added (pure function, regex-based)
  • Returns "" for dangerous-only inputs
  • Truncation to 64 chars applied in validateLabel
  • useMemo used for safeLabel and safeLocation
  • Tests follow vitest config pattern (tests/**/*.test.ts)

Part of fix for issue Utility-Protocol#8 — XSS sanitization for device labels.
Apply sanitize() to metrics keys and values (previously unsanitized).
Import sanitize/validateLabel from shared utility.
Fixes issue Utility-Protocol#8.
Covers sanitize() and validateLabel() with known attack vectors
including script tags, javascript: URIs, and on* event handlers.
Fixes issue Utility-Protocol#8.

@elizabetheonoja-art elizabetheonoja-art left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@elizabetheonoja-art
elizabetheonoja-art merged commit 0e563b7 into Utility-Protocol:main Jun 23, 2026
3 checks passed
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.

Issue 9: Secure Client Sanitizers Combatting Custom Meter Label Script Injections

2 participants