Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports starting with multiple zeros pass the verification #2208

Closed
hancao97 opened this issue Mar 22, 2023 · 5 comments · Fixed by #2211
Closed

Ports starting with multiple zeros pass the verification #2208

hancao97 opened this issue Mar 22, 2023 · 5 comments · Fixed by #2211
Labels

Comments

@hancao97
Copy link

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

@niuzhihao
Copy link

I has the same problem! 😢

@anasshakil
Copy link
Contributor

Try this

allow_leading_zeroes: false

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

export default function isPort(str) {
  return isInt(str, { allow_leading_zeroes: false, min: 0, max: 65535 });
}

@hancao97
Copy link
Author

This is the internal implementation of validatorjs, so I can't pass in options:

https://github.com/validatorjs/validator.js/blob/master/src/lib/isPort.js

At the same time, we need to think about how to configure the port format in 'isUrl'

1 similar comment
@hancao97
Copy link
Author

This is the internal implementation of validatorjs, so I can't pass in options:

https://github.com/validatorjs/validator.js/blob/master/src/lib/isPort.js

At the same time, we need to think about how to configure the port format in 'isUrl'

@anasshakil
Copy link
Contributor

This is the internal implementation of validatorjs, so I can't pass in options:

https://github.com/validatorjs/validator.js/blob/master/src/lib/isPort.js

I will create PR but seems like maintainers are not actively merging PRs, that's why I’ve suggested you edit your local version.

anasshakil added a commit to anasshakil/validator.js that referenced this issue Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@hancao97 @anasshakil @niuzhihao and others