amplication icon indicating copy to clipboard operation
amplication copied to clipboard

Arbitrary File Read and Directory Traversal in Storage Gateway

Open codeCraft-Ritik opened this issue 2 months ago • 0 comments

  • 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)

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

  1. The HTTP endpoint GET /storage/:resourceId/:buildId/content?path=... allows authenticated users to view build output files.
  2. CodeAccessGuard verifies that the caller owns or can access buildId.
  3. Once authentication succeeds, StorageService.fileContent concatenates this.buildsFolder, resourceId, buildId, and the user-supplied path using path.join.
  4. In Node.js, path.join resolves ../ dot-dot sequences without enforcing root directory boundaries.
  5. If a user supplies path=../../../../etc/passwd or path=../../../../.env, filePath escapes the artifact storage folder and resolves to arbitrary files on the host file system.
  6. readFileSync(filePath).toString() synchronously reads the target file and serves it directly in the HTTP body with Content-Type: text/plain.
  7. Likewise, getBuildFilesList uses join on relativePath and passes it as cwd to glob.sync, allowing callers to enumerate directory structures anywhere on the filesystem that the container process can access.
  8. If a non-existent file or directory is passed to fileContent, readFileSync throws an unhandled ENOENT / EISDIR exception, 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 resourceId and buildId to 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-gateway or fileContent.
  • 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");
   }

codeCraft-Ritik avatar Oct 02 '26 03:10 codeCraft-Ritik