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

Create tighter URL field validations #221

Open
andylowry opened this issue Sep 5, 2018 · 0 comments
Open

Create tighter URL field validations #221

andylowry opened this issue Sep 5, 2018 · 0 comments

Comments

@andylowry
Copy link
Contributor

Recent implementation work resolving #209 has resulted in a significant weakening of URL validations. The reasons are:

  • The OAS specification (v3.0.2 draft) states:

    Unless specified otherwise, all properties that are URLs MAY be relative references as defined by RFC3986. Relative references are resolved using the URLs defined in the Server Object as a Base URI.
    In this context "relative reference" is used as in the referenced RFC, in the context of paths in hierarchical URLS. This has nothing to do with JSON references, except that reference strings in the latter may themselves include relative references, which are in that case resolved relative to the URL of the containing document.

  • As far as I can say, "specified otherwise" does not hold anywhere in the specification, so at this point I conclude that all URL fields should permit relative URLs, despite the fact that some explicitly say so and others do not.
  • This is probably not correct, but following a practice of preferring false negatives to false positives, KZOP currently permits relative URLs for all URL properties.

So that's one problem with URL validation - it's probably allowing relative URLs where they're not intended.

The second problem is that the current validation implementation is very weak, especially in the case of relative URLs.

The problems are that:

  • The URL(string) constructor allows lots of clearly bogus URL strings to succeed without throwing an exception. Apparently, many scheme-specific checks don't happen until the URL is opened.
  • The URL(URL, string) constructor, which is currently used to validate relative URLs, appears to let nearly anything through! E.g. http/:/ds, which is clearly not "relative" at all, but when used with a context URL, is accepted just fine.

What I propose for this is to not rely at all on URL constructors. Instead, use two steps:

  1. Use regex or other means to verify that the string conforms to the general syntactic requirements of the applicable RFC. This should handle both absolute and relative URLs, and the check should also permit us to distinguish the two.
  2. If step 1 passes and the URL is absolute, use Apache commons-validation to perform further validation. This will include scheme-specific checks for several common schemes.
  3. If step 1 succeeds and step 2 fails due to unrecognized scheme, validation passes.
  4. If step 1 succeeds and the URL is relative validation passes or fails depending on whether relative URLs are permitted.
  5. In all other cases, validation fails.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant