Skip to content

Commit c61aff9

Browse files
authored
Merge pull request #268 from NodeSecure/avoid-invalid-github-crash
Avoid invalid GitHub crash
2 parents 618033a + 198e0d6 commit c61aff9

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

public/js/components/home.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export class HomeView {
2828
const repoName = utils.getGithubRepositoryPath(
2929
utils.parseRepositoryUrl(repository)
3030
)
31+
if (repoName === null) {
32+
return;
33+
}
3134

3235
fetchScorecardData(repoName).then((data) => {
3336
if (data !== null) {

public/js/components/package/pannels/scorecard.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export class Scorecard {
2424
}
2525

2626
const repoName = utils.getGithubRepositoryPath(githubURL.href);
27+
if (repoName === null) {
28+
return;
29+
}
2730
const pannel = clone.getElementById("pan-scorecard");
2831
fetchScorecardData(repoName).then((data) => {
2932
if (!data) {

public/js/utils.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ import avatarURL from "../img/avatar-default.png";
44
window.activeLegendElement = null;
55

66
export function getGithubRepositoryPath(url) {
7-
const github = new URL(url);
7+
try {
8+
const github = new URL(url);
89

9-
return github.pathname.slice(
10-
1,
11-
github.pathname.includes(".git") ? -4 : github.pathname.length
12-
);
10+
return github.pathname.slice(
11+
1,
12+
github.pathname.includes(".git") ? -4 : github.pathname.length
13+
);
14+
}
15+
catch {
16+
return null;
17+
}
1318
}
1419

1520
/**

src/commands/http.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ import path from "node:path";
44
// Import Internal Dependencies
55
import { buildServer } from "../http-server/index.js";
66

7-
export async function start(json = "nsecure-result.json", options = {}) {
7+
export async function start(
8+
payloadFileBasename = "nsecure-result.json",
9+
options = {}
10+
) {
811
const port = Number(options.port);
9-
const dataFilePath = path.join(process.cwd(), json);
12+
const fileExtension = path.extname(payloadFileBasename);
13+
if (fileExtension !== ".json" && fileExtension !== "") {
14+
throw new Error("You must provide a JSON file (scanner payload) to open");
15+
}
16+
17+
const dataFilePath = path.join(
18+
process.cwd(),
19+
fileExtension === "" ? `${payloadFileBasename}.json` : payloadFileBasename
20+
);
1021

1122
const httpServer = buildServer(dataFilePath, {
1223
port: Number.isNaN(port) ? 0 : port

0 commit comments

Comments
 (0)