mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-29 07:21:16 -07:00
Fix: handle literal null from crowdsec api alerts (#7088)
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
This commit is contained in:
@@ -21,7 +21,7 @@ export default function Component({ service }) {
|
|||||||
return <Container service={service} error={alertsError ?? bansError} />;
|
return <Container service={service} error={alertsError ?? bansError} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!alerts && !bans) {
|
if (alerts === undefined && bans === undefined) {
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="crowdsec.alerts" />
|
<Block label="crowdsec.alerts" />
|
||||||
|
|||||||
@@ -86,4 +86,15 @@ describe("widgets/crowdsec/component", () => {
|
|||||||
|
|
||||||
expectBlockValue(container, "crowdsec.alerts", 499);
|
expectBlockValue(container, "crowdsec.alerts", 499);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("renders null responses as 0 counts", () => {
|
||||||
|
useWidgetAPI.mockImplementation(() => ({ data: null, error: undefined }));
|
||||||
|
|
||||||
|
const { container } = renderWithProviders(<Component service={{ widget: { type: "crowdsec" } }} />, {
|
||||||
|
settings: { hideErrors: false },
|
||||||
|
});
|
||||||
|
|
||||||
|
expectBlockValue(container, "crowdsec.alerts", 0);
|
||||||
|
expectBlockValue(container, "crowdsec.bans", 0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -102,6 +102,11 @@ export default async function crowdsecProxyHandler(req, res) {
|
|||||||
return res.status(status).json({ error: "Crowdsec API Error", data });
|
return res.status(status).json({ error: "Crowdsec API Error", data });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Crowdsec returns a literal null instead of an empty array when nothing matches
|
||||||
|
if (data?.toString().trim() === "null") {
|
||||||
|
return res.status(status).json([]);
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(status).send(data);
|
return res.status(status).send(data);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error("Exception calling Crowdsec API: %s", error.message);
|
logger.error("Exception calling Crowdsec API: %s", error.message);
|
||||||
|
|||||||
@@ -161,4 +161,29 @@ describe("widgets/crowdsec/proxy", () => {
|
|||||||
expect(res.statusCode).toBe(500);
|
expect(res.statusCode).toBe(500);
|
||||||
expect(res.body).toEqual({ error: "Failed to authenticate with Crowdsec" });
|
expect(res.body).toEqual({ error: "Failed to authenticate with Crowdsec" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("normalizes a literal null response to an empty array", async () => {
|
||||||
|
getServiceWidget.mockResolvedValue({
|
||||||
|
type: "crowdsec",
|
||||||
|
url: "http://cs",
|
||||||
|
username: "machine",
|
||||||
|
password: "pw",
|
||||||
|
});
|
||||||
|
|
||||||
|
httpProxy
|
||||||
|
.mockResolvedValueOnce([
|
||||||
|
200,
|
||||||
|
"application/json",
|
||||||
|
JSON.stringify({ token: "tok", expire: new Date(Date.now() + 60_000).toISOString() }),
|
||||||
|
])
|
||||||
|
.mockResolvedValueOnce([200, "application/json", Buffer.from("null")]);
|
||||||
|
|
||||||
|
const req = { query: { group: "g", service: "svc", endpoint: "alerts", index: "0" } };
|
||||||
|
const res = createMockRes();
|
||||||
|
|
||||||
|
await crowdsecProxyHandler(req, res);
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toEqual([]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user