fix(storage): constrain recursive blob deletion to resource folders
This commit is contained in:
+33
-4
@@ -525,6 +525,31 @@ export const deleteBlob = async (containerName, blobName) => {
|
||||
return { "deleted": blobName };
|
||||
};
|
||||
|
||||
const isSafeDeletionPathSegment = (value) =>
|
||||
typeof value === "string" &&
|
||||
value.length > 0 &&
|
||||
value.length <= 255 &&
|
||||
value === value.trim() &&
|
||||
value !== "." &&
|
||||
value !== ".." &&
|
||||
!/[\\/\u0000-\u001f\u007f]/.test(value);
|
||||
|
||||
const buildDeletionFolderPrefix = (resourcePath, expectedSegmentCount) => {
|
||||
if (typeof resourcePath !== "string") {
|
||||
throw new Error("Invalid deletion resource path");
|
||||
}
|
||||
|
||||
const segments = resourcePath.split("/");
|
||||
if (
|
||||
segments.length !== expectedSegmentCount ||
|
||||
!segments.every(isSafeDeletionPathSegment)
|
||||
) {
|
||||
throw new Error("Invalid deletion resource path");
|
||||
}
|
||||
|
||||
return segments.join("/") + "/";
|
||||
};
|
||||
|
||||
export const deleteBlobCase = async (containerName, blobName) => {
|
||||
const creds = new DefaultAzureCredential();
|
||||
|
||||
@@ -538,10 +563,12 @@ export const deleteBlobCase = async (containerName, blobName) => {
|
||||
|
||||
//conLog"deleteBlobCase ");
|
||||
|
||||
console.log("blob to delete:", blobName);
|
||||
const folderPrefix = buildDeletionFolderPrefix(blobName, 1);
|
||||
|
||||
console.log("blob to delete:", folderPrefix);
|
||||
|
||||
for await (const blob of containerClient.listBlobsFlat({
|
||||
prefix: blobName
|
||||
prefix: folderPrefix
|
||||
})) {
|
||||
console.log(" ------ :", blob.name);
|
||||
containerClient.deleteBlob(blob.name);
|
||||
@@ -566,10 +593,12 @@ export const deleteBlobRep = async (containerName, blobName) => {
|
||||
const sasUrl = `${STORAGE_PATH}/${containerName}?${containerToken}`;
|
||||
const containerClient = new ContainerClient(sasUrl);
|
||||
|
||||
console.log("blob to delete:", blobName);
|
||||
const folderPrefix = buildDeletionFolderPrefix(blobName, 2);
|
||||
|
||||
console.log("blob to delete:", folderPrefix);
|
||||
|
||||
for await (const blob of containerClient.listBlobsFlat({
|
||||
prefix: blobName
|
||||
prefix: folderPrefix
|
||||
})) {
|
||||
console.log("------ :", blob.name);
|
||||
const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
|
||||
|
||||
Reference in New Issue
Block a user