fix(storage): constrain recursive blob deletion to resource folders
This commit is contained in:
@@ -33,6 +33,65 @@ const loadAzureStorageHelperModule = (injected = {}) => {
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const loadRecursiveDeletionHelpers = ({ blobNames = [] } = {}) => {
|
||||
const filePath = path.join(rootDir, "actions", "azurestorage.js");
|
||||
const source = fs.readFileSync(filePath, "utf8");
|
||||
const start = source.indexOf("const isSafeDeletionPathSegment =");
|
||||
const end = source.indexOf("export const uploadFile =");
|
||||
|
||||
if (start < 0 || end < 0 || end <= start) {
|
||||
throw new Error("Unable to isolate recursive deletion helper block");
|
||||
}
|
||||
|
||||
const listedPrefixes = [];
|
||||
const deletedBlobNames = [];
|
||||
const containerClient = {
|
||||
async *listBlobsFlat({ prefix }) {
|
||||
listedPrefixes.push(prefix);
|
||||
for (const name of blobNames.filter((blobName) =>
|
||||
blobName.startsWith(prefix)
|
||||
)) {
|
||||
yield { name };
|
||||
}
|
||||
},
|
||||
deleteBlob(name) {
|
||||
deletedBlobNames.push(name);
|
||||
return Promise.resolve();
|
||||
},
|
||||
getBlockBlobClient() {
|
||||
return { exists: async () => true };
|
||||
}
|
||||
};
|
||||
|
||||
let helperSource = source
|
||||
.slice(start, end)
|
||||
.replace(/export const /g, "const ");
|
||||
helperSource +=
|
||||
"\nmodule.exports = { deleteBlobCase, deleteBlobRep };\n";
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
console: { log: () => {} },
|
||||
DefaultAzureCredential: class {},
|
||||
createContainerSas: async () => "sas-token",
|
||||
STORAGE_PATH: "https://storage.example.test",
|
||||
ContainerClient: class {
|
||||
constructor() {
|
||||
return containerClient;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
vm.runInNewContext(helperSource, context, { filename: filePath });
|
||||
|
||||
return {
|
||||
...context.module.exports,
|
||||
listedPrefixes,
|
||||
deletedBlobNames
|
||||
};
|
||||
};
|
||||
|
||||
const tests = [];
|
||||
const test = (name, fn) => tests.push({ name, fn });
|
||||
|
||||
@@ -103,6 +162,63 @@ test("azurestorage helper builds hash metadata map with stable keys", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("case deletion uses a complete folder prefix and excludes lexical siblings", async () => {
|
||||
const loaded = loadRecursiveDeletionHelpers({
|
||||
blobNames: [
|
||||
"TMP-ABC/TMP-ABC_appeal.json",
|
||||
"TMP-ABC/files/evidence.pdf",
|
||||
"TMP-ABC1/TMP-ABC1_appeal.json",
|
||||
"TMP-ABCD/files/sibling.pdf"
|
||||
]
|
||||
});
|
||||
|
||||
await loaded.deleteBlobCase("container-1", "TMP-ABC");
|
||||
|
||||
assert.deepStrictEqual(loaded.listedPrefixes, ["TMP-ABC/"]);
|
||||
assert.deepStrictEqual(loaded.deletedBlobNames, [
|
||||
"TMP-ABC/TMP-ABC_appeal.json",
|
||||
"TMP-ABC/files/evidence.pdf"
|
||||
]);
|
||||
});
|
||||
|
||||
test("representation deletion uses a complete folder prefix and preserves parent and siblings", async () => {
|
||||
const loaded = loadRecursiveDeletionHelpers({
|
||||
blobNames: [
|
||||
"CASE-1/CASE-1_case.json",
|
||||
"CASE-1/REP-1/REP-1_rep.json",
|
||||
"CASE-1/REP-1/files/evidence.pdf",
|
||||
"CASE-1/REP-10/REP-10_rep.json",
|
||||
"CASE-1/REP-2/REP-2_rep.json"
|
||||
]
|
||||
});
|
||||
|
||||
await loaded.deleteBlobRep("container-1", "CASE-1/REP-1");
|
||||
|
||||
assert.deepStrictEqual(loaded.listedPrefixes, ["CASE-1/REP-1/"]);
|
||||
assert.deepStrictEqual(loaded.deletedBlobNames, [
|
||||
"CASE-1/REP-1/REP-1_rep.json",
|
||||
"CASE-1/REP-1/files/evidence.pdf"
|
||||
]);
|
||||
});
|
||||
|
||||
test("recursive deletion helpers reject empty and unsafe resource paths before listing", async () => {
|
||||
for (const [helperName, unsafePath] of [
|
||||
["deleteBlobCase", ""],
|
||||
["deleteBlobCase", "CASE-1/REP-1"],
|
||||
["deleteBlobRep", "CASE-1"],
|
||||
["deleteBlobRep", "CASE-1/REP-1/FILES"],
|
||||
["deleteBlobRep", "CASE-1/../REP-1"]
|
||||
]) {
|
||||
const loaded = loadRecursiveDeletionHelpers();
|
||||
|
||||
await assert.rejects(
|
||||
() => loaded[helperName]("container-1", unsafePath),
|
||||
/Invalid deletion resource path/
|
||||
);
|
||||
assert.deepStrictEqual(loaded.listedPrefixes, []);
|
||||
}
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
for (const currentTest of tests) {
|
||||
|
||||
Reference in New Issue
Block a user