Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions packages/core/src/__tests__/command-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ function makeCtx(overrides?: Partial<CommandContext>): CommandContext {
channelId: "C1",
args: "",
platform: "slack",
reply: mock(async () => {}),
reply: mock(async () => {
/* noop */
}),
...overrides,
};
}
Expand All @@ -33,7 +35,9 @@ describe("CommandRegistry.register / get / getAll", () => {
const cmd: CommandDefinition = {
name: "help",
description: "Show help",
handler: async () => {},
handler: async () => {
/* noop */
},
};
registry.register(cmd);
expect(registry.get("help")).toBe(cmd);
Expand All @@ -49,12 +53,16 @@ describe("CommandRegistry.register / get / getAll", () => {
const a: CommandDefinition = {
name: "a",
description: "A",
handler: async () => {},
handler: async () => {
/* noop */
},
};
const b: CommandDefinition = {
name: "b",
description: "B",
handler: async () => {},
handler: async () => {
/* noop */
},
};
registry.register(a);
registry.register(b);
Expand All @@ -73,12 +81,16 @@ describe("CommandRegistry.register / get / getAll", () => {
const first: CommandDefinition = {
name: "ping",
description: "v1",
handler: async () => {},
handler: async () => {
/* noop */
},
};
const second: CommandDefinition = {
name: "ping",
description: "v2",
handler: async () => {},
handler: async () => {
/* noop */
},
};
registry.register(first);
registry.register(second);
Expand All @@ -99,7 +111,9 @@ describe("CommandRegistry.tryHandle", () => {

test("returns true and calls handler for registered command", async () => {
const registry = new CommandRegistry();
const handlerFn = mock(async () => {});
const handlerFn = mock(async () => {
/* noop */
});
registry.register({
name: "ping",
description: "Ping",
Expand Down Expand Up @@ -143,7 +157,9 @@ describe("CommandRegistry.tryHandle", () => {
},
});

const replyFn = mock(async () => {});
const replyFn = mock(async () => {
/* noop */
});
const ctx = makeCtx({ reply: replyFn });
const handled = await registry.tryHandle("boom", ctx);

Expand All @@ -157,7 +173,9 @@ describe("CommandRegistry.tryHandle", () => {

test("reply is NOT called when handler succeeds", async () => {
const registry = new CommandRegistry();
const replyFn = mock(async () => {});
const replyFn = mock(async () => {
/* noop */
});
registry.register({
name: "ok",
description: "OK",
Expand All @@ -181,7 +199,9 @@ describe("CommandRegistry.tryHandle", () => {
r1.register({
name: "shared",
description: "In r1",
handler: async () => {},
handler: async () => {
/* noop */
},
});

expect(r1.get("shared")).toBeDefined();
Expand Down
17 changes: 17 additions & 0 deletions packages/server/src/gateway/utils/rate-limiter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import { getDb } from "../../db/client.js";

export function getClientIp(headers: {
forwardedFor?: string;
realIp?: string;
}): string {
const forwarded = headers.forwardedFor?.split(",")[0]?.trim().toLowerCase();
if (forwarded) {
return forwarded;
Comment on lines +7 to +9
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Do not trust worker-supplied X-Forwarded-For

When the legacy secret-swap path has no /a/{agentId} in the URL, secret-proxy uses this value as the source for ResolutionFailureLimiter; but worker requests can supply arbitrary x-forwarded-for values, and the gateway HTTP proxy copies request headers through unchanged (forwardHeaders = { ...req.headers }). In that scenario, a compromised worker probing bogus lobu_secret_* placeholders can rotate this header to get a fresh bucket each time and avoid the 20-failure throttle entirely, so the restored helper should not key security limits on an untrusted forwarded header unless it is set/validated by a trusted proxy layer.

Useful? React with 👍 / 👎.

}

const realIp = headers.realIp?.trim().toLowerCase();
if (realIp) {
return realIp;
}

return "unknown";
}

/**
* Sweep expired `public.rate_limits` rows. Safe to call periodically.
*
Expand Down
Loading