fix: use two-pass validation in settings PUT to prevent partial writes

Validation now runs on all entries before any database writes.
Previously, clean entries could be written before a later malicious
entry triggered a 400 response.
This commit is contained in:
Siddharth Kumar Sah
2026-03-28 19:08:17 +08:00
parent 8a62093130
commit 813fa6b7e8
2 changed files with 34 additions and 5 deletions
+24
View File
@@ -1420,6 +1420,30 @@ describe("Settings", () => {
expect(body.code).toBe("VALIDATION_ERROR");
});
it("does not partially write entries when a later entry contains HTML tags", async () => {
const cleanKey = `atomicity_test_clean_${Date.now()}`;
const res = await app.inject({
method: "PUT",
url: "/api/v1/settings",
headers: { authorization: `Bearer ${adminToken}` },
payload: {
[cleanKey]: "safe_value",
"<script>xss</script>": "evil",
},
});
expect(res.statusCode).toBe(400);
const body = JSON.parse(res.body);
expect(body.code).toBe("VALIDATION_ERROR");
// The clean entry must NOT have been written
const getRes = await app.inject({
method: "GET",
url: `/api/v1/settings/${cleanKey}`,
headers: { authorization: `Bearer ${adminToken}` },
});
expect(getRes.statusCode).toBe(404);
});
it("allows normal setting values without HTML", async () => {
const res = await app.inject({
method: "PUT",