-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scanner/depWalker): add missing metadata when manifest does not …
…exists on remote registry
- Loading branch information
1 parent
d06b76e
commit b2dba04
Showing
9 changed files
with
259 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Import Third-party Dependencies | ||
import type { Contact } from "@nodesecure/npm-types"; | ||
|
||
// CONSTANTS | ||
const kStrAuthorRegexp = /^([^<(]+?)?[ \t]*(?:<([^>(]+?)>)?[ \t]*(?:\(([^)]+?)\)|$)/g; | ||
|
||
export function manifestAuthor(author: string | Contact | undefined): Contact | null { | ||
if (author === void 0) { | ||
return null; | ||
} | ||
|
||
if (typeof author === "string") { | ||
if (author.trim() === "") { | ||
return null; | ||
} | ||
|
||
const [_, name, email, url] = kStrAuthorRegexp.exec(author) ?? []; | ||
Check failure Code scanning / CodeQL Polynomial regular expression used on uncontrolled data High
This
regular expression Error loading related location Loading library input Error loading related location Loading |
||
kStrAuthorRegexp.lastIndex = 0; | ||
|
||
return { name, email, url }; | ||
} | ||
|
||
return author; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
workspaces/scanner/test/fixtures/depWalker/non-npm-package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "non-npm-package", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"description": "", | ||
"dependencies": { | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/NodeSecure/non-npm-package.git" | ||
}, | ||
"author": "NodeSecure", | ||
"homepage": "https://nodesecure.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Import Node.js Dependencies | ||
import assert from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
|
||
// Import Third-party Dependencies | ||
import { PackageJSON, PackumentVersion } from "@nodesecure/npm-types"; | ||
|
||
// Import Internal Dependencies | ||
import * as utils from "../../src/utils/index.js"; | ||
|
||
describe("utils.getLinks", () => { | ||
it("should return all links", () => { | ||
assert.deepStrictEqual(utils.getLinks({ | ||
homepage: "https://github.com/foo/bar", | ||
repository: "git@github.com:foo/bar.git", | ||
name: "foo", | ||
version: "1.0.0" | ||
} as any as PackumentVersion), { | ||
npm: "https://www.npmjs.com/package/foo/v/1.0.0", | ||
homepage: "https://github.com/foo/bar", | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
|
||
it("homepage should be null but repository should be parsed", () => { | ||
assert.deepStrictEqual(utils.getLinks({ | ||
homepage: null, | ||
repository: "https://github.com/foo/bar.git", | ||
name: "foo", | ||
version: "1.0.0" | ||
} as any), { | ||
npm: "https://www.npmjs.com/package/foo/v/1.0.0", | ||
homepage: null, | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
|
||
it("should return repository.url", () => { | ||
assert.deepStrictEqual(utils.getLinks({ | ||
name: "foo", | ||
version: "1.0.0", | ||
homepage: "https://github.com/foo/bar", | ||
repository: { | ||
type: "git", | ||
url: "github.com/foo/bar" | ||
} | ||
} as any), { | ||
npm: "https://www.npmjs.com/package/foo/v/1.0.0", | ||
homepage: "https://github.com/foo/bar", | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
}); | ||
|
||
describe("utils.getManifestLinks", () => { | ||
it("should return homepage and repository", () => { | ||
assert.deepStrictEqual(utils.getManifestLinks({ | ||
homepage: "https://github.com/foo/bar", | ||
repository: "https://github.com/foo/bar" | ||
} as PackageJSON), { | ||
npm: null, | ||
homepage: "https://github.com/foo/bar", | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
|
||
it("should return repository only", () => { | ||
assert.deepStrictEqual(utils.getManifestLinks({ | ||
homepage: null, | ||
repository: "https://github.com/foo/bar" | ||
} as any), { | ||
npm: null, | ||
homepage: null, | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
|
||
it("should return repository.url", () => { | ||
assert.deepStrictEqual(utils.getManifestLinks({ | ||
homepage: null, | ||
repository: { | ||
type: "git", | ||
url: "https://github.com/foo/bar" | ||
} | ||
} as any), { | ||
npm: null, | ||
homepage: null, | ||
repository: "https://github.com/foo/bar" | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Import Node.js Dependencies | ||
import assert from "node:assert/strict"; | ||
import { describe, it } from "node:test"; | ||
|
||
// Import Internal Dependencies | ||
import * as utils from "../../src/utils/index.js"; | ||
|
||
describe("utils.manifestAuthor", () => { | ||
it("should return null when given undefined", () => { | ||
assert.strictEqual(utils.manifestAuthor(undefined), null); | ||
}); | ||
|
||
it("should return null when given empty string", () => { | ||
assert.strictEqual(utils.manifestAuthor(""), null); | ||
}); | ||
|
||
it("should return author object with only name", () => { | ||
assert.deepStrictEqual(utils.manifestAuthor("John Doe"), { | ||
name: "John Doe", | ||
email: void 0, | ||
url: void 0 | ||
}); | ||
}); | ||
|
||
it("should return author object with name and email", () => { | ||
assert.deepStrictEqual(utils.manifestAuthor("John Doe <john@doe.com>"), { | ||
name: "John Doe", | ||
email: "john@doe.com", | ||
url: void 0 | ||
}); | ||
}); | ||
|
||
it("should return author object with name, email and url", () => { | ||
assert.deepStrictEqual(utils.manifestAuthor("John Doe <john@doe.com> (john.com)"), { | ||
name: "John Doe", | ||
email: "john@doe.com", | ||
url: "john.com" | ||
}); | ||
}); | ||
|
||
it("should return given author object", () => { | ||
const author = { | ||
name: "John Doe", | ||
email: "john@doe.com", | ||
url: "john.com" | ||
}; | ||
|
||
assert.deepStrictEqual(utils.manifestAuthor(author), author); | ||
}); | ||
}); |