Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions handwritten/storage/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,22 @@ export interface CopyCallback {

export type DownloadResponse = [Buffer];

export type DownloadResponseWithStatus = [Buffer] & {
skipped?: boolean;
reason?: SkipReason;
fileName?: string;
localPath?: string;
message?: string;
error?: Error;
};

export enum SkipReason {
PATH_TRAVERSAL = 'PATH_TRAVERSAL',
ILLEGAL_CHARACTER = 'ILLEGAL_CHARACTER',
ALREADY_EXISTS = 'ALREADY_EXISTS',
DOWNLOAD_ERROR = 'DOWNLOAD_ERROR',
}

export type DownloadCallback = (
err: RequestError | null,
contents: Buffer,
Expand Down
112 changes: 84 additions & 28 deletions handwritten/storage/src/transfer-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import {Bucket, UploadOptions, UploadResponse} from './bucket.js';
import {
DownloadOptions,
DownloadResponse,
DownloadResponseWithStatus,
File,
FileExceptionMessages,
RequestError,
SkipReason,
} from './file.js';
import pLimit from 'p-limit';
import * as path from 'path';
Expand Down Expand Up @@ -570,9 +572,13 @@ export class TransferManager {
const limit = pLimit(
options.concurrencyLimit || DEFAULT_PARALLEL_DOWNLOAD_LIMIT,
);
const promises: Promise<DownloadResponse>[] = [];
const promises: Promise<void>[] = [];
let files: File[] = [];

const baseDestination = path.resolve(
options.passthroughOptions?.destination || options.prefix || '.'
);

if (!Array.isArray(filesOrFolder)) {
const directoryFiles = await this.bucket.getFiles({
prefix: filesOrFolder,
Expand All @@ -592,45 +598,95 @@ export class TransferManager {
: EMPTY_REGEX;
const regex = new RegExp(stripRegexString, 'g');

for (const file of files) {
const passThroughOptionsCopy = {
...options.passthroughOptions,
[GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_MANY,
};
const finalResults: DownloadResponseWithStatus[] = new Array(files.length);

if (options.prefix || passThroughOptionsCopy.destination) {
passThroughOptionsCopy.destination = path.join(
options.prefix || '',
passThroughOptionsCopy.destination || '',
file.name,
);
for (let i = 0; i < files.length; i++) {
const file = files[i];

const hasIllegalDrive = /^[a-zA-Z]:/.test(file.name);
if (hasIllegalDrive) {
const skippedResult = [Buffer.alloc(0)] as DownloadResponseWithStatus;
skippedResult.skipped = true;
skippedResult.reason = SkipReason.ILLEGAL_CHARACTER;
skippedResult.fileName = file.name;
finalResults[i] = skippedResult;
continue;
}
if (options.stripPrefix) {
passThroughOptionsCopy.destination = file.name.replace(regex, '');

const normalizedGcsName = file.name
.replace(/\\/g, '/')
.replace(/^\/+/, '');

const dest = options.stripPrefix
? normalizedGcsName.replace(regex, '')
: normalizedGcsName;

const resolvedPath = path.resolve(baseDestination, dest);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dest is an absolute path, path.Resolve will return dest as is. But that is not the behavior we want. If object name is /etc/passwd and if the user given options.passthroughOptions?.destination is /usr/downloads, the final path should be /usr/downloads/etc/passwd. Can you check if this is happening in this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch. We addressed this by 'de-rooting' the object name before resolution. By using .replace(/^\/+/, '') on the GCS object name, we force it to be treated as a relative path.

In your example, /etc/passwd becomes etc/passwd, and path.resolve('/usr/downloads', 'etc/passwd') correctly results in /usr/downloads/etc/passwd rather than escaping to the root.

const relativeFromBase = path.relative(baseDestination, resolvedPath);

const isOutside =
path.isAbsolute(relativeFromBase) ||
relativeFromBase.split(path.sep).includes('..');

if (isOutside) {
const skippedResult = [Buffer.alloc(0)] as DownloadResponseWithStatus;
skippedResult.skipped = true;
skippedResult.reason = SkipReason.PATH_TRAVERSAL;
skippedResult.fileName = file.name;
skippedResult.localPath = resolvedPath;
finalResults[i] = skippedResult;
continue;
}
if (
options.skipIfExists &&
existsSync(passThroughOptionsCopy.destination || '')
) {

if (options.skipIfExists && existsSync(resolvedPath)) {
continue;
}

const passThroughOptionsCopy = {
...options.passthroughOptions,
destination: resolvedPath,
[GCCL_GCS_CMD_KEY]: GCCL_GCS_CMD_FEATURE.DOWNLOAD_MANY,
};

promises.push(
limit(async () => {
const destination = passThroughOptionsCopy.destination;
if (destination && destination.endsWith(path.sep)) {
await fsp.mkdir(destination, {recursive: true});
return Promise.resolve([
Buffer.alloc(0),
]) as Promise<DownloadResponse>;
try {
const destination = passThroughOptionsCopy.destination!;

if (destination.endsWith(path.sep) || destination.endsWith('/')) {
await fsp.mkdir(destination, {recursive: true});
const dirResp = [Buffer.alloc(0)] as DownloadResponseWithStatus;
dirResp.skipped = false;
dirResp.fileName = file.name;
dirResp.localPath = destination;
finalResults[i] = dirResp;
return;
}

await fsp.mkdir(path.dirname(destination), {recursive: true});

const resp = (await file.download(
passThroughOptionsCopy
)) as DownloadResponseWithStatus;
resp.skipped = false;
resp.fileName = file.name;
resp.localPath = destination;
finalResults[i] = resp;
} catch (err) {
const errorResp = [Buffer.alloc(0)] as DownloadResponseWithStatus;
errorResp.skipped = true;
errorResp.reason = SkipReason.DOWNLOAD_ERROR;
errorResp.fileName = file.name;
errorResp.localPath = resolvedPath;
errorResp.error = err as Error;
finalResults[i] = errorResp;
}

return file.download(passThroughOptionsCopy);
}),
})
);
}

return Promise.all(promises);
await Promise.all(promises);
return finalResults;
}

/**
Expand Down
Loading
Loading