Skip to content

Commit 806c3ec

Browse files
authored
fix: suppress auth warning when PNPM_CONFIG_NPMRC_AUTH_FILE points at project .npmrc (#12679)
When npmrcAuthFile resolves to the same path as the project .npmrc, the user has explicitly opted in to trusting that file for auth. Before this fix, the warning was unconditionally fired for any auth env var in the project .npmrc, even when PNPM_CONFIG_NPMRC_AUTH_FILE pointed at it. Fix: compare the resolved npmrcAuthFile path against the workspace .npmrc path. If they match, allow env var expansion and skip the warning. Closes #12480
1 parent 25c7388 commit 806c3ec

7 files changed

Lines changed: 373 additions & 18 deletions

File tree

‎.changeset/rare-mice-peel.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pnpm/config.reader": patch
3+
"pnpm": patch
4+
---
5+
6+
pnpm no longer warns about ignored project-level auth settings when `PNPM_CONFIG_NPMRC_AUTH_FILE` points at the project `.npmrc` — setting it to that file is an explicit opt-in to trusting it, so auth env variables in it are expanded [pnpm/pnpm#12480](https://github.com/pnpm/pnpm/issues/12480).

‎pacquet/crates/config/src/api.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ pub trait GetHomeDir {
5959
/// Mirrors [`std::env::current_dir`]. Only used by code that
6060
/// genuinely needs the cwd — the `SmartDefault` for
6161
/// [`crate::Config::store_dir`] consults it on Windows for the
62-
/// drive-letter derivation. Code that needs a "starting path" — like
63-
/// [`crate::Config::current`] — takes a direct path parameter
64-
/// instead, because production passes a caller-supplied path (the
65-
/// canonicalized `--dir`) rather than the host's cwd.
62+
/// drive-letter derivation, and [`crate::Config::current`] anchors a
63+
/// relative `npmrcAuthFile` value at the cwd (matching where the file
64+
/// is actually read from, and pnpm's `path.resolve`). Code that needs
65+
/// a "starting path" — like [`crate::Config::current`] — otherwise
66+
/// takes a direct path parameter, because production passes a
67+
/// caller-supplied path (the canonicalized `--dir`) rather than the
68+
/// host's cwd.
6669
pub trait GetCurrentDir {
6770
/// Return the process's current working directory, or an error
6871
/// if it can't be determined. Mirrors [`std::env::current_dir`].

‎pacquet/crates/config/src/lib.rs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,7 +1719,7 @@ impl Config {
17191719
start_dir: &std::path::Path,
17201720
) -> Result<Self, LoadWorkspaceYamlError>
17211721
where
1722-
Sys: EnvVar + EnvVarOs + GetHomeDir + LinkProbe,
1722+
Sys: EnvVar + EnvVarOs + GetCurrentDir + GetHomeDir + LinkProbe,
17231723
{
17241724
// Re-anchor the path-valued defaults (`modules_dir`,
17251725
// `virtual_store_dir`) onto the caller-supplied starting directory.
@@ -1819,8 +1819,25 @@ impl Config {
18191819
};
18201820
let project_npmrc_dir =
18211821
workspace_yaml.as_ref().map_or(start_dir, |(base_dir, _)| base_dir.as_path());
1822+
let project_npmrc_path = project_npmrc_dir.join(".npmrc");
1823+
// When npmrcAuthFile explicitly points at the project .npmrc, the user has
1824+
// opted in to trusting it — allow auth env expansion and suppress the warning.
1825+
// A relative value (e.g. `PNPM_CONFIG_NPMRC_AUTH_FILE=.npmrc`) is anchored
1826+
// at the cwd — where the user-level read below actually reads it from, and
1827+
// how pnpm's `path.resolve` anchors it.
1828+
let project_is_trusted_auth_file = user_npmrc_path.as_deref().is_some_and(|user| {
1829+
if user.is_absolute() {
1830+
user == project_npmrc_path
1831+
} else {
1832+
Sys::current_dir().is_ok_and(|cwd| cwd.join(user) == project_npmrc_path)
1833+
}
1834+
});
18221835
let project_source = read_npmrc(project_npmrc_dir).map(|text| {
1823-
let mut auth = NpmrcAuth::from_project_ini::<Sys>(&text, project_npmrc_dir);
1836+
let mut auth = if project_is_trusted_auth_file {
1837+
NpmrcAuth::from_ini::<Sys>(&text, project_npmrc_dir)
1838+
} else {
1839+
NpmrcAuth::from_project_ini::<Sys>(&text, project_npmrc_dir)
1840+
};
18241841
auth.rescope_unscoped("<project>/.npmrc");
18251842
auth
18261843
});

‎pacquet/crates/config/src/npmrc_auth/tests.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,3 +1459,46 @@ fn json_env_normalizes_registry_url_key() {
14591459
Some("https://reg.example/"),
14601460
);
14611461
}
1462+
1463+
// Regression test for pnpm/pnpm#12480: from_project_ini warns and drops
1464+
// auth env vars (correct default); from_ini trusts them (used when
1465+
// PNPM_CONFIG_NPMRC_AUTH_FILE explicitly points at the project .npmrc).
1466+
#[test]
1467+
fn from_project_ini_warns_on_auth_env_placeholder() {
1468+
static_env!(Env, &[("MY_TOKEN", "secret")]);
1469+
1470+
let auth = NpmrcAuth::from_project_ini::<Env>(
1471+
"//registry.npmjs.org/:_authToken=${MY_TOKEN}\n",
1472+
Path::new(""),
1473+
);
1474+
1475+
assert!(
1476+
auth.warnings.iter().any(|w| w.contains("Ignored project-level auth setting")),
1477+
"expected auth warning but got: {:?}",
1478+
auth.warnings,
1479+
);
1480+
assert_eq!(
1481+
default_auth_token(&auth, "//registry.npmjs.org/"),
1482+
None,
1483+
"token must not be set when project .npmrc is untrusted",
1484+
);
1485+
}
1486+
1487+
#[test]
1488+
fn from_ini_expands_auth_env_placeholder_without_warning() {
1489+
static_env!(Env, &[("MY_TOKEN", "secret")]);
1490+
1491+
let auth =
1492+
NpmrcAuth::from_ini::<Env>("//registry.npmjs.org/:_authToken=${MY_TOKEN}\n", Path::new(""));
1493+
1494+
assert!(
1495+
!auth.warnings.iter().any(|w| w.contains("Ignored project-level auth setting")),
1496+
"unexpected auth warning: {:?}",
1497+
auth.warnings,
1498+
);
1499+
assert_eq!(
1500+
default_auth_token(&auth, "//registry.npmjs.org/"),
1501+
Some(Some("secret")),
1502+
"token must be expanded when the file is trusted via PNPM_CONFIG_NPMRC_AUTH_FILE",
1503+
);
1504+
}

0 commit comments

Comments
 (0)