Skip to content

Commit e2ee0c4

Browse files
Merge commit from fork
* fix(asset-server-plugin): Fix path traversal vulnerability Relates to GHSA-r9mq-3c9r-fmjq * fix(asset-server-plugin): Fix crash caused by malformed URI Relates to GHSA-r9mq-3c9r-fmjq
1 parent a578b53 commit e2ee0c4

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

packages/asset-server-plugin/e2e/asset-server-plugin.e2e-spec.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2-
import { mergeConfig } from '@vendure/core';
2+
import { ConfigService, mergeConfig } from '@vendure/core';
33
import { AssetFragment } from '@vendure/core/e2e/graphql/generated-e2e-admin-types';
44
import { createTestEnvironment } from '@vendure/testing';
5+
import { exec } from 'child_process';
56
import fs from 'fs-extra';
67
import gql from 'graphql-tag';
78
import fetch from 'node-fetch';
@@ -193,6 +194,41 @@ describe('AssetServerPlugin', () => {
193194
it('does not error on non-integer height', async () => {
194195
return fetch(`${asset.preview}?h=10.5`);
195196
});
197+
198+
// https://github.com/vendure-ecommerce/vendure/security/advisories/GHSA-r9mq-3c9r-fmjq
199+
describe('path traversal', () => {
200+
function curlWithPathAsIs(url: string) {
201+
return new Promise<string>((resolve, reject) => {
202+
// We use curl here rather than node-fetch or any other fetch-type function because
203+
// those will automatically perform path normalization which will mask the path traversal
204+
return exec(`curl --path-as-is ${url}`, (err, stdout, stderr) => {
205+
if (err) {
206+
reject(err);
207+
}
208+
resolve(stdout);
209+
});
210+
});
211+
}
212+
213+
function testPathTraversalOnUrl(urlPath: string) {
214+
return async () => {
215+
const port = server.app.get(ConfigService).apiOptions.port;
216+
const result = await curlWithPathAsIs(`http://localhost:${port}/assets${urlPath}`);
217+
expect(result).not.toContain('@vendure/asset-server-plugin');
218+
expect(result.toLowerCase()).toContain('resource not found');
219+
};
220+
}
221+
222+
it('blocks path traversal 1', testPathTraversalOnUrl(`/../../package.json`));
223+
it('blocks path traversal 2', testPathTraversalOnUrl(`/foo/../../../package.json`));
224+
it('blocks path traversal 3', testPathTraversalOnUrl(`/foo/../../../foo/../package.json`));
225+
it('blocks path traversal 4', testPathTraversalOnUrl(`/%2F..%2F..%2Fpackage.json`));
226+
it('blocks path traversal 5', testPathTraversalOnUrl(`/%2E%2E/%2E%2E/package.json`));
227+
it('blocks path traversal 6', testPathTraversalOnUrl(`/..//..//package.json`));
228+
it('blocks path traversal 7', testPathTraversalOnUrl(`/.%2F.%2F.%2Fpackage.json`));
229+
it('blocks path traversal 8', testPathTraversalOnUrl(`/..\\\\..\\\\package.json`));
230+
it('blocks path traversal 9', testPathTraversalOnUrl(`/\\\\\\..\\\\\\..\\\\\\package.json`));
231+
});
196232
});
197233

198234
describe('deletion', () => {
@@ -268,7 +304,7 @@ describe('AssetServerPlugin', () => {
268304
// https://github.com/vendure-ecommerce/vendure/issues/1563
269305
it('falls back to binary preview if image file cannot be processed', async () => {
270306
const filesToUpload = [path.join(__dirname, 'fixtures/assets/bad-image.jpg')];
271-
const { createAssets }: CreateAssets.Mutation = await adminClient.fileUploadMutation({
307+
const { createAssets }: CreateAssetsMutation = await adminClient.fileUploadMutation({
272308
mutation: CREATE_ASSETS,
273309
filePaths: filesToUpload,
274310
mapVariables: filePaths => ({

packages/asset-server-plugin/src/plugin.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
281281
return async (err: any, req: Request, res: Response, next: NextFunction) => {
282282
if (err && (err.status === 404 || err.statusCode === 404)) {
283283
if (req.query) {
284-
const decodedReqPath = decodeURIComponent(req.path);
284+
const decodedReqPath = this.sanitizeFilePath(req.path);
285285
Logger.debug(`Pre-cached Asset not found: ${decodedReqPath}`, loggerCtx);
286286
let file: Buffer;
287287
try {
@@ -347,9 +347,7 @@ export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
347347
imageParamsString += quality;
348348
}
349349

350-
/* eslint-enable @typescript-eslint/restrict-template-expressions */
351-
352-
const decodedReqPath = decodeURIComponent(req.path);
350+
const decodedReqPath = this.sanitizeFilePath(req.path);
353351
if (imageParamsString !== '') {
354352
const imageParamHash = this.md5(imageParamsString);
355353
return path.join(this.cacheDir, this.addSuffix(decodedReqPath, imageParamHash, imageFormat));
@@ -358,6 +356,20 @@ export class AssetServerPlugin implements NestModule, OnApplicationBootstrap {
358356
}
359357
}
360358

359+
/**
360+
* Sanitize the file path to prevent directory traversal attacks.
361+
*/
362+
private sanitizeFilePath(filePath: string): string {
363+
let decodedPath: string;
364+
try {
365+
decodedPath = decodeURIComponent(filePath);
366+
} catch (e: any) {
367+
Logger.error((e.message as string) + ': ' + filePath, loggerCtx);
368+
return '';
369+
}
370+
return path.normalize(decodedPath).replace(/(\.\.[\/\\])+/, '');
371+
}
372+
361373
private md5(input: string): string {
362374
return createHash('md5').update(input).digest('hex');
363375
}

0 commit comments

Comments
 (0)