Skip to content

[node-core-library] PackageNameParser.tryParse skips validating the last character of @scope #6223

Backlog

Description

@trackoor

Summary

PackageNameParser.tryParse (and PackageName.tryParse / parse / validate / isValidName / combineParts) fails to validate the final character of @scope against _invalidNameCharactersRegExp. Scoped package names with a trailing space or invalid punctuation at the end of the scope (such as @types /node, @microsoft!/example-package, or @!/example-package) are accepted as valid, even though IPackageNameParserOptions documents that only letters, numbers, -, _, and . are allowed in package names.

Downstream in @microsoft/rush, rush add -p "@types /node" passes packageNameParser.isValidName instead of reporting ERROR: The package name "@types /node" is not valid., then fails later with ERROR: Process exited with code 1 when querying the npm registry, and rush list accepts "packageName": "@my-org /my-app" in rush.json while rejecting "@my org/my-app".

Repro steps

mkdir repro && cd repro
npm init -y >/dev/null
npm install @rushstack/node-core-library@5.25.2

node -e '
const { PackageName } = require("@rushstack/node-core-library");
console.log(PackageName.tryParse("@micro!soft/example-package"));
console.log(PackageName.tryParse("@microsoft!/example-package"));
console.log(PackageName.tryParse("@types /node"));
console.log(PackageName.isValidName("@!/example-package"));
'

Expected result:

@microsoft!/example-package, @types /node, and @!/example-package all fail validation with The package name "..." contains an invalid character: "..." (isValidName returns false), matching @micro!soft/example-package in PackageName.test.ts.

Actual result:

{
  scope: '@micro!soft',
  unscopedName: 'example-package',
  error: 'The package name "@micro!soft/example-package" contains an invalid character: "!"'
}
{ scope: '@microsoft!', unscopedName: 'example-package', error: '' }
{ scope: '@types ', unscopedName: 'node', error: '' }
true

Details

In libraries/node-core-library/src/PackageName.ts (commit 7dc4564245cb9cda018a8618696b7b27ddec3069), line 125 extracts result.scope without the / delimiter (result.scope = input.substr(0, indexOfScopeSlash)), so for @scope/unscoped-name, result.scope is "@scope". Lines 149-151 then build nameWithoutScopeSymbols using slice(1, -1) instead of slice(1):

// Convert "@scope/unscoped-name" --> "scopeunscoped-name"
const nameWithoutScopeSymbols: string =
  (result.scope ? result.scope.slice(1, -1) : '') + result.unscopedName;

"@scope".slice(1, -1) evaluates to "scop" (and "@!".slice(1, -1) evaluates to ""), omitting the last character of result.scope from the _invalidNameCharactersRegExp check on line 169.

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
Package name: @rushstack/node-core-library (also verified via @microsoft/rush 5.182.1)
Package version? 5.25.2 (and main at 7dc4564245cb9cda018a8618696b7b27ddec3069)
Operating system? Linux
Would you consider contributing a PR? Maybe
Node.js version (node -v)? v20.20.2

Credit: https://ucsbplse.github.io/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status
      Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions