fix(security,config): enforce 0600 file permissions, dynamic account store path, and HTTP origin protection
This commit is contained in:
1 parent
bd585db889
commit
d0af46b445
5 files changed
+143
-19
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync, utimesSync } from "node:fs";
|
||||
import { mkdtempSync, mkdirSync, rmSync, writeFileSync, existsSync, readFileSync, statSync, utimesSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import {
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
syncLegacyEnterprisesDir,
|
||||
syncLegacyGlobalConfig,
|
||||
} from "./loader.js";
|
||||
import { saveAccount, loadAccountStore, resolveAccountCachePath } from "../auth/account-store.js";
|
||||
import type { CorpConfig } from "./types.js";
|
||||
|
||||
describe("core/config/loader", () => {
|
||||
@@ -213,4 +214,30 @@ describe("core/config/loader", () => {
|
||||
expect(JSON.parse(readFileSync(primary, "utf8")).stateful).toBe(false);
|
||||
expect(existsSync(legacy)).toBe(false);
|
||||
});
|
||||
|
||||
test("saveCorpConfig and saveAccount enforce 0o600 file permissions and respect HOSE_ACCOUNTS_FILE", () => {
|
||||
const targetDir = join(tempDir, "secure-corps");
|
||||
const corpFile = saveCorpConfig(createMockCorp("sec-1", "安全测试企业"), targetDir);
|
||||
if (process.platform !== "win32") {
|
||||
expect(statSync(corpFile).mode & 0o777).toBe(0o600);
|
||||
}
|
||||
|
||||
const customAccountsFile = join(tempDir, "secure-accounts", "accounts.json");
|
||||
process.env.HOSE_ACCOUNTS_FILE = customAccountsFile;
|
||||
expect(resolveAccountCachePath()).toBe(customAccountsFile);
|
||||
|
||||
saveAccount({
|
||||
accountId: "86-13800138000",
|
||||
cloudAuthToken: "secret-cloud-token",
|
||||
createdAt: Date.now(),
|
||||
updatedAt: Date.now(),
|
||||
corporations: [],
|
||||
});
|
||||
|
||||
expect(existsSync(customAccountsFile)).toBe(true);
|
||||
if (process.platform !== "win32") {
|
||||
expect(statSync(customAccountsFile).mode & 0o777).toBe(0o600);
|
||||
}
|
||||
expect(loadAccountStore().accounts["86-13800138000"]?.cloudAuthToken).toBe("secret-cloud-token");
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { chmodSync, copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigSchema } from "./types.js";
|
||||
@@ -17,7 +17,18 @@ export const LEGACY_GLOBAL_CONFIG_FILE = join(homedir(), ".cache", "ekb-mcp", "c
|
||||
const LEGACY_CLEANUP_GRACE_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
function ensureDir(dir: string): void {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
mkdirSync(dir, { recursive: true, mode: 0o700 });
|
||||
try {
|
||||
chmodSync(dir, 0o700);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function writeSecureFile(filePath: string, content: string): void {
|
||||
ensureDir(dirname(filePath));
|
||||
writeFileSync(filePath, content, { encoding: "utf-8", mode: 0o600 });
|
||||
try {
|
||||
chmodSync(filePath, 0o600);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
export function resolveEnterprisesDirs(): string[] {
|
||||
@@ -165,8 +176,7 @@ export function saveGlobalConfig(config: Partial<GlobalConfig>): GlobalConfig {
|
||||
|
||||
const configured = process.env[GLOBAL_CONFIG_ENV]?.trim();
|
||||
const targetFile = configured || PRIMARY_GLOBAL_CONFIG_FILE;
|
||||
ensureDir(dirname(targetFile));
|
||||
writeFileSync(targetFile, JSON.stringify(updated, null, 2), "utf-8");
|
||||
writeSecureFile(targetFile, JSON.stringify(updated, null, 2));
|
||||
return updated;
|
||||
}
|
||||
|
||||
@@ -230,7 +240,7 @@ export function saveCorpConfig(config: CorpConfig, dir?: string): string {
|
||||
}
|
||||
const validated = CorpConfigSchema.parse(config);
|
||||
const filePath = join(targetDir, `${validated.id}.json`);
|
||||
writeFileSync(filePath, JSON.stringify(validated, null, 2), "utf-8");
|
||||
writeSecureFile(filePath, JSON.stringify(validated, null, 2));
|
||||
return filePath;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user