Disable debug endpoint by default

This commit is contained in:
xixu-me committed 2026-03-11 14:07:24 +08:00
1 parent c7d015d7ba
commit d520140a6a
6 files changed
+39 -3

No files matched your search

+1
View File
@@ -0,0 +1 @@
CLAUDE.md
+9 -1
View File
@@ -27,6 +27,14 @@ import { createStandardResponse } from "./lib/types";
*/
const app = new Hono<{ Bindings: Env }>();
function isDebugModeEnabled(value?: string): boolean {
if (!value) {
return false;
}
return ["true", "1", "yes", "on"].includes(value.trim().toLowerCase());
}
/**
* Scheduled event handler for periodic maintenance tasks
* Executes every 5 minutes as configured in wrangler.jsonc
@@ -214,7 +222,7 @@ app
*/
.post("/debug", async (c) => {
// Check if debug mode is enabled via environment variable
if (!c.env.DEBUG_MODE) {
if (!isDebugModeEnabled(c.env.DEBUG_MODE)) {
return c.json(createStandardResponse(404, null), 404);
}
+24
View File
@@ -68,6 +68,18 @@ describe("Main App", () => {
});
describe("POST /debug", () => {
it("should return 404 when debug mode is unset", async () => {
delete mockEnv.DEBUG_MODE;
const request = new Request("http://localhost/debug", {
method: "POST",
body: JSON.stringify({ text: "Hello world" }),
});
const response = await app.fetch(request, mockEnv);
expect(response.status).toBe(404);
});
it("should return 404 when debug mode is disabled", async () => {
mockEnv.DEBUG_MODE = "false";
@@ -80,6 +92,18 @@ describe("Main App", () => {
expect(response.status).toBe(404);
});
it("should return 404 when debug mode is a non-empty falsey-like string", async () => {
mockEnv.DEBUG_MODE = "disabled";
const request = new Request("http://localhost/debug", {
method: "POST",
body: JSON.stringify({ text: "Hello world" }),
});
const response = await app.fetch(request, mockEnv);
expect(response.status).toBe(404);
});
it("should provide debug information when enabled", async () => {
mockEnv.DEBUG_MODE = "true";
+1 -1
View File
@@ -39,7 +39,7 @@ global.createMockEnv = (): Env => ({
PROXY_URLS:
"https://test1.example.com/jsonrpc,https://test2.example.com/jsonrpc",
PROXY_WEIGHTS: "1,1",
DEBUG_MODE: "true",
DEBUG_MODE: "false",
});
// Mock Request and Response for Cloudflare Workers environment
+1 -1
View File
@@ -192,7 +192,7 @@ export function createTestEnvironment() {
PROXY_URLS:
"https://test1.example.com/jsonrpc,https://test2.example.com/jsonrpc",
PROXY_WEIGHTS: "1,1",
DEBUG_MODE: "true",
DEBUG_MODE: "false",
};
return {
+3
View File
@@ -17,6 +17,9 @@ interface Env {
/** Comma-separated list of proxy weights (optional) */
PROXY_WEIGHTS?: string;
/** Enables the /debug endpoint only when set to an explicit truthy value */
DEBUG_MODE?: string;
}
/**