amplication
amplication copied to clipboard
Arbitrary File Read and Directory Traversal in Storage Gateway
- Severity: 🟠High (CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:H/I:N/A:N — Base Score: 6.5 / 7.5 depending on deployment)
- CWE: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
- Vulnerable Location:
- File:
packages/amplication-storage-gateway/src/storage/storage.service.ts(lines 29–48, 74–77) - Controller:
packages/amplication-storage-gateway/src/storage/storage.controller.ts(lines 38–47)
- File:
Vulnerable Code
In packages/amplication-storage-gateway/src/storage/storage.service.ts:
fileContent(resourceId: string, buildId: string, path = ""): string {
const filePath = join(this.buildsFolder, resourceId, buildId, path);
return readFileSync(filePath).toString();
}
And in getBuildFilesList (lines 29–40):
getBuildFilesList(resourceId: string, buildId: string, relativePath = "") {
const results: FilesDictionary = {};
const cwd = join(this.buildsFolder, resourceId, buildId, relativePath);
console.log(`Current working directory is ${cwd}`);
const files = sync(`*`, {
nodir: true,
dot: true,
cwd,
});
...
Root Cause Analysis
- The HTTP endpoint
GET /storage/:resourceId/:buildId/content?path=...allows authenticated users to view build output files. CodeAccessGuardverifies that the caller owns or can accessbuildId.- Once authentication succeeds,
StorageService.fileContentconcatenatesthis.buildsFolder,resourceId,buildId, and the user-suppliedpathusingpath.join. - In Node.js,
path.joinresolves../dot-dot sequences without enforcing root directory boundaries. - If a user supplies
path=../../../../etc/passwdorpath=../../../../.env,filePathescapes the artifact storage folder and resolves to arbitrary files on the host file system. readFileSync(filePath).toString()synchronously reads the target file and serves it directly in the HTTP body withContent-Type: text/plain.- Likewise,
getBuildFilesListusesjoinonrelativePathand passes it ascwdtoglob.sync, allowing callers to enumerate directory structures anywhere on the filesystem that the container process can access. - If a non-existent file or directory is passed to
fileContent,readFileSyncthrows an unhandledENOENT/EISDIRexception, crashing request processing with an unhandled 500 server error.
Impact & Exploitation Scenario
- Arbitrary File Disclosure: An attacker with a standard Amplication account can create a project, trigger a build, and use their valid
resourceIdandbuildIdto read sensitive files from the container/host machine:GET /storage/<resourceId>/<buildId>/content?path=../../../../etc/passwd GET /storage/<resourceId>/<buildId>/content?path=../../../../var/run/secrets/kubernetes.io/serviceaccount/token GET /storage/<resourceId>/<buildId>/content?path=../../../../app/.env - Exposes environment variables, database credentials, JWT secrets, service account tokens, and application source code.
GitHub Search Verification
- Checked GitHub Issues: No reported issues matching path traversal in
amplication-storage-gatewayorfileContent. - Checked GitHub PRs: No pending or closed PRs implementing path containment checks on
StorageService.
Recommended Remediation
Enforce absolute path containment using path.resolve and verify that the target path remains inside the build's dedicated root directory before accessing the filesystem. Throw NestJS standard ForbiddenException and NotFoundException:
--- a/packages/amplication-storage-gateway/src/storage/storage.service.ts
+++ b/packages/amplication-storage-gateway/src/storage/storage.service.ts
@@ -1,9 +1,10 @@
-import { Injectable } from "@nestjs/common";
+import { Injectable, ForbiddenException, NotFoundException } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
-import { readFileSync } from "fs";
+import { readFileSync, existsSync, statSync } from "fs";
import { utimes, open } from "fs/promises";
import { sync } from "glob";
-import { join } from "path";
+import { join, resolve, sep } from "path";
import { BUILD_ARTIFACTS_BASE_FOLDER } from "../constants";
import { FileMeta } from "./dto/FileMeta";
import { NodeTypeEnum } from "./dto/NodeTypeEnum";
@@ -29,8 +30,13 @@ export class StorageService {
getBuildFilesList(resourceId: string, buildId: string, relativePath = "") {
const results: FilesDictionary = {};
- const cwd = join(this.buildsFolder, resourceId, buildId, relativePath);
+ const rootDir = resolve(this.buildsFolder, resourceId, buildId);
+ const cwd = resolve(rootDir, relativePath);
+ if (cwd !== rootDir && !cwd.startsWith(rootDir + sep)) {
+ throw new ForbiddenException("Invalid path traversal detected");
+ }
console.log(`Current working directory is ${cwd}`);
@@ -74,7 +80,15 @@ export class StorageService {
fileContent(resourceId: string, buildId: string, path = ""): string {
- const filePath = join(this.buildsFolder, resourceId, buildId, path);
- return readFileSync(filePath).toString();
+ const rootDir = resolve(this.buildsFolder, resourceId, buildId);
+ const filePath = resolve(rootDir, path);
+ if (filePath !== rootDir && !filePath.startsWith(rootDir + sep)) {
+ throw new ForbiddenException("Invalid path traversal detected");
+ }
+ if (!existsSync(filePath) || statSync(filePath).isDirectory()) {
+ throw new NotFoundException("Requested file does not exist");
+ }
+ return readFileSync(filePath, "utf-8");
}