mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-12 12:39:52 +08:00
fix: validate admin settings file target (#894)
This commit is contained in:
@@ -125,7 +125,12 @@ let writableCache: boolean | null = null
|
|||||||
export function isSettingsWritable(): boolean {
|
export function isSettingsWritable(): boolean {
|
||||||
if (writableCache !== null) return writableCache
|
if (writableCache !== null) return writableCache
|
||||||
try {
|
try {
|
||||||
const dir = path.dirname(getSettingsPath())
|
const filePath = getSettingsPath()
|
||||||
|
if (fs.existsSync(filePath) && !fs.statSync(filePath).isFile()) {
|
||||||
|
writableCache = false
|
||||||
|
return writableCache
|
||||||
|
}
|
||||||
|
const dir = path.dirname(filePath)
|
||||||
fs.mkdirSync(dir, { recursive: true })
|
fs.mkdirSync(dir, { recursive: true })
|
||||||
fs.accessSync(dir, fs.constants.W_OK)
|
fs.accessSync(dir, fs.constants.W_OK)
|
||||||
writableCache = true
|
writableCache = true
|
||||||
|
|||||||
@@ -154,9 +154,9 @@ describe("isSettingsWritable", () => {
|
|||||||
expect(isSettingsWritable()).toBe(true)
|
expect(isSettingsWritable()).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("returns false for an unwritable path", () => {
|
it("returns false when the settings path is a directory", () => {
|
||||||
_resetForTests()
|
_resetForTests()
|
||||||
process.env.SETTINGS_FILE = "/nonexistent-root-dir/settings.json"
|
process.env.SETTINGS_FILE = tmpDir
|
||||||
expect(isSettingsWritable()).toBe(false)
|
expect(isSettingsWritable()).toBe(false)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -170,7 +170,9 @@ describe("settings file on disk", () => {
|
|||||||
version: 1,
|
version: 1,
|
||||||
values: { TEST_ADMIN_VAR: "secret" },
|
values: { TEST_ADMIN_VAR: "secret" },
|
||||||
})
|
})
|
||||||
const mode = fs.statSync(filePath).mode & 0o777
|
if (process.platform !== "win32") {
|
||||||
expect(mode).toBe(0o600)
|
const mode = fs.statSync(filePath).mode & 0o777
|
||||||
|
expect(mode).toBe(0o600)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user