|
| 1 | +const { getCascadingScans } = require("./hook"); |
| 2 | + |
| 3 | +let parentScan = undefined; |
| 4 | +let sslyzeCascadingRules = undefined; |
| 5 | + |
| 6 | +beforeEach(() => { |
| 7 | + parentScan = { |
| 8 | + apiVersion: "execution.experimental.securecodebox.io/v1", |
| 9 | + kind: "Scan", |
| 10 | + metadata: { |
| 11 | + name: "nmap-foobar.com", |
| 12 | + annotations: {} |
| 13 | + }, |
| 14 | + spec: { |
| 15 | + scanType: "nmap", |
| 16 | + parameters: "foobar.com", |
| 17 | + cascades: {} |
| 18 | + } |
| 19 | + }; |
| 20 | + |
| 21 | + sslyzeCascadingRules = [ |
| 22 | + { |
| 23 | + apiVersion: "cascading.experimental.securecodebox.io/v1", |
| 24 | + kind: "CascadingRule", |
| 25 | + metadata: { |
| 26 | + name: "tls-scans" |
| 27 | + }, |
| 28 | + spec: { |
| 29 | + matches: { |
| 30 | + anyOf: [ |
| 31 | + { |
| 32 | + category: "Open Port", |
| 33 | + attributes: { |
| 34 | + port: 443, |
| 35 | + service: "https" |
| 36 | + } |
| 37 | + }, |
| 38 | + { |
| 39 | + category: "Open Port", |
| 40 | + attributes: { |
| 41 | + service: "https" |
| 42 | + } |
| 43 | + } |
| 44 | + ] |
| 45 | + }, |
| 46 | + scanSpec: { |
| 47 | + scanType: "sslyze", |
| 48 | + parameters: ["--regular", "{{$.hostOrIP}}:{{attributes.port}}"] |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + ]; |
| 53 | +}); |
| 54 | + |
| 55 | +test("should create subsequent scans for open HTTPS ports (NMAP findings)", () => { |
| 56 | + const findings = [ |
| 57 | + { |
| 58 | + name: "Port 443 is open", |
| 59 | + category: "Open Port", |
| 60 | + attributes: { |
| 61 | + state: "open", |
| 62 | + hostname: "foobar.com", |
| 63 | + port: 443, |
| 64 | + service: "https" |
| 65 | + } |
| 66 | + } |
| 67 | + ]; |
| 68 | + |
| 69 | + const cascadedScans = getCascadingScans( |
| 70 | + parentScan, |
| 71 | + findings, |
| 72 | + sslyzeCascadingRules |
| 73 | + ); |
| 74 | + |
| 75 | + expect(cascadedScans).toMatchInlineSnapshot(` |
| 76 | + Array [ |
| 77 | + Object { |
| 78 | + "cascades": null, |
| 79 | + "generatedBy": "tls-scans", |
| 80 | + "name": "sslyze-foobar.com-tls-scans", |
| 81 | + "parameters": Array [ |
| 82 | + "--regular", |
| 83 | + "foobar.com:443", |
| 84 | + ], |
| 85 | + "scanType": "sslyze", |
| 86 | + }, |
| 87 | + ] |
| 88 | + `); |
| 89 | +}); |
| 90 | + |
| 91 | +test("Should create no subsequent scans if there are no rules", () => { |
| 92 | + const findings = [ |
| 93 | + { |
| 94 | + name: "Port 443 is open", |
| 95 | + category: "Open Port", |
| 96 | + attributes: { |
| 97 | + state: "open", |
| 98 | + hostname: "foobar.com", |
| 99 | + port: 443, |
| 100 | + service: "https" |
| 101 | + } |
| 102 | + } |
| 103 | + ]; |
| 104 | + |
| 105 | + const cascadingRules = []; |
| 106 | + |
| 107 | + const cascadedScans = getCascadingScans(parentScan, findings, cascadingRules); |
| 108 | + |
| 109 | + expect(cascadedScans).toMatchInlineSnapshot(`Array []`); |
| 110 | +}); |
| 111 | + |
| 112 | +test("should not try to do magic to the scan name if its something random", () => { |
| 113 | + parentScan.metadata.name = "foobar.com"; |
| 114 | + |
| 115 | + const findings = [ |
| 116 | + { |
| 117 | + name: "Port 443 is open", |
| 118 | + category: "Open Port", |
| 119 | + attributes: { |
| 120 | + state: "open", |
| 121 | + hostname: undefined, |
| 122 | + ip_address: "10.42.42.42", |
| 123 | + port: 443, |
| 124 | + service: "https" |
| 125 | + } |
| 126 | + } |
| 127 | + ]; |
| 128 | + |
| 129 | + const cascadedScans = getCascadingScans( |
| 130 | + parentScan, |
| 131 | + findings, |
| 132 | + sslyzeCascadingRules |
| 133 | + ); |
| 134 | + |
| 135 | + expect(cascadedScans).toMatchInlineSnapshot(` |
| 136 | + Array [ |
| 137 | + Object { |
| 138 | + "cascades": null, |
| 139 | + "generatedBy": "tls-scans", |
| 140 | + "name": "foobar.com-tls-scans", |
| 141 | + "parameters": Array [ |
| 142 | + "--regular", |
| 143 | + "10.42.42.42:443", |
| 144 | + ], |
| 145 | + "scanType": "sslyze", |
| 146 | + }, |
| 147 | + ] |
| 148 | + `); |
| 149 | +}); |
| 150 | + |
| 151 | +test("should not start scan when the cascadingrule for it is already in the chain", () => { |
| 152 | + parentScan.metadata.annotations["cascading.securecodebox.io/chain"] = |
| 153 | + sslyzeCascadingRules[0].metadata.name; |
| 154 | + |
| 155 | + const findings = [ |
| 156 | + { |
| 157 | + name: "Port 443 is open", |
| 158 | + category: "Open Port", |
| 159 | + attributes: { |
| 160 | + state: "open", |
| 161 | + hostname: "foobar.com", |
| 162 | + port: 443, |
| 163 | + service: "https" |
| 164 | + } |
| 165 | + } |
| 166 | + ]; |
| 167 | + |
| 168 | + const cascadedScans = getCascadingScans( |
| 169 | + parentScan, |
| 170 | + findings, |
| 171 | + sslyzeCascadingRules |
| 172 | + ); |
| 173 | + |
| 174 | + expect(cascadedScans).toMatchInlineSnapshot(`Array []`); |
| 175 | +}); |
0 commit comments