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

fix: url handle #247

Merged
merged 5 commits into from
Oct 12, 2021
Merged

fix: url handle #247

merged 5 commits into from
Oct 12, 2021

Conversation

climba03003
Copy link
Member

@climba03003 climba03003 commented Oct 11, 2021

It is a quick fix to #246

Fixes #246 Closes #245

I am think of how to reach the coverage to 100%. Some helps is appreciate.

Checklist

@mcollina mcollina requested a review from Eomm October 11, 2021 15:18
@climba03003 climba03003 changed the title Fix url handle fix: url handle Oct 11, 2021
try {
const parsed = new URL(url, 'http://localhost.com/')
return parsed.pathname + (parsed.pathname[parsed.pathname.length - 1] !== '/' ? '/' : '') + (parsed.search || '')
} catch (error) {
// the try-catch here is actually unreachable, but we keep it for safety and prevent DoS attack
Copy link
Member Author

@climba03003 climba03003 Oct 12, 2021

Choose a reason for hiding this comment

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

In here, the catch branch is actually unreachable after we convert all leading slash to a single /. But, it may be changed in future and URL will be crashed on some weird case.

So, I will keep the try {} catch {} here and ignore the coverage.

Why it is unreachable?
When we specify the host for in URL, the path can be either a full url or path. After we stripe the leading slash and it can only be path or schema://something in this case. And URL do not check the invalid encoded component. Thus, it is not able to crash anymore.

Copy link
Member

Choose a reason for hiding this comment

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

This code was reachable for this command:

curl --path-as-is "http://localhost:3000//^/.."

Copy link
Member Author

Choose a reason for hiding this comment

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

http://localhost:3000//^/.. is actually doing new URL('/^/..', 'http://localhost.com/') and it is not throwing.

Copy link
Member

Choose a reason for hiding this comment

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

on node 16.8 I get (see the double slash - this trigger the issue but your check hide this case)

➜  fastify-static git:(PULL_247) ✗ node       
Welcome to Node.js v16.8.0.
Type ".help" for more information.
> new URL('//^/..', 'http://localhost.com/')
Uncaught TypeError [ERR_INVALID_URL]: Invalid URL
    at __node_internal_captureLargerStackTrace (node:internal/errors:464:5)
    at new NodeError (node:internal/errors:371:5)
    at onParseError (node:internal/url:552:9)
    at new URL (node:internal/url:628:5) {
  input: '//^/..',
  code: 'ERR_INVALID_URL'
}

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina merged commit d871592 into fastify:master Oct 12, 2021
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

Successfully merging this pull request may close these issues.

Fix for CVE-2021-22964 (4.4.1) mistreats valid URLs & erroneously returns '/' coverage 100%
3 participants