Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions scanners/subfinder/parser/__snapshots__/parser.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,24 @@ exports[`should properly parse subfinder json file and add target domain to find
},
]
`;

exports[`should properly parse empty json file with includeTargetDomain=true 1`] = `
[
{
"attributes": {
"domain": "example.com",
"hostname": "example.com",
"ip_address": null,
"ip_addresses": [],
"source": "parser",
},
"category": "Subdomain",
"description": "Found subdomain example.com",
"identified_at": null,
"location": "example.com",
"name": "example.com",
"osi_layer": "NETWORK",
"severity": "INFORMATIONAL",
},
]
`;
12 changes: 7 additions & 5 deletions scanners/subfinder/parser/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const DOMAIN_FLAGS = ["-d", "-domain", "--domain"];
export async function parse(
fileContent,
scan,
includeTargetDomain = process.env["INCLUDE_TARGET_DOMAIN"]?.toLowerCase() ==
"true",
options = {},
) {
if (!fileContent && !includeTargetDomain) return [];
const includeTargetDomain = options.includeTargetDomain ??
(process.env["INCLUDE_TARGET_DOMAIN"]?.toLowerCase() === "true");

const targets = parseResultFile(fileContent);
const findings = transformToFindings(targets);
Expand Down Expand Up @@ -92,8 +92,10 @@ function transformToFindings(targets) {
* @param {*} fileContent
*/
function parseResultFile(fileContent) {
return fileContent
.trim()
const trimmed = fileContent.trim();
if (!trimmed) return [];

return trimmed
.split("\n")
.map((line) => JSON.parse(line));
}
29 changes: 26 additions & 3 deletions scanners/subfinder/parser/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ test("should properly parse empty json file", async () => {
expect(findings).toMatchSnapshot();
});

test("should properly parse empty json file with includeTargetDomain=true", async () => {
const scan = {
spec: {
scanType: "subfinder",
parameters: ["-timeout", "1", "-d", "example.com"],
},
metadata: {
annotations: {
"metadata.scan.securecodebox.io/subfinder":
"https://github.com/secureCodeBox/secureCodeBox",
},
},
};

const fileContent = await readFile(__dirname + "/__testFiles__/empty.jsonl", {
encoding: "utf8",
});
const findings = await parse(fileContent, scan, { includeTargetDomain: true });
// validate findings
expect(validateParser(findings)).toBeUndefined();
expect(findings).toMatchSnapshot();
});

test("should properly parse subfinder json file and add target domain to findings with param -d", async () => {
const scan = {
spec: {
Expand All @@ -62,7 +85,7 @@ test("should properly parse subfinder json file and add target domain to finding
encoding: "utf8",
},
);
const findings = await parse(fileContent, scan, "true");
const findings = await parse(fileContent, scan, { includeTargetDomain: true });
// validate findings
expect(validateParser(findings)).toBeUndefined();
expect(findings).toMatchSnapshot();
Expand All @@ -88,7 +111,7 @@ test("should properly parse subfinder json file and add target domain to finding
encoding: "utf8",
},
);
const findings = await parse(fileContent, scan, "true");
const findings = await parse(fileContent, scan, { includeTargetDomain: true });
// validate findings
expect(validateParser(findings)).toBeUndefined();
expect(findings).toMatchSnapshot();
Expand All @@ -114,7 +137,7 @@ test("should properly parse subfinder json file and add target domain to finding
encoding: "utf8",
},
);
const findings = await parse(fileContent, scan, "true");
const findings = await parse(fileContent, scan, { includeTargetDomain: true });
// validate findings
expect(validateParser(findings)).toBeUndefined();
expect(findings).toMatchSnapshot();
Expand Down
Loading