Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 4ce96cc

Browse files
authored
Merge pull request #63 from secureCodeBox/fix/scheduled-scan-cascading-scans
Fix Declarative CombinedScans Hooks from crashing when used by a ScheduledScan
2 parents c7ce097 + fc726be commit 4ce96cc

File tree

3 files changed

+69
-28
lines changed

3 files changed

+69
-28
lines changed

hooks/declarative-subsequent-scans/hook.test.js

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ beforeEach(() => {
99
kind: "Scan",
1010
metadata: {
1111
name: "nmap-foobar.com",
12-
annotations: {}
12+
annotations: {},
1313
},
1414
spec: {
1515
scanType: "nmap",
1616
parameters: "foobar.com",
17-
cascades: {}
18-
}
17+
cascades: {},
18+
},
1919
};
2020

2121
sslyzeCascadingRules = [
2222
{
2323
apiVersion: "cascading.experimental.securecodebox.io/v1",
2424
kind: "CascadingRule",
2525
metadata: {
26-
name: "tls-scans"
26+
name: "tls-scans",
2727
},
2828
spec: {
2929
matches: {
@@ -32,23 +32,23 @@ beforeEach(() => {
3232
category: "Open Port",
3333
attributes: {
3434
port: 443,
35-
service: "https"
36-
}
35+
service: "https",
36+
},
3737
},
3838
{
3939
category: "Open Port",
4040
attributes: {
41-
service: "https"
42-
}
43-
}
44-
]
41+
service: "https",
42+
},
43+
},
44+
],
4545
},
4646
scanSpec: {
4747
scanType: "sslyze",
48-
parameters: ["--regular", "{{$.hostOrIP}}:{{attributes.port}}"]
49-
}
50-
}
51-
}
48+
parameters: ["--regular", "{{$.hostOrIP}}:{{attributes.port}}"],
49+
},
50+
},
51+
},
5252
];
5353
});
5454

@@ -61,9 +61,9 @@ test("should create subsequent scans for open HTTPS ports (NMAP findings)", () =
6161
state: "open",
6262
hostname: "foobar.com",
6363
port: 443,
64-
service: "https"
65-
}
66-
}
64+
service: "https",
65+
},
66+
},
6767
];
6868

6969
const cascadedScans = getCascadingScans(
@@ -97,9 +97,9 @@ test("Should create no subsequent scans if there are no rules", () => {
9797
state: "open",
9898
hostname: "foobar.com",
9999
port: 443,
100-
service: "https"
101-
}
102-
}
100+
service: "https",
101+
},
102+
},
103103
];
104104

105105
const cascadingRules = [];
@@ -121,9 +121,9 @@ test("should not try to do magic to the scan name if its something random", () =
121121
hostname: undefined,
122122
ip_address: "10.42.42.42",
123123
port: 443,
124-
service: "https"
125-
}
126-
}
124+
service: "https",
125+
},
126+
},
127127
];
128128

129129
const cascadedScans = getCascadingScans(
@@ -160,9 +160,9 @@ test("should not start scan when the cascadingrule for it is already in the chai
160160
state: "open",
161161
hostname: "foobar.com",
162162
port: 443,
163-
service: "https"
164-
}
165-
}
163+
service: "https",
164+
},
165+
},
166166
];
167167

168168
const cascadedScans = getCascadingScans(
@@ -173,3 +173,41 @@ test("should not start scan when the cascadingrule for it is already in the chai
173173

174174
expect(cascadedScans).toMatchInlineSnapshot(`Array []`);
175175
});
176+
177+
test("should not crash when the annotations are not set", () => {
178+
parentScan.metadata.annotations = undefined;
179+
180+
const findings = [
181+
{
182+
name: "Port 443 is open",
183+
category: "Open Port",
184+
attributes: {
185+
state: "open",
186+
hostname: "foobar.com",
187+
port: 443,
188+
service: "https",
189+
},
190+
},
191+
];
192+
193+
const cascadedScans = getCascadingScans(
194+
parentScan,
195+
findings,
196+
sslyzeCascadingRules
197+
);
198+
199+
expect(cascadedScans).toMatchInlineSnapshot(`
200+
Array [
201+
Object {
202+
"cascades": null,
203+
"generatedBy": "tls-scans",
204+
"name": "sslyze-foobar.com-tls-scans",
205+
"parameters": Array [
206+
"--regular",
207+
"foobar.com:443",
208+
],
209+
"scanType": "sslyze",
210+
},
211+
]
212+
`);
213+
});

hooks/declarative-subsequent-scans/hook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ export function getCascadingScans(
5252
const cascadingRuleChain = new Set<string>();
5353

5454
// Get the current Scan Chain (meaning which CascadingRules were used to start this scan and its parents) and convert it to a set, which makes it easier to query.
55-
if (parentScan.metadata.annotations["cascading.securecodebox.io/chain"]) {
55+
if (
56+
parentScan.metadata.annotations &&
57+
parentScan.metadata.annotations["cascading.securecodebox.io/chain"]
58+
) {
5659
const chainElements = parentScan.metadata.annotations[
5760
"cascading.securecodebox.io/chain"
5861
].split(",");

hooks/declarative-subsequent-scans/scan-helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export async function startSubsequentSecureCodeBoxScan({
6565
}) {
6666
let cascadingChain: Array<string> = [];
6767

68-
if (parentScan.metadata.annotations["cascading.securecodebox.io/chain"]) {
68+
if (parentScan.metadata.annotations && parentScan.metadata.annotations["cascading.securecodebox.io/chain"]) {
6969
cascadingChain = parentScan.metadata.annotations[
7070
"cascading.securecodebox.io/chain"
7171
].split(",");

0 commit comments

Comments
 (0)