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

Validate and normalize hosted url. #3030

Merged
merged 3 commits into from
Jul 8, 2021
Merged

Conversation

jonasfj
Copy link
Member

@jonasfj jonasfj commented Jun 8, 2021

Fixes #3024

We normalize the URL for a hosted pub server to have no slash if the
server is just a bare domain like https://example.com, but if the URL
contains a path like https://example.com/my-folder then we always
normalize to https://example.com/my-folder/.

The reason for normalizing the URL is to improve consistency in
pubspec.lock and make it easier to implement authentication without
risks of being tricked by incorrect prefixes.

Additionally, be normalizing to no slash for empty paths, and paths
always ending in a slash when path is non-empty, we gain the benefit
that relative URLs can always be constructed correctly using
hostedUrl.resolve('api/packages/$package').

This additionally forbids a few edge cases such as:

  • querystring in the hosted URL (https://server.com/?query),
  • fragment in the hosted URL (https://server.com/#hash),
  • user-info in the hosted URL (https://user:[email protected]).

These may have worked with previous versions of the pub client, but
most likely the querystring or fragment would cause URLs to be garbled.
Any user-info would likely have been ignored, this was not tested, any
usage of these options is considered unlikely.

Previously, dart pub publish would ignore the path in the hosted URL
and always upload to /api/packages/new. This commit fixes this issue.

We normalize the URL for a _hosted pub server_ to have no slash if the
server is just a bare domain like `https://example.com`, but if the URL
contains a path like `https://example.com/my-folder` then we always
normalize to `https://example.com/my-folder/`.

The reason for normalizing the URL is to improve consistency in
`pubspec.lock` and make it easier to implement authentication without
risks of being tricked by incorrect prefixes.

Additionally, be normalizing to no slash for empty paths, and paths
always ending in a slash when path is non-empty, we gain the benefit
that relative URLs can always be constructed correctly using
`hostedUrl.resolve('api/packages/$package')`.

This additionally forbids a few edge cases such as:
 * querystring in the hosted URL (`https://server.com/?query`),
 * fragment in the hosted URL (`https://server.com/#hash`),
 * user-info in the hosted URL (`https://user:[email protected]`).

These may have worked with previous versions of the `pub` client, but
most likely the _querystring_ or _fragment_ would cause URLs to be garbled.
Any user-info would likely have been ignored, this was not tested, any
usage of these options is considered unlikely.

Previously, `dart pub publish` would ignore the path in the hosted URL
and always upload to `/api/packages/new`. This commit fixes this issue.
@google-cla google-cla bot added the cla: yes label Jun 8, 2021
@jonasfj
Copy link
Member Author

jonasfj commented Jun 8, 2021

@themisir I'm hoping this will make authentication easier... Let me know what you think.

@themisir
Copy link
Contributor

themisir commented Jun 8, 2021

@themisir I'm hoping this will make authentication easier... Let me know what you think.

  1. Is there was a normalization function already existed? I would like to use it for normalizing URLs when adding a new credentials using dart login.
  2. I planned to also use custom normalization for matching request URLs to credential URLs. (already discussed this on #hackers-pub)

I am a bit busy for a few days. I will continue working on authentication soon 🤞

@jonasfj
Copy link
Member Author

jonasfj commented Jun 9, 2021

I am a bit busy for a few days. I will continue working on authentication soon

That's fine. I'm busy too finding bugs in the solver.

As long as we get back to it, there is no rush :D

@jonasfj jonasfj requested a review from athomas June 9, 2021 06:42
Copy link
Member

@athomas athomas left a comment

Choose a reason for hiding this comment

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

LGTM, comments are nits.

lib/src/command/lish.dart Outdated Show resolved Hide resolved
}

/// Cache value for [server].
Copy link
Member

Choose a reason for hiding this comment

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

Assuming that a publish command pretty much always needs to look at the server property, it seems easier to just have a final Uri server = (){ return ...; }(); property and get rid of the lazy loading.

Copy link
Member Author

@jonasfj jonasfj Jul 8, 2021

Choose a reason for hiding this comment

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

if only... members like argResults can't be accessed in an initializer 😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

HostedSource should check that url is valid!
3 participants