Skip to content

Check credentials for configured registry before falling back to public npm registry #12798

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

Merged
merged 15 commits into from
Aug 11, 2025

Conversation

thavaahariharangit
Copy link
Contributor

@thavaahariharangit thavaahariharangit commented Aug 7, 2025

What are you trying to accomplish?

If the lockfile is missing, then it will collect the information from .npmrc or the dependabot.yml private registry credential configuration.

Anything you want to highlight for special attention from reviewers?

Maintains existing behavior when lockfile source information is available
Normalizes registry URLs by removing trailing slashes for consistent formatting

How will you know you've accomplished your goal?

Executed the Dependabot CLI and confirmed that it does not fall back to the public registry URL, and that there are no trailing backslashes causing 404 errors.

 proxy | 2025/08/07 13:06:03 [047] * authenticating npm registry request (host: jfrogghdemo.jfrog.io, token auth)
  proxy | 2025/08/07 13:06:03 [047] 200 https://jfrogghdemo.jfrog.io/artifactory/api/npm/e2e-tests-dependabot-npm/lodash
  proxy | 2025/08/07 13:06:04 [048] POST http://host.docker.internal:33637/update_jobs/cli/create_pull_request

Checklist

  • I have run the complete test suite to ensure all tests and linters pass.
  • I have thoroughly tested my code changes to ensure they work as expected, including adding additional tests for new functionality.
  • I have written clear and descriptive commit messages.
  • I have provided a detailed description of the changes in the pull request, including the problem it addresses, how it fixes the problem, and any relevant details about the implementation.
  • I have ensured that the code is well-documented and easy to understand.

@thavaahariharangit thavaahariharangit requested a review from a team as a code owner August 7, 2025 13:16
@thavaahariharangit thavaahariharangit changed the title Check credentials for registry url before falling back to public registry Check credentials for a configured registry before falling back to public registry Aug 7, 2025
@jakecoffman
Copy link
Member

Can you write a test so we can see it's working and make sure it doesn't fail in the future?

@thavaahariharangit
Copy link
Contributor Author

thavaahariharangit commented Aug 7, 2025

Can you write a test so we can see it's working and make sure it doesn't fail in the future?

Thanks @jakecoffman , RSpec tests are added as per the discussion.

@thavaahariharangit thavaahariharangit changed the title Check credentials for a configured registry before falling back to public registry Check credentials for configured registry before falling back to public npm registry Aug 8, 2025
Comment on lines 242 to 248
# Look for a credential that replaces the base registry (global registry replacement)
replaces_base_cred = credentials.find { |cred| cred["type"] == "npm_registry" && cred.replaces_base? }
return normalize_registry_url(replaces_base_cred["registry"]) if replaces_base_cred

# Look for any npm_registry credential as fallback
npm_cred = credentials.find { |cred| cred["type"] == "npm_registry" && cred["registry"] }
return normalize_registry_url(npm_cred["registry"]) if npm_cred
Copy link
Contributor

Choose a reason for hiding this comment

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

When we have a config with two npm ecosystems, one using a private registry and one that doesn't, will we always attempt to fetch metadata from the private registry?

Copy link
Contributor

Choose a reason for hiding this comment

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

It might be worth writing a test for this scenario

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@robaiken

This change enables Dependabot to work seamlessly in enterprise environments where public registry access is blocked by security policies.

Before this fix: Even with private registry credentials configured, Dependabot would still attempt to fetch metadata from the public registry, causing failures for firewall-restricted environments.

After this fix: Dependabot respects the configured private registry credentials and avoids unnecessary public registry calls.

Test scenario added: The "with multiple credentials" test ensures we have predictable behavior when multiple private registries are configured - always using the first available credential as fallback.

Copy link
Contributor Author

@thavaahariharangit thavaahariharangit Aug 8, 2025

Choose a reason for hiding this comment

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

Registry Selection Priority:

When lockfile source info is available: Uses the resolved registry URL from package-lock.json - credential-based selection is bypassed entirely.

When no lockfile source info exists: Falls back to credential-based registry selection in this order:

  • First credential with replaces-base: true
  • First regular npm_registry credential
  • Public registry registry.npmjs.org as final fallback

Key Point: The credential selection logic only activates when package-lock.json doesn't provide registry resolution information for a dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

@thavaahariharangit I feel two ecosystem test case testing is not needed as we are not writing this from scratch.

Copy link
Contributor

Choose a reason for hiding this comment

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

@thavaahariharangit Thanks for the detailed explanation! That helps clarify the behavior.

To directly answer my original question: if we have a monorepo where both ecosystems (one with a private registry and one without) are npm and both ecosystems have lockfiles that don't provide registry information for a dependency, are we going to try and get the info from the private registry?

Copy link
Contributor

Choose a reason for hiding this comment

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

@randhircs I disagree, the two ecosystem test case is valuable here. We might not be writing from scratch, but we're changing behavior, and mixed configs are common enough that we should have explicit coverage

Copy link
Contributor Author

@thavaahariharangit thavaahariharangit Aug 8, 2025

Choose a reason for hiding this comment

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

@robaiken

Ref:https://docs.github.com/en/code-security/dependabot/maintain-dependencies/removing-dependabot-access-to-public-registries#npm

We gave 2 option.

  1. Set replace_base=true
  2. When replace_base=true is not set then use .npmrc

Focus of this implementation is to ensure that if one of this option is set then it's not accessing the public registry.

If it is complex as mono repo (Having multiple projects in single repo) we definitely recommend them to use the lock file. Getting the registry url from global files like .npmrc or dependabot.yml will not be an option there.

But in a simple cases such as choosing between private or public, this option will be handy.

Are you suggesting to have a fallback logic only if replace_base=true is set?

Copy link
Contributor

@randhircs randhircs Aug 8, 2025

Choose a reason for hiding this comment

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

@robaiken Following our call discussion, please go ahead and test or share the failure scenarios for the specific case you mentioned. From my understanding, when there's a conflict between public and private registries, if the system defaults to the private registry and encounters a failure, that should be acceptable since private registries take precedence over public ones for customers.

randhircs
randhircs previously approved these changes Aug 8, 2025
Copy link
Contributor

@randhircs randhircs left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@randhircs randhircs left a comment

Choose a reason for hiding this comment

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

LGTM.

@thavaahariharangit thavaahariharangit merged commit 3901e55 into main Aug 11, 2025
83 checks passed
@thavaahariharangit thavaahariharangit deleted the harry/registry-url-from-credential branch August 11, 2025 13:36
robaiken added a commit that referenced this pull request Aug 12, 2025
…ic `npm` registry (#12798)

* Check credentials for registry url before falling back to public registry

* Lint fix

* Lint fixes

* RSpec added.

* Focusing only on registry url.

* updating the comments.

* updating the comments.

* Update npm_and_yarn/lib/dependabot/npm_and_yarn/metadata_finder.rb

Co-authored-by: Rob Aiken <[email protected]>

* Test added for multi credentials scenario

* fallback only if replaces true is

* Lint fixes

---------

Co-authored-by: Rob Aiken <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants