Skip to content

Commit

Permalink
Merge pull request #298 from AikidoSec/patch-status-code
Browse files Browse the repository at this point in the history
Check status code of API response
  • Loading branch information
willem-delbare authored Jul 25, 2024
2 parents 6326a44 + a8e9e2a commit ffb1f37
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
21 changes: 21 additions & 0 deletions library/agent/api/ReportingAPINodeHTTP.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function generateStartedEvent(): Event {
hostname: "hostname",
packages: {},
ipAddress: "ipAddress",
library: "firewall-node",
preventedPrototypePollution: false,
nodeEnv: "",
os: {
Expand Down Expand Up @@ -161,3 +162,23 @@ t.test("it deals with malformed JSON", async () => {
});
await stop();
});

t.test("it deals with 400", async () => {
const stop = await createTestEndpoint({ statusCode: 400, port: 3006 });
const api = new ReportingAPINodeHTTP(new URL("http://localhost:3006"));
t.same(await api.report(new Token("123"), generateStartedEvent(), 1000), {
success: false,
error: "unknown_error",
});
await stop();
});

t.test("it deals with 500", async () => {
const stop = await createTestEndpoint({ statusCode: 500, port: 3007 });
const api = new ReportingAPINodeHTTP(new URL("http://localhost:3007"));
t.same(await api.report(new Token("123"), generateStartedEvent(), 1000), {
success: false,
error: "unknown_error",
});
await stop();
});
12 changes: 8 additions & 4 deletions library/agent/api/ReportingAPINodeHTTP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ export class ReportingAPINodeHTTP implements ReportingAPI {
return { success: false, error: "invalid_token" };
}

try {
return JSON.parse(data);
} catch {
return { success: false, error: "unknown_error" };
if (statusCode === 200) {
try {
return JSON.parse(data);
} catch {
// Fall through
}
}

return { success: false, error: "unknown_error" };
}

async report(
Expand Down

0 comments on commit ffb1f37

Please sign in to comment.