11import type { Readable } from 'node:stream'
2- import type { BlobServiceClient as BlobServiceClientType } from '@azure/storage-blob'
2+ import type {
3+ BlobServiceClient as BlobServiceClientType ,
4+ ContainerClient ,
5+ } from '@azure/storage-blob'
36import { createLogger } from '@sim/logger'
47import { generateId } from '@sim/utils/id'
58import {
@@ -20,7 +23,7 @@ import type {
2023 MultipartCompletionPolicy ,
2124 StoredObjectInfo ,
2225} from '@/lib/uploads/shared/types'
23- import { sanitizeStorageMetadata } from '@/lib/uploads/utils/file'
26+ import { hasDotSegment , sanitizeStorageMetadata } from '@/lib/uploads/utils/file'
2427import { sanitizeFileName } from '@/executor/constants'
2528
2629const logger = createLogger ( 'BlobClient' )
@@ -45,6 +48,18 @@ function isBlobCreateConflict(error: unknown): boolean {
4548 )
4649}
4750
51+ /**
52+ * The SDK writes the blob name into the URL path, whose parser collapses `.`/`..` segments, so
53+ * `og-images/../workspace/x` would address `workspace/x` (or a sibling container). Every blob
54+ * client is built here so no such key reaches Azure.
55+ */
56+ function blockBlobClientIn ( containerClient : ContainerClient , key : string ) {
57+ if ( hasDotSegment ( key ) ) {
58+ throw new Error ( 'Path traversal detected in blob key' )
59+ }
60+ return containerClient . getBlockBlobClient ( key )
61+ }
62+
4863/**
4964 * Extract account name and key from an Azure connection string.
5065 * Connection strings have the format: DefaultEndpointsProtocol=https;AccountName=...;AccountKey=...;EndpointSuffix=...
@@ -157,7 +172,7 @@ export async function uploadToBlob(
157172
158173 const blobServiceClient = await getBlobServiceClient ( )
159174 const containerClient = blobServiceClient . getContainerClient ( config . containerName )
160- const blockBlobClient = containerClient . getBlockBlobClient ( uniqueKey )
175+ const blockBlobClient = blockBlobClientIn ( containerClient , uniqueKey )
161176
162177 const blobMetadata : Record < string , string > = {
163178 originalName : encodeURIComponent ( fileName ) , // Encode filename to prevent invalid characters in HTTP headers
@@ -235,7 +250,7 @@ export async function getPresignedUrlWithConfig(
235250 }
236251
237252 const containerClient = tempBlobServiceClient . getContainerClient ( customConfig . containerName )
238- const blockBlobClient = containerClient . getBlockBlobClient ( key )
253+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
239254
240255 const sasOptions = {
241256 containerName : customConfig . containerName ,
@@ -369,7 +384,7 @@ export async function downloadFromBlob(
369384 }
370385
371386 const containerClient = blobServiceClient . getContainerClient ( containerName )
372- const blockBlobClient = containerClient . getBlockBlobClient ( key )
387+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
373388
374389 const downloadBlockBlobResponse = await blockBlobClient . download ( 0 , undefined , {
375390 abortSignal : signal ,
@@ -440,7 +455,7 @@ export async function downloadFromBlobStream(
440455 }
441456
442457 const containerClient = blobServiceClient . getContainerClient ( containerName )
443- const blockBlobClient = containerClient . getBlockBlobClient ( key )
458+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
444459
445460 const downloadBlockBlobResponse = range
446461 ? await blockBlobClient . download ( range . start , range . end - range . start + 1 )
@@ -485,7 +500,7 @@ export async function headBlobObject(
485500 }
486501
487502 const containerClient = blobServiceClient . getContainerClient ( containerName )
488- const blockBlobClient = containerClient . getBlockBlobClient ( key )
503+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
489504
490505 try {
491506 const properties = await blockBlobClient . getProperties ( )
@@ -568,7 +583,7 @@ export async function deleteFromBlob(
568583 }
569584
570585 const containerClient = blobServiceClient . getContainerClient ( containerName )
571- const blockBlobClient = containerClient . getBlockBlobClient ( key )
586+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
572587
573588 signal ?. throwIfAborted ( )
574589 await blockBlobClient . deleteIfExists ( ...( signal ? [ { abortSignal : signal } ] : [ ] ) )
@@ -667,7 +682,7 @@ export async function getMultipartPartUrls(
667682 }
668683
669684 const containerClient = blobServiceClient . getContainerClient ( containerName )
670- const blockBlobClient = containerClient . getBlockBlobClient ( key )
685+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
671686
672687 return partNumbers . map ( ( partNumber ) => {
673688 const blockId = deriveBlobBlockId ( partNumber )
@@ -736,7 +751,7 @@ async function getBlockBlobClientFor(key: string, customConfig?: BlobConfig) {
736751 containerName = BLOB_CONFIG . containerName
737752 }
738753
739- return blobServiceClient . getContainerClient ( containerName ) . getBlockBlobClient ( key )
754+ return blockBlobClientIn ( blobServiceClient . getContainerClient ( containerName ) , key )
740755}
741756
742757/**
@@ -828,7 +843,7 @@ export async function completeMultipartUpload(
828843 }
829844
830845 const containerClient = blobServiceClient . getContainerClient ( containerName )
831- const blockBlobClient = containerClient . getBlockBlobClient ( key )
846+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
832847
833848 const sortedBlockIds = parts
834849 . sort ( ( a , b ) => a . partNumber - b . partNumber )
@@ -922,7 +937,7 @@ export async function abortMultipartUpload(
922937 }
923938
924939 const containerClient = blobServiceClient . getContainerClient ( containerName )
925- const blockBlobClient = containerClient . getBlockBlobClient ( key )
940+ const blockBlobClient = blockBlobClientIn ( containerClient , key )
926941
927942 try {
928943 const properties = await blockBlobClient . getProperties ( )
0 commit comments