Skip to content
This repository was archived by the owner on Apr 3, 2019. It is now read-only.

fix(validation): Reject URLs with unexpected characters. - #2370

Merged
rfk merged 1 commit into
masterfrom
url-validation
Mar 28, 2018
Merged

fix(validation): Reject URLs with unexpected characters.#2370
rfk merged 1 commit into
masterfrom
url-validation

Conversation

@rfk

@rfk rfk commented Mar 28, 2018

Copy link
Copy Markdown
Contributor

Previously we could accept URLs with unescaped special characters such as newlines or unicode, which means we were depending on other layers of the code to handle them correctly. This change makes the
requestor responsible for properly escaping any special characters in their URLs before passing them in to us.

@philbooth r?

@rfk
rfk requested a review from philbooth March 28, 2018 00:20
@ghost ghost assigned rfk Mar 28, 2018
@ghost ghost added the waffle:active label Mar 28, 2018
Comment thread lib/routes/validators.js
module.exports.isValidUrl = function (url, hostnameRegex) {
let parsed
try {
parsed = new URL(url)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I switched to the new URL class because it does the right thing with unescaped special characers:

> const { URL } = require('url')
> new URL('https://test\u010Dme.com').href
'https://xn--testme-l2a.com/'
> new URL('https://test.me/special/\u010D/chars').href
'https://test.me/special/%C4%8D/chars'

Comment thread lib/routes/validators.js Outdated
module.exports.isValidUrl = function (redirect, hostnameRegex) {
var parsed = url.parse(redirect)
module.exports.url = function url(options) {
const validator = isA.string().max(512).uri(options)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice that in the place where we use this validator, it does max(255) itself. Is 512 here a touch on the generous side? Could we make this 255 and then eliminate the other call to max?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 512 here was just copied blindly from the function above; maybe we can remove the length-limit here and let individual schemas suggest one which works better? (e.g. I assume the .max(255) comes from an actual constraint on db column size.

Comment thread lib/routes/validators.js Outdated
return validator
}

module.exports.isValidUrl = function (url, hostnameRegex) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's at least 2 places in this module where this is dereferenced internally as module.exports.isValidUrl. If we named the function expression we could make those read nicer as isValidUrl or whatever.

(I realise this function already existed but as it's changed I figured it's fair game for comments)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, when I grep locally, this isn't even used outside the module. Do we need to export it?

One of the things that was a little bit confusing when I first read this diff was the dichotomy between the similarily-named url and isValidUrl exports. But if isValidUrl is just an internal helper for this module, that makes more sense to me, and I think it would be clearer if it was only callable locally.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, let's make it an internal helper.

Previously we could accept URLs with unescaped special characters
such as newlines or unicode, which means we were depending on other
layers of the code to handle them correctly.  This change makes the
requestor responsible for properly escaping any special characters
in their URLs before passing them in to us.
@rfk
rfk force-pushed the url-validation branch from e28b8ce to 10219aa Compare March 28, 2018 08:02
@rfk

rfk commented Mar 28, 2018

Copy link
Copy Markdown
Contributor Author

Updated.

@philbooth philbooth left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@rfk
rfk merged commit 10e934f into master Mar 28, 2018
@ghost ghost removed the waffle:review label Mar 28, 2018
@shane-tomlinson
shane-tomlinson deleted the url-validation branch April 18, 2018 12:46
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants