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

+28 -1
View File
@@ -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");
});
});