Skip to content

isRgbColor accepts whitespace inside numeric values with allowSpaces: true #2885

Description

@emme1t

Describe the bug

With { allowSpaces: true }, isRgbColor removes whitespace inside numeric values and accepts malformed RGB/RGBA strings. For example, the first channel in rgb(2 55,0,0) is treated as 255.

Reproduction

const validator = require('validator');

for (const value of [
  'rgb(2 55,0,0)',
  'rgba(0,0,0,0. 5)',
  'rgb(25 %,0%,0%)',
]) {
  console.log(value, validator.isRgbColor(value, { allowSpaces: true }));
}

All three calls return true. The expected result is false: each channel or alpha value must form a contiguous numeric token, and a percentage sign must immediately follow its number. Whitespace around comma-separated values, such as rgb( 255 , 0 , 0 ), should remain supported.

The allowSpaces branch in src/lib/isRgbColor.js strips every whitespace character before checking the numeric patterns. A regression test covering these inputs fails on the current source.

Environment

  • validator.js 13.15.35; also reproduced from master at 9ff342479591ca5a43cb30000195bcf55c1bbed9
  • Node.js 24.16.0
  • Windows

Prepared with OpenAI Codex. The reproduction and regression test were run locally.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions