Skip to content

Escape color/attribute values to prevent SVG injection #12

Description

@productdevbook

Context

  • uqr#10 — Escape color attributes in renderSVG to prevent SVG/XML injection (1 comment)
  • uqr#9 — Escape SVG colour attributes to prevent XML injection (0 comments)
  • bwip-js#327 — Harmonize color regex & ignore SVG default black (6 comments)
  • bwip-js#329 — backgroundcolor with # doesn't work or throw error (1 comment)

Description

User-supplied color strings and other options are interpolated directly into SVG markup without sanitization. A malicious or malformed color value like red" onload="alert(1) could inject arbitrary SVG/XML attributes or content.

Current Status in etiket

The barcode SVG renderer has escapeXml() for text content, but color/fill values are inserted directly:

parts.push(`<rect ... fill="${color}"/>`);

The QR renderer similarly interpolates color values without escaping.

Proposed Solution

  1. Add input validation for color values — accept only valid CSS color formats (hex, rgb, hsl, named colors)
  2. Escape all attribute values that come from user input using XML attribute escaping
  3. Apply this consistently across all renderers (barcode, QR, matrix)
  4. Add a shared escapeAttr() utility that handles ", ', <, >, &
  5. Add tests with adversarial inputs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfrom-competitorFeature inspired by competitor libraries

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions