import { isSensitiveQueryParam, stripUrlCredentials } from "../../src/utils/url.js"; describe("isSensitiveQueryParam", () => { it("recognizes sensitive authentication parameter names", () => { const sensitive = [ "token", "authToken", "_authToken", "auth_token", "access_token", "accessToken", "download-token", "npm_token", "auth", "authorization", "secret", "client_secret", "password", "passwd", "pwd", "pass", "key", "api_key", "apiKey", "signature", "sig", "bearer", "jwt", "session", "session_token", "sessionToken", "credential", "credentials", "X-Amz-Security-Token", "X-Amz-Signature", "X-Amz-Credential", "auth[password]", "auth.password", "user[token]", ]; for (const param of sensitive) { expect(isSensitiveQueryParam(param)).toBe(true); } }); it("does not flag common non-sensitive query parameters", () => { const nonSensitive = [ "author", "authenticate", "format", "version", "download", "checksum", "path", "file", "tag", "branch", "v", "q", "X-Amz-Date", ]; for (const param of nonSensitive) { expect(isSensitiveQueryParam(param)).toBe(false); } }); }); describe("stripUrlCredentials", () => { it("removes user:password credentials from resolved URL", () => { const url = "https://user:password@registry.example.com/lodash/-/lodash-4.17.21.tgz"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/lodash/-/lodash-4.17.21.tgz"); }); it("removes token-only userinfo from resolved URL", () => { const url = "https://my-secret-token@registry.example.com/lodash/-/lodash-4.17.21.tgz"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/lodash/-/lodash-4.17.21.tgz"); }); it("removes query token from resolved URL", () => { const url = "https://registry.example.com/lodash/-/lodash-4.17.21.tgz?token=my-secret-token"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/lodash/-/lodash-4.17.21.tgz"); }); it("removes pass query parameter from resolved URL", () => { const url = "https://registry.example.com/pkg.tgz?pass=hunter2&version=1.0.0"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/pkg.tgz?version=1.0.0"); }); it("removes both user:password credentials and query tokens while preserving other query params and hash", () => { const url = "https://user:pass@registry.example.com/pkg/-/pkg-1.0.0.tgz?token=secret123&keep=true#hash40char"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/pkg/-/pkg-1.0.0.tgz?keep=true#hash40char"); }); it("removes camelCase and snake_case sensitive query parameters", () => { const url = "https://registry.example.com/pkg.tgz?authToken=123&api_key=456&version=1.0.0"; expect(stripUrlCredentials(url)).toBe("https://registry.example.com/pkg.tgz?version=1.0.0"); }); it("removes bracketed and dotted sensitive query parameters", () => { const urlBracket = "https://registry.example.com/pkg.tgz?auth[password]=hunter2&version=1.0.0"; expect(stripUrlCredentials(urlBracket)).toBe("https://registry.example.com/pkg.tgz?version=1.0.0"); const urlDot = "https://registry.example.com/pkg.tgz?auth.password=hunter2&version=1.0.0"; expect(stripUrlCredentials(urlDot)).toBe("https://registry.example.com/pkg.tgz?version=1.0.0"); }); it("removes AWS presigned token query parameters while preserving harmless parameters like X-Amz-Date", () => { const url = "https://s3.amazonaws.com/bucket/pkg.tgz?X-Amz-Security-Token=secret&X-Amz-Signature=sig123&X-Amz-Date=20261110&download=1"; expect(stripUrlCredentials(url)).toBe("https://s3.amazonaws.com/bucket/pkg.tgz?X-Amz-Date=20261110&download=1"); }); it("preserves commit hash fragment on git and tarball URLs", () => { const url = "https://user:password@registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"; expect(stripUrlCredentials(url)).toBe("https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"); }); it("preserves original URL unchanged when there are no credentials or sensitive parameters", () => { const fileUrl = "file:vendor/foo-1.0.0.tgz"; expect(stripUrlCredentials(fileUrl)).toBe(fileUrl); const mixedCaseUrl = "https://Registry.Example.COM/lodash/-/lodash-4.17.21.tgz?version=1.0.0"; expect(stripUrlCredentials(mixedCaseUrl)).toBe(mixedCaseUrl); }); it("strips user credentials from git+https URLs", () => { const url = "git+https://token:secret@github.com/org/repo.git#v1.0.0"; expect(stripUrlCredentials(url)).toBe("git+https://github.com/org/repo.git#v1.0.0"); }); it("preserves standard git@ user in git+ssh URLs without password", () => { const url = "git+ssh://git@github.com/org/repo.git#main"; expect(stripUrlCredentials(url)).toBe("git+ssh://git@github.com/org/repo.git#main"); const scpGitSsh = "git+ssh://git@github.com:org/repo.git"; expect(stripUrlCredentials(scpGitSsh)).toBe("git+ssh://git@github.com:org/repo.git"); }); it("strips non-git user credentials from git+ssh URLs", () => { const url = "git+ssh://user:password@github.com/org/repo.git#main"; expect(stripUrlCredentials(url)).toBe("git+ssh://github.com/org/repo.git#main"); const scpUrl = "git+ssh://secret@github.com:org/repo.git"; expect(stripUrlCredentials(scpUrl)).toBe("git+ssh://github.com:org/repo.git"); }); it("strips token-only user from git+ssh URLs", () => { const url = "git+ssh://my-token@github.com/org/repo.git#main"; expect(stripUrlCredentials(url)).toBe("git+ssh://github.com/org/repo.git#main"); }); it("handles pure SCP-style Git references correctly", () => { expect(stripUrlCredentials("git@github.com:org/repo.git")).toBe("git@github.com:org/repo.git"); expect(stripUrlCredentials("secret@github.com:org/repo.git")).toBe("github.com:org/repo.git"); expect(stripUrlCredentials("user:password@github.com:org/repo.git")).toBe("github.com:org/repo.git"); expect(stripUrlCredentials("user:p@ss@github.com:org/repo.git")).toBe("github.com:org/repo.git"); expect(stripUrlCredentials("secret@github.com:org/repo.git?ref=a@b")).toBe("github.com:org/repo.git?ref=a@b"); expect(stripUrlCredentials("git@github.com:org/repo.git?ref=a@b")).toBe("git@github.com:org/repo.git?ref=a@b"); expect(stripUrlCredentials("secret@github.com:org/@scope/repo.git")).toBe("github.com:org/@scope/repo.git"); expect(stripUrlCredentials("git@github.com:org/@scope/repo.git")).toBe("git@github.com:org/@scope/repo.git"); }); it("preserves relative and file paths containing scoped @ symbols", () => { expect(stripUrlCredentials("packages/@scope/foo")).toBe("packages/@scope/foo"); expect(stripUrlCredentials("./packages/@scope/foo")).toBe("./packages/@scope/foo"); expect(stripUrlCredentials("../packages/@scope/foo")).toBe("../packages/@scope/foo"); expect(stripUrlCredentials("@scope/foo")).toBe("@scope/foo"); }); it("handles non-standard or malformed URLs gracefully", () => { const url = "git+ssh://user:secret@github.com:org/repo.git?token=secret123#hash"; expect(stripUrlCredentials(url)).toBe("git+ssh://github.com:org/repo.git#hash"); }); it("drops parameters with malformed percent escapes in the fallback path rather than throwing", () => { const url = "git+ssh://host:org/repo.git?tok%E0%A4%A=x&keep=1"; expect(stripUrlCredentials(url)).toBe("git+ssh://host:org/repo.git?keep=1"); }); it("does not corrupt host and query when @ appears inside a query parameter in non-standard URLs", () => { const urlWithoutSlash = "git+ssh://github.com:org?ref=a@b"; expect(stripUrlCredentials(urlWithoutSlash)).toBe("git+ssh://github.com:org?ref=a@b"); const urlWithSlash = "git+ssh://github.com:org/repo.git?ref=a@b"; expect(stripUrlCredentials(urlWithSlash)).toBe("git+ssh://github.com:org/repo.git?ref=a@b"); }); it("strips the whole password when it contains @ in a malformed URL that new URL rejects", () => { expect(stripUrlCredentials("https://user:p@ss@host:org/repo.git")).toBe("https://host:org/repo.git"); }); it("fails closed when unencoded delimiters make credentials in non-standard URLs ambiguous", () => { const urlWithQuestion = "git+ssh://user:pa?ss@github.com:org/repo.git"; expect(stripUrlCredentials(urlWithQuestion)).toBe("git+ssh://github.com:org/repo.git"); const urlWithQuestionAndEqual = "git+ssh://user:pa?ss=foo@github.com:org/repo.git"; expect(stripUrlCredentials(urlWithQuestionAndEqual)).toBe("git+ssh://github.com:org/repo.git"); const urlWithHash = "git+ssh://user:pa#ss@github.com:org/repo.git"; expect(stripUrlCredentials(urlWithHash)).toBe("git+ssh://github.com:org/repo.git"); const urlWithHashAndEqual = "git+ssh://user:pa#ss=foo@github.com:org/repo.git"; expect(stripUrlCredentials(urlWithHashAndEqual)).toBe("git+ssh://github.com:org/repo.git"); }); it("returns undefined when given undefined", () => { expect(stripUrlCredentials(undefined)).toBeUndefined(); }); it("returns empty string when given empty string", () => { expect(stripUrlCredentials("")).toBe(""); }); });