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: add https port parsing on registry matching for artifactory compatibility #6864

Conversation

FrederickEngelhardt
Copy link
Contributor

@FrederickEngelhardt FrederickEngelhardt commented Jul 26, 2023

Changes

* add compatibility back for always-auth=true so the authorization is still passed to components not matching the registry urls Removed due to security issue.

  • add https (:443 ) port filter in the URL parsing section so that authorization headers are added if the registry request matches.
  • This fixes problems with large organizations that use and share multiple scopes across registries

Issue

1. pnpm was deleting the authorization header, making registries that act as pass-through registries fail to fetch their tarball file download when attempting to access another registry with shared scope. Removed due to security issue.
2. pnpm was skipping https ports IE :443 that artifactory was transforming. This caused a registry mismatch and it skipped authorization. This is related to URL which does not return well-known ports (0-1023) in the .ports param. It still however parses and removes the https port.

Solution

Initial fix:
1. Add a logic block to protect against deletion of authorization if always-auth is provided in .npmrc (<=Node16) or the pnpm config >=Node18. Removed due to security issue.
2. Adding a matcher for :443 fixes the port problem so that when communicating with artifactory the authorization is not removed due to url mismatch.

Possible follow-up fixes related to auth (excluded from this PR scope)

  • add url parsing so that the authorization urls approved and authorization is not being passed to packages outside registry scope accidentally.
    • Example if you provided the registry=https://vllc.dev/artifactory/api/npm/npm-virtual/ adding a second key registry-root=https://vllc.dev/artifactory/api/npm/ would capture and add auth to all other registries in this structure and delete the rest.

pnpm config file

registry-root=https://vllc.dev/artifactory/api/npm/
registry=https://vllc.dev/artifactory/api/npm/npm-virtual/

Use case

This problem applies to all Artifactory registries with (>=2) registries and with scoped packages mixed across each registry. FYI this is a real world problem.

  • Any usage of another registry that could have blended scope
    • Example Installing a scope of @platform could resolve to multiple registries resolved by artifactory authorization.
  • Any usage of artifactory with more than 1 registry with blended scopes.

Due to a scoped package being unknown which registry to use, using the @platform:registry=https://<company_domain>/artifactory/api/npm/npm-virtual/ will not properly resolve the package correctly, this is because package-a could point to npm-virtual but package-b could point to npm-virtual-v2 and so on.

Artifactory continues to recommend in npm v9 to use the always-auth=true flag (which is deprecated in 18).

@welcome
Copy link

welcome bot commented Jul 26, 2023

💖 Thanks for opening this pull request! 💖
Please be patient and we will get back to you as soon as we can.

@FrederickEngelhardt FrederickEngelhardt force-pushed the fix/pnpm-issues-with-breaking-artifactory-virtual-registries branch from 078f8d6 to 80dae65 Compare July 26, 2023 05:22
@zkochan
Copy link
Member

zkochan commented Jul 26, 2023

add compatibility back for always-auth=true so the authorization is still passed to components not matching the registry urls

We cannot add this back. It was removed both from npm and pnpm as it creates a security vulnerability.

@FrederickEngelhardt
Copy link
Contributor Author

We cannot add this back. It was removed both from npm and pnpm as it creates a security vulnerability.

Can we do a registry origin check instead and then resolve the url only if it matches that domain or base path?

@zkochan
Copy link
Member

zkochan commented Jul 26, 2023

I'll think about it later. @weswigham you were the one who reported the always-auth issue, so I would be happy to have your feedback here.

@FrederickEngelhardt
Copy link
Contributor Author

FrederickEngelhardt commented Jul 26, 2023

@zkochan Since the first commit cannot be merged due to security reasons, I can remove it or make a new PR with only the HTTPS port fix. Related commit that should go in 9efa80a

This will allow for scoped packages to resolve correctly if they have the :443 port added in the request. Artifactory urls do this.

@FrederickEngelhardt FrederickEngelhardt force-pushed the fix/pnpm-issues-with-breaking-artifactory-virtual-registries branch from 9efa80a to fb68e29 Compare July 26, 2023 18:42
@FrederickEngelhardt FrederickEngelhardt changed the title feat(always-auth): add back support for always-auth for artifactory compatibility feat(always-auth): add https port parsing on registry matching for artifactory compatibility Jul 26, 2023
@FrederickEngelhardt
Copy link
Contributor Author

Renamed the PR and rebase dropped the always auth changes. This PR only contains the HTTPS port parsing logic now.

@FrederickEngelhardt
Copy link
Contributor Author

FrederickEngelhardt commented Jul 26, 2023

Noting that the following needs to be done to replicate the issue.

  1. Add a new dependency only available within artifactory or delete the pnpm-lock.yaml and have that dependency defined within your package.json
  2. Install a dependency that is published only in the artifactory scope IE pnpm install @platform/artifactory-only-platform-code
  3. Each replication make sure to do a rm pnpm-lock.yaml; ../pnpm/pnpm/dev/pd.js store prune; rm -rf node_modules; ../pnpm/pnpm/dev/pd.js install in your test repo that is on the same level as the pnpm repo.

Curiously only the artifactory urls fail that are unavailable externally. I'm wondering if it's defaulting to npm registry if the package fails to validate the registry. The output pnpm-lock.yaml still points to the :443 urls so at least the registry is preserved for the actual downloading part.

@zkochan
Copy link
Member

zkochan commented Jul 26, 2023

Can you cover this with a unit test?

@zkochan
Copy link
Member

zkochan commented Jul 26, 2023

Please also handle the case with port :80 and http. So there are two issues, port 443 on https and 80 on http.

Comment on lines 44 to 51
/**
* @artifactory adds a https port in the middle of their urls and URL.parse will not return ports in that range
*/
if (hasHTTPS && urlObj.port === '') {
return urlObj.href
}

if (urlObj.port === '') return originalUrl
Copy link
Member

Choose a reason for hiding this comment

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

It is probably just enough to do this:

Suggested change
/**
* @artifactory adds a https port in the middle of their urls and URL.parse will not return ports in that range
*/
if (hasHTTPS && urlObj.port === '') {
return urlObj.href
}
if (urlObj.port === '') return originalUrl
if (urlObj.port === '') return urlObj.href

it will handle both :80 and :443

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@zkochan agreed. I added tests for http|https|ws|wss and moved the removePort method to it's own file so it can be tested without exporting it.

@zkochan zkochan merged commit aa20818 into pnpm:main Jul 27, 2023
6 of 8 checks passed
@welcome
Copy link

welcome bot commented Jul 27, 2023

Congrats on merging your first pull request! 🎉🎉🎉

@FrederickEngelhardt FrederickEngelhardt changed the title feat(always-auth): add https port parsing on registry matching for artifactory compatibility feat(always-auth): add http|https|ws|wss port parsing on registry matching for artifactory compatibility Jul 27, 2023
@FrederickEngelhardt FrederickEngelhardt changed the title feat(always-auth): add http|https|ws|wss port parsing on registry matching for artifactory compatibility feat(always-auth): add https port parsing on registry matching for artifactory compatibility Jul 27, 2023
zkochan added a commit that referenced this pull request Jul 28, 2023
…tifactory compatibility (#6864)

Co-authored-by: Frederick Engelhardt <frederick.engelhardt@bestbuy.com>
Co-authored-by: Zoltan Kochan <z@kochan.io>
@zkochan zkochan changed the title feat(always-auth): add https port parsing on registry matching for artifactory compatibility fix: add https port parsing on registry matching for artifactory compatibility Jul 30, 2023
zkochan added a commit to teambit/bit that referenced this pull request Jul 30, 2023
zkochan added a commit to teambit/bit that referenced this pull request Aug 7, 2023
luvkapur pushed a commit to teambit/bit that referenced this pull request Aug 21, 2023
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.

None yet

2 participants