Skip to content

Ports starting with multiple zeros pass the verification #2208

Closed
@hancao97

Description

@hancao97

Describe the bug
When I add 0 in front of a port string, just like ‘00065535’,I expect false,but the function return true.

Examples

validator.isPort('0001');
validator.isURL('https://www.google.com:0001');
// return true, but expect false

Source Code:
I can't configure the options of isInt when I use isPort api

// isPort
import isInt from './isInt';

export default function isPort(str) {
  return isInt(str, { min: 0, max: 65535 });
}
// isInt
import assertString from './util/assertString';

const int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/;
const intLeadingZeroes = /^[-+]?[0-9]+$/;

export default function isInt(str, options) {
  assertString(str);
  options = options || {};

  // Get the regex to use for testing, based on whether
  // leading zeroes are allowed or not.
  let regex = (
    options.hasOwnProperty('allow_leading_zeroes') && !options.allow_leading_zeroes ?
      int : intLeadingZeroes
  );

  // Check min/max/lt/gt
  let minCheckPassed = (!options.hasOwnProperty('min') || str >= options.min);
  let maxCheckPassed = (!options.hasOwnProperty('max') || str <= options.max);
  let ltCheckPassed = (!options.hasOwnProperty('lt') || str < options.lt);
  let gtCheckPassed = (!options.hasOwnProperty('gt') || str > options.gt);

  return regex.test(str) && minCheckPassed && maxCheckPassed && ltCheckPassed && gtCheckPassed;
}

Additional context
Validator.js version: 13.7.7
Node.js version: v16.12.0
OS platform: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions