Skip to content

Commit c2a1fa8

Browse files
Merge pull request #508 from AikidoSec/dont-report-zero-port
Only capture hostname if the port is known
2 parents 692a1d4 + fa7986c commit c2a1fa8

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

library/agent/Agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ export class Agent {
485485
this.logger.log(`Failed to wrap file ${filename} in module ${module}`);
486486
}
487487

488-
onConnectHostname(hostname: string, port: number | undefined) {
488+
onConnectHostname(hostname: string, port: number) {
489489
this.hostnames.add(hostname, port);
490490
}
491491

library/agent/Hostnames.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@ t.test("it works", async () => {
2121
{ hostname: "google.com", port: 80, hits: 1 },
2222
]);
2323

24-
hostnames.add("google.com", undefined);
24+
hostnames.add("google.com", 0);
25+
hostnames.add("google.com", -1);
2526
t.same(hostnames.asArray(), [
27+
{ hostname: "aikido.dev", port: 443, hits: 1 },
2628
{ hostname: "aikido.dev", port: 80, hits: 1 },
2729
{ hostname: "google.com", port: 80, hits: 1 },
28-
{ hostname: "google.com", port: undefined, hits: 1 },
2930
]);
3031

3132
hostnames.add("github.com", 80);
3233
t.same(hostnames.asArray(), [
34+
{ hostname: "aikido.dev", port: 80, hits: 1 },
3335
{ hostname: "google.com", port: 80, hits: 1 },
34-
{ hostname: "google.com", port: undefined, hits: 1 },
3536
{ hostname: "github.com", port: 80, hits: 1 },
3637
]);
3738

3839
hostnames.add("jetbrains.com", 80);
3940
t.same(hostnames.asArray(), [
40-
{ hostname: "google.com", port: undefined, hits: 1 },
41+
{ hostname: "google.com", port: 80, hits: 1 },
4142
{ hostname: "github.com", port: 80, hits: 1 },
4243
{ hostname: "jetbrains.com", port: 80, hits: 1 },
4344
]);

library/agent/Hostnames.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ export class Hostnames {
55

66
constructor(private readonly maxEntries: number = 200) {}
77

8-
add(hostname: string, port: number | undefined = -1) {
8+
add(hostname: string, port: number) {
9+
if (port <= 0) {
10+
return;
11+
}
12+
913
if (!this.map.has(hostname)) {
1014
this.map.set(hostname, new Map([[port, 1]]));
1115
} else {
@@ -46,7 +50,7 @@ export class Hostnames {
4650
Array.from(ports.entries()).map(([port, hits]) => {
4751
return {
4852
hostname,
49-
port: port === -1 ? undefined : port,
53+
port,
5054
hits,
5155
};
5256
})

library/sinks/Fetch.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ export class Fetch implements Wrapper {
2121
): InterceptorResult {
2222
// Let the agent know that we are connecting to this hostname
2323
// This is to build a list of all hostnames that the application is connecting to
24-
agent.onConnectHostname(hostname, port);
24+
if (typeof port === "number" && port > 0) {
25+
agent.onConnectHostname(hostname, port);
26+
}
2527
const context = getContext();
2628

2729
if (!context) {

library/sinks/HTTPRequest.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export class HTTPRequest implements Wrapper {
2323
): InterceptorResult {
2424
// Let the agent know that we are connecting to this hostname
2525
// This is to build a list of all hostnames that the application is connecting to
26-
agent.onConnectHostname(url.hostname, port);
26+
if (typeof port === "number" && port > 0) {
27+
agent.onConnectHostname(url.hostname, port);
28+
}
2729
const context = getContext();
2830

2931
if (!context) {

library/sinks/Undici.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export class Undici implements Wrapper {
3131
): InterceptorResult {
3232
// Let the agent know that we are connecting to this hostname
3333
// This is to build a list of all hostnames that the application is connecting to
34-
agent.onConnectHostname(hostname, port);
34+
if (typeof port === "number" && port > 0) {
35+
agent.onConnectHostname(hostname, port);
36+
}
3537
const context = getContext();
3638

3739
if (!context) {

0 commit comments

Comments
 (0)