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

Fix bug in package URL resolution due to startsWith containment check. #902

Merged
merged 2 commits into from
Mar 9, 2018

Conversation

aomarks
Copy link
Member

@aomarks aomarks commented Mar 9, 2018

If a dependency name began with the name of the package it was being resolved from, we would not resolve the dependency to the components directory, because we were checking for containment naively with string startsWith.

Real life example:

  • /repos/iron-icons/a.html imports ../iron-iconset-svg/b.html
  • We test "/repos/iron-iconset-svg/b.html".startsWith("/repos/iron-icons") and did not rewrite the url into the components directory, because it looked like it was already inside the package.
  • CHANGELOG.md has been updated

@aomarks aomarks requested review from rictic and justinfagnani March 9, 2018 05:52
If a dependency name began with the name of the package it was being
resolved from, we would not resolve the dependency to the components
directory, because we were checking for containment naively with string
startsWith.

Real life example:

  - "/repos/iron-icons/a.html" imports "../iron-iconset-svg/b.html"
  - We test "/repos/iron-iconset-svg/b.html".startsWith("/repos/iron-icons")
    and did not rewrite the url into the components directory, because
    it looked like it was already inside the package.
@@ -30,15 +30,13 @@ export class FSUrlLoader implements UrlLoader {
root: string;

constructor(root: string = '') {
if (root.endsWith('/')) {
Copy link
Member Author

@aomarks aomarks Mar 9, 2018

Choose a reason for hiding this comment

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

Seemed like this was both wrong (shouldn't it be not endsWith?) and also unnecessary, because the resolve right after normalizes the slash away anyway.

return url.startsWith('file://') &&
isPathInside(this.root, Uri.parse(url).fsPath);
const parsed = Uri.parse(url);
return parsed.scheme === 'file' && !parsed.authority &&
Copy link
Member Author

@aomarks aomarks Mar 9, 2018

Choose a reason for hiding this comment

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

Previously this was testing using startsWith, so if there was a host/authority (which we have a test for), there would be a missing slash, and it worked out. Now we need to explicitly check there is no authority.

Copy link
Contributor

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

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

Nice catch!

// If the path from the directory to the file does not require traversing up,
// then it must be either a descendent or the same directory. Note this is
// case-insensitive on Windows (which is what we want).
return !pathlib.relative(directory, file).startsWith('..');
Copy link
Contributor

Choose a reason for hiding this comment

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

A small issue that probably won't come up:

I think isPathInside('c:\\foo', 'd:\\bar') returns true right now. Maybe we should add an extra check if we're on windows?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. I've just switched to pull in path-is-inside, which already handles this case correctly. Note that it has the parameters in reverse order (which is the more intuitive order to me anyway). Also filed domenic/path-is-inside#7 to add typings there, but added custom ones here for now.

@@ -93,6 +93,7 @@
"jsonschema": "^1.1.0",
"minimatch": "^3.0.4",
"parse5": "^4.0.0",
"path-is-inside": "^1.0.2",
Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, zero dependencies and written by Domenic

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup!

@aomarks aomarks merged commit df60e48 into master Mar 9, 2018
@rictic rictic deleted the package-resolve-bug branch March 9, 2018 21:02
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.

3 participants