Skip to content
Merged
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
31 changes: 0 additions & 31 deletions src/common/s3-upload/upload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,37 +97,6 @@ describe('uploadFiles', () => {
expect(uploadFile).toHaveBeenCalledTimes(1);
});

it('builds an S3 key from the target prefix and a relative file path', async () => {
headObject.mockRejectedValueOnce({name: 'NotFound'});

await uploadFiles(['/build/assets/file.txt'], {
...createConfig(),
options: {
...createConfig().options,
targetPath: 'releases/current',
},
});

expect(headObject).toHaveBeenCalledWith('bucket', 'releases/current/assets/file.txt');
expect(uploadFile).toHaveBeenCalledWith(
'bucket',
'/build/assets/file.txt',
'releases/current/assets/file.txt',
{cacheControl: undefined},
);
});

it.each(['../outside.txt', '/outside.txt'])(
'rejects a file outside of sourcePath: %s',
async (filePath) => {
await expect(uploadFiles([filePath], createConfig())).rejects.toThrow(
`File ${filePath} is outside of source path /build`,
);
expect(headObject).not.toHaveBeenCalled();
expect(uploadFile).not.toHaveBeenCalled();
},
);

it.each([
['file.js.gz', 'gzip'],
['file.js.br', 'br'],
Expand Down
31 changes: 5 additions & 26 deletions src/common/s3-upload/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export async function uploadFiles(files: string[], config: UploadFilesOptions) {

return Promise.all(
files.flatMap((filePath) => {
const relativeFilePath = getRelativeFilePath(config.options.sourcePath, filePath);
const relativeFilePath = path.isAbsolute(filePath)
? path.relative(config.options.sourcePath, filePath)
: filePath;
return processFile(relativeFilePath);
}),
);
Expand Down Expand Up @@ -69,11 +71,8 @@ export async function uploadFiles(files: string[], config: UploadFilesOptions) {

function fileUploader(options: UploadOptions) {
return async (relativeFilePath: string) => {
const sourceFilePath = path.resolve(options.sourcePath, relativeFilePath);
const targetFilePath = path.posix.join(
toPosixPath(options.targetPath ?? ''),
toPosixPath(relativeFilePath),
);
const sourceFilePath = path.join(options.sourcePath, relativeFilePath);
const targetFilePath = path.join(options.targetPath || '', relativeFilePath);

log.verbose(`Uploading file ${relativeFilePath} ...`);
const existsBehavior = options.existsBehavior ?? 'ignore';
Expand Down Expand Up @@ -156,26 +155,6 @@ export async function uploadFiles(files: string[], config: UploadFilesOptions) {

const NOT_COMPRESS = ['png', 'zip', 'gz', 'br'];

function getRelativeFilePath(sourcePath: string, filePath: string) {
const absoluteSourcePath = path.resolve(sourcePath);
const absoluteFilePath = path.resolve(absoluteSourcePath, filePath);
const relativeFilePath = path.relative(absoluteSourcePath, absoluteFilePath);

if (
relativeFilePath === '..' ||
relativeFilePath.startsWith(`..${path.sep}`) ||
path.isAbsolute(relativeFilePath)
) {
throw new Error(`File ${filePath} is outside of source path ${sourcePath}`);
}

return relativeFilePath;
}

function toPosixPath(filePath: string) {
return filePath.split(path.sep).join(path.posix.sep);
}

function getContentEncoding(filePath: string) {
switch (path.extname(filePath).toLowerCase()) {
case '.gz':
Expand Down
Loading