fix(security,config): enforce 0600 file permissions, dynamic account store path, and HTTP origin protection

This commit is contained in:
gitea committed 2026-09-29 23:34:06 +08:00
1 parent bd585db889
commit d0af46b445
5 files changed
+143 -19

No files matched your search

+51 -4
View File
@@ -7,13 +7,44 @@ import { tokenizeCommand, normalizeCliArgs } from "../server.js";
import { VERSION } from "../version.js";
import { renderDocsHtml } from "./docs.js";
export function isTrustedOrigin(originHeader: string | undefined): boolean {
if (!originHeader) return true; // non-browser / same-origin requests without Origin header
const allowedOriginEnv = process.env.HOSE_HTTP_CORS_ORIGIN?.trim();
if (allowedOriginEnv === "*") return true;
if (allowedOriginEnv && originHeader === allowedOriginEnv) return true;
try {
const parsed = new URL(originHeader);
const host = parsed.hostname.toLowerCase();
return host === "127.0.0.1" || host === "localhost" || host === "::1" || host === "[::1]";
} catch {
return false;
}
}
function checkHttpAuth(req: IncomingMessage): boolean {
const requiredToken = process.env.HOSE_HTTP_TOKEN?.trim();
if (!requiredToken) return true;
const authHeader = req.headers.authorization || "";
return authHeader === `Bearer ${requiredToken}`;
}
export async function handleHttpRequest(req: IncomingMessage, res: ServerResponse): Promise<void> {
// 1. CORS headers
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Hose-Corp");
const origin = typeof req.headers.origin === "string" ? req.headers.origin : undefined;
const trustedOrigin = isTrustedOrigin(origin);
// 1. CORS headers: only grant CORS to local/trusted origins instead of blind wildcard RCE
if (trustedOrigin) {
res.setHeader("Access-Control-Allow-Origin", origin || "http://127.0.0.1");
res.setHeader("Vary", "Origin");
res.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Hose-Corp");
}
if (req.method === "OPTIONS") {
if (!trustedOrigin) {
sendError(res, 403, "Forbidden cross-origin preflight request");
return;
}
res.writeHead(204);
res.end();
return;
@@ -71,6 +102,14 @@ export async function handleHttpRequest(req: IncomingMessage, res: ServerRespons
// 3. Command Execution Endpoint: POST /api/cli
if (method === "POST" && pathname === "/api/cli") {
if (!trustedOrigin) {
sendError(res, 403, "Forbidden: untrusted cross-origin request to /api/cli");
return;
}
if (!checkHttpAuth(req)) {
sendError(res, 401, "Unauthorized: valid Bearer token required");
return;
}
const body = (await parseJsonBody(req)) as Record<string, unknown> | null;
const command = (typeof body?.command === "string" ? body.command : "").trim();
if (!command) {
@@ -104,6 +143,14 @@ export async function handleHttpRequest(req: IncomingMessage, res: ServerRespons
// 4. RESTful: GET /v1/corps
if (method === "GET" && pathname === "/v1/corps") {
if (!trustedOrigin) {
sendError(res, 403, "Forbidden: untrusted cross-origin request to /v1/corps");
return;
}
if (!checkHttpAuth(req)) {
sendError(res, 401, "Unauthorized: valid Bearer token required");
return;
}
const configs = loadCorpConfigs();
const globalConfig = loadGlobalConfig();
sendJson(res, 200, {