-
-
Notifications
You must be signed in to change notification settings - Fork 886
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
Improved hostname validation. #1143
Conversation
…nt rather than string length.
For anyone wanting to implement this now, it can be achieved with something like the following in your own implementation:
|
@sambauers Sam - thanks for the PR. Having issue is not a requirement, just a general recommendation in most projects to avoid spending time implementing changes that may be rejected (for whatever reason :). So no problem. I trust you that trailing dots in hostnames are valid, but it would help accepting if you maybe could point to a specific section of the relevant RFC to confirm it? Re the implementation itself. Ajv has two modes of format validation - "fast" (default) and "full" (more comprehensive and slower) - it's managed by the option Is it possible to modify HOSTNAME to pass this test as well, without complicating it too much? If not, then this particular test can be moved into code - please create a new file |
I'll see what I can do to get this up to scratch. |
This is the best explanation of the octet versus length issue I have found: |
The relevant RFC is 1034 (section 3.1) here:
|
You can see many trailing dots on domain names in the zone file example here: |
I have used a positive lookahead in the regex pattern for hostname to do the octet (character) count. The tests I originally wrote and added to the other hostname tests now pass. They are triggered by https://github.com/epoberezkin/ajv/blob/master/spec/schema-tests.spec.js#L33 I think, so no need for a new |
@sambauers If all that hostname now does is testing against the format, then the function can be just removed and replaced with the same regular expression in line 73. |
Thank you @sambauers |
I’ll release all pending changes next weekend |
Allow trailing dot. Check for octet count rather than string length.
Trailing dots in hostnames are valid, and also highly desirable in some contexts (e.g. DNS zone files).
Also, currently the validator is checking for string length of hostname, rather than the octet count. This is an edge case as few hostnames reach the limit, but technically incorrect. The fix is fairly straightforward and included here.
I have included tests for the length check, but the check for a domain that is too long is failing, as it appears that the validate function that is constructed for the tests bypasses the defined function
hostname()
inlib/compile/formats.js
and instead simply uses native regextest()
function. I couldn't troubleshoot this as it is all obfuscated in the generator inlib/dotjs/validate.js
. I have left the failing test in here as it appears to be an issue with AJV testing itself, rather than the implementation.What issue does this pull request resolve?
None, sorry. I didn't realise this was a requirement of submitting a pull request.
What changes did you make?
I updated the
format: hostname
regex to allow for trailing dots in hostnames. I also updated thehostname()
function. Both inlib/compile/formats.js
.Is there anything that requires more attention while reviewing?
The failing test that I mentioned. The validation test it generated for me was:
…which as you can see only validates the regex using:
if (!formats.hostname.test(data) ) {
- maybe I just put my test cases in the wrong place, or maybe this is something more fundamental. I would have thought it should somehow call thehostname()
function inlib/compile/formats.js
instead.