Skip to content

Commit 25c7388

Browse files
JSap0914zkochan
andauthored
fix(resolving): reject malformed jsr: and named-registry package names (empty scope/name, path separators) (#12677)
The jsr: specifier parser accepted scoped names with an empty scope or name and with path separators inside the name; the malformed name was folded into the @jsr/... npm name and flowed into registry URLs and metadata cache file paths. It now throws INVALID_JSR_PACKAGE_NAME for any name rejected by npm's package-name rules. The named-registry specifier parser (e.g. gh:) had the same gap: the scoped branch only rejected a missing '/' or trailing slash, and the unscoped branch validated nothing, while the downstream pick-package validator only rejects '/' in unscoped names. The parser now validates the final package name in every branch (including alias-derived names) and throws INVALID_NAMED_REGISTRY_PACKAGE_NAME for the same shapes. Rather than hand-rolling the shape checks, both parsers delegate to the validator the repo already ships: validate-npm-package-name (validForOldPackages) in TypeScript and its existing pacquet port is_valid_old_npm_package_name. Both fixes land in the TypeScript CLI and pacquet, with matching tests. --------- Co-authored-by: JSap0914 <[email protected]> Co-authored-by: Zoltan Kochan <[email protected]>
1 parent e2e3c81 commit 25c7388

17 files changed

Lines changed: 232 additions & 37 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pnpm/resolving.jsr-specifier-parser": patch
3+
"pnpm": patch
4+
---
5+
6+
pnpm now rejects `jsr:` specifiers whose package name is not a valid npm package name — an empty scope or name (e.g. `jsr:@scope/`), path separators inside the name, or any other shape `validate-npm-package-name` rejects — with `ERR_PNPM_INVALID_JSR_PACKAGE_NAME` instead of silently converting them into a malformed `@jsr/...` npm package name.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pnpm/resolving.npm-resolver": patch
3+
"pnpm": patch
4+
---
5+
6+
pnpm now rejects named-registry specifiers (e.g. `gh:`) whose package name is not a valid npm package name — an empty scope (e.g. `gh:@/bar`), path separators inside the name (e.g. `gh:@scope/../name`), or any other shape `validate-npm-package-name` rejects — with `ERR_PNPM_INVALID_NAMED_REGISTRY_PACKAGE_NAME` instead of passing the name through to registry URLs and metadata cache file paths.

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎deny.toml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ ignore = [
5252
# ships only in `hickory-proto` 0.26.1+. Revisit when reqwest moves to
5353
# hickory 0.26.
5454
{ id = "RUSTSEC-2026-0119", reason = "Temporary risk acceptance: reqwest 0.13.x (latest 0.13.3) is locked to hickory-resolver 0.25 and no release consumes hickory-proto 0.26.1+ yet; revisit on reqwest upgrade." },
55+
# quick-xml DoS advisories (quadratic duplicate-attribute check, unbounded
56+
# namespace-declaration allocation). quick-xml is reached only through
57+
# object_store's S3 XML response parsing in pnpr, so the XML comes from the
58+
# operator-configured S3-compatible storage backend, not from untrusted
59+
# clients. Both fixes ship only in quick-xml 0.41.0, which no object_store
60+
# release can resolve yet: object_store 0.13 requires ^0.39 and 0.14
61+
# requires ^0.40.1. Revisit when object_store consumes quick-xml 0.41+.
62+
{ id = "RUSTSEC-2026-0194", reason = "Temporary risk acceptance: XML parsed by quick-xml comes only from the operator-configured S3 backend via object_store; no object_store release consumes the patched quick-xml 0.41 yet." },
63+
{ id = "RUSTSEC-2026-0195", reason = "Temporary risk acceptance: XML parsed by quick-xml comes only from the operator-configured S3 backend via object_store; no object_store release consumes the patched quick-xml 0.41 yet." },
5564
]
5665

5766
# --- Licenses ------------------------------------------------------------

‎pacquet/crates/resolving-jsr-specifier-parser/Cargo.toml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ license.workspace = true
1111
repository.workspace = true
1212

1313
[dependencies]
14+
pacquet-resolving-parse-wanted-dependency = { workspace = true }
15+
1416
derive_more = { workspace = true }
1517
miette = { workspace = true }
1618

‎pacquet/crates/resolving-jsr-specifier-parser/src/lib.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use derive_more::{Display, Error};
1515
use miette::Diagnostic;
16+
use pacquet_resolving_parse_wanted_dependency::is_valid_old_npm_package_name;
1617

1718
/// Parsed `jsr:` specifier.
1819
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -43,8 +44,11 @@ pub enum ParseJsrSpecifierError {
4344
#[diagnostic(code(ERR_PNPM_MISSING_JSR_PACKAGE_SCOPE))]
4445
MissingScope,
4546

46-
/// Specifier carries a scope but no `/name` segment
47-
/// (e.g. `jsr:@foo` or `jsr:@foo@^1`).
47+
/// Specifier carries a scoped name that is not a valid npm package
48+
/// name: no `/name` segment (e.g. `jsr:@foo`), an empty scope or
49+
/// name (e.g. `jsr:@foo/`), path separators inside the name
50+
/// (e.g. `jsr:@foo/../bar`), or anything else
51+
/// `validate-npm-package-name` rejects.
4852
#[display("The package name '{pkg_name}' is invalid")]
4953
#[diagnostic(code(ERR_PNPM_INVALID_JSR_PACKAGE_NAME))]
5054
InvalidPackageName {
@@ -129,10 +133,16 @@ fn jsr_to_npm_package_name(jsr_pkg_name: &str) -> Result<String, ParseJsrSpecifi
129133
let Some(after_at) = jsr_pkg_name.strip_prefix('@') else {
130134
return Err(ParseJsrSpecifierError::MissingScope);
131135
};
136+
let invalid =
137+
|| ParseJsrSpecifierError::InvalidPackageName { pkg_name: jsr_pkg_name.to_string() };
138+
// The returned name is used in registry URLs and metadata cache file
139+
// paths, so anything that is not a valid npm package name must never
140+
// make it through.
141+
if !is_valid_old_npm_package_name(jsr_pkg_name) {
142+
return Err(invalid());
143+
}
132144
let Some((scope, name)) = after_at.split_once('/') else {
133-
return Err(ParseJsrSpecifierError::InvalidPackageName {
134-
pkg_name: jsr_pkg_name.to_string(),
135-
});
145+
return Err(invalid());
136146
};
137147
Ok(format!("@jsr/{scope}__{name}"))
138148
}

‎pacquet/crates/resolving-jsr-specifier-parser/src/tests.rs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ fn scope_without_name_is_an_error() {
8585
parse_jsr_specifier("jsr:@foo", None),
8686
Err(ParseJsrSpecifierError::InvalidPackageName { pkg_name: "@foo".to_string() }),
8787
);
88+
assert_eq!(
89+
parse_jsr_specifier("jsr:@foo/", None),
90+
Err(ParseJsrSpecifierError::InvalidPackageName { pkg_name: "@foo/".to_string() }),
91+
);
92+
assert_eq!(
93+
parse_jsr_specifier("jsr:@foo/@^1.0.0", None),
94+
Err(ParseJsrSpecifierError::InvalidPackageName { pkg_name: "@foo/".to_string() }),
95+
);
96+
}
97+
98+
#[test]
99+
fn empty_scope_is_an_error() {
100+
assert_eq!(
101+
parse_jsr_specifier("jsr:@/bar", None),
102+
Err(ParseJsrSpecifierError::InvalidPackageName { pkg_name: "@/bar".to_string() }),
103+
);
104+
}
105+
106+
#[test]
107+
fn path_separators_in_name_are_an_error() {
108+
for input in ["jsr:@foo/../bar", "jsr:@foo/bar/baz", r"jsr:@foo/bar\baz", r"jsr:@sco\pe/bar"] {
109+
let pkg_name = input.strip_prefix("jsr:").unwrap().to_string();
110+
assert_eq!(
111+
parse_jsr_specifier(input, None),
112+
Err(ParseJsrSpecifierError::InvalidPackageName { pkg_name }),
113+
"input: {input}",
114+
);
115+
}
88116
}
89117

90118
#[test]

‎pacquet/crates/resolving-npm-resolver/Cargo.toml‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ license.workspace = true
1111
repository.workspace = true
1212

1313
[dependencies]
14-
pacquet-config = { workspace = true }
15-
pacquet-lockfile = { workspace = true }
16-
pacquet-network = { workspace = true }
17-
pacquet-registry = { workspace = true }
18-
pacquet-resolving-jsr-specifier-parser = { workspace = true }
19-
pacquet-resolving-resolver-base = { workspace = true }
20-
pacquet-workspace-range-resolver = { workspace = true }
21-
pacquet-workspace-spec = { workspace = true }
14+
pacquet-config = { workspace = true }
15+
pacquet-lockfile = { workspace = true }
16+
pacquet-network = { workspace = true }
17+
pacquet-registry = { workspace = true }
18+
pacquet-resolving-jsr-specifier-parser = { workspace = true }
19+
pacquet-resolving-parse-wanted-dependency = { workspace = true }
20+
pacquet-resolving-resolver-base = { workspace = true }
21+
pacquet-workspace-range-resolver = { workspace = true }
22+
pacquet-workspace-spec = { workspace = true }
2223

2324
chrono = { workspace = true }
2425
dashmap = { workspace = true }

‎pacquet/crates/resolving-npm-resolver/src/parse_bare_specifier.rs‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use derive_more::{Display, Error};
1717
use miette::Diagnostic;
1818
use node_semver::{Range, Version};
1919
use pacquet_resolving_jsr_specifier_parser::{ParseJsrSpecifierError, parse_jsr_specifier};
20+
use pacquet_resolving_parse_wanted_dependency::is_valid_old_npm_package_name;
2021
use reqwest::Url;
2122

2223
use crate::pick_package_from_meta::{RegistryPackageSpec, RegistryPackageSpecType};
@@ -157,9 +158,13 @@ pub struct NamedRegistryPackageSpec {
157158
#[derive(Debug, Display, Error, Diagnostic, PartialEq, Eq)]
158159
#[non_exhaustive]
159160
pub enum ParseNamedRegistrySpecifierError {
160-
/// Scope without a `/<name>` segment. Always a configuration bug,
161-
/// refused so the user gets an immediate, actionable error instead
162-
/// of a confusing downstream 404.
161+
/// Package name that is not a valid npm package name — a missing
162+
/// or empty scope/name segment, path separators inside the name,
163+
/// or anything else `validate-npm-package-name` rejects. Always a
164+
/// configuration bug, refused so the user gets an immediate,
165+
/// actionable error instead of a confusing downstream 404 (or a
166+
/// name that escapes into registry URL paths and metadata cache
167+
/// file paths).
163168
#[display("The package name '{pkg_name}' in named registry '{registry_name}:' is invalid")]
164169
#[diagnostic(code(ERR_PNPM_INVALID_NAMED_REGISTRY_PACKAGE_NAME))]
165170
InvalidPackageName {
@@ -174,9 +179,9 @@ pub enum ParseNamedRegistrySpecifierError {
174179
///
175180
/// Returns `Ok(None)` for any specifier that does not use one of the
176181
/// configured aliases (no colon, alias unknown, body unparsable) so
177-
/// the caller can fall through to other resolvers. Errors only for
178-
/// recoverable-by-the-user input — a scoped name with no `/<name>`
179-
/// segment.
182+
/// the caller can fall through to other resolvers. Errors when the
183+
/// alias matches but the package name is malformed (see
184+
/// [`ParseNamedRegistrySpecifierError::InvalidPackageName`]).
180185
///
181186
/// Supported shapes:
182187
/// - `<alias>:[@<owner>/]<name>[@<version_selector>]`
@@ -216,12 +221,6 @@ pub fn parse_named_registry_specifier_to_registry_package_spec(
216221
} else {
217222
(&body[..last_at], Some(body[last_at + 1..].to_string()))
218223
};
219-
if !name_part.contains('/') || name_part.ends_with('/') {
220-
return Err(ParseNamedRegistrySpecifierError::InvalidPackageName {
221-
registry_name: registry_name.to_string(),
222-
pkg_name: name_part.to_string(),
223-
});
224-
}
225224
pkg_name = name_part.to_string();
226225
version_selector = ver_part;
227226
} else if package_alias.is_some_and(|alias| alias.starts_with('@')) {
@@ -244,6 +243,16 @@ pub fn parse_named_registry_specifier_to_registry_package_spec(
244243
version_selector = ver_part;
245244
}
246245

246+
// The name is used in registry URLs and metadata cache file paths, so
247+
// anything that is not a valid npm package name must never make it
248+
// through.
249+
if !is_valid_old_npm_package_name(&pkg_name) {
250+
return Err(ParseNamedRegistrySpecifierError::InvalidPackageName {
251+
registry_name: registry_name.to_string(),
252+
pkg_name,
253+
});
254+
}
255+
247256
let selector_input = version_selector.as_deref().unwrap_or(default_tag);
248257
let Some(selector) = get_version_selector_type(selector_input) else {
249258
return Ok(None);

‎pacquet/crates/resolving-npm-resolver/src/parse_bare_specifier/tests.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,53 @@ fn named_registry_scope_without_name_errors() {
378378
}
379379
}
380380

381+
#[test]
382+
fn named_registry_empty_scope_errors() {
383+
let gh = gh_aliases();
384+
let err =
385+
parse_named_registry_specifier_to_registry_package_spec("gh:@/bar", &gh, None, "latest")
386+
.expect_err("empty scope must error");
387+
assert!(
388+
matches!(err, ParseNamedRegistrySpecifierError::InvalidPackageName { .. }),
389+
"got {err:?}",
390+
);
391+
}
392+
393+
#[test]
394+
fn named_registry_dot_segment_name_errors() {
395+
let gh = gh_aliases();
396+
for input in ["gh:.", "gh:..", "gh:@acme/.", "gh:@acme/.."] {
397+
let err =
398+
parse_named_registry_specifier_to_registry_package_spec(input, &gh, None, "latest")
399+
.expect_err("dot-segment names must error");
400+
assert!(
401+
matches!(err, ParseNamedRegistrySpecifierError::InvalidPackageName { .. }),
402+
"got {err:?} for {input:?}",
403+
);
404+
}
405+
}
406+
407+
#[test]
408+
fn named_registry_path_separators_in_name_error() {
409+
let gh = gh_aliases();
410+
for input in [
411+
"gh:@acme/../foo",
412+
"gh:@acme/foo/bar",
413+
r"gh:@acme/foo\bar",
414+
r"gh:@sco\pe/foo",
415+
"gh:foo/../bar",
416+
r"gh:foo\bar",
417+
] {
418+
let err =
419+
parse_named_registry_specifier_to_registry_package_spec(input, &gh, None, "latest")
420+
.expect_err("path separators in the name must error");
421+
assert!(
422+
matches!(err, ParseNamedRegistrySpecifierError::InvalidPackageName { .. }),
423+
"got {err:?} for {input:?}",
424+
);
425+
}
426+
}
427+
381428
#[test]
382429
fn named_registry_version_only_no_alias_declines() {
383430
let gh = gh_aliases();

0 commit comments

Comments
 (0)