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
5 changes: 5 additions & 0 deletions .changeset/soft-vans-ask.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/node': patch
---

Hardens static file handler path resolution to ensure resolved paths stay within the client directory
33 changes: 27 additions & 6 deletions packages/integrations/node/src/serve-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@ import { resolveClientDir } from './shared.js';
import type { NodeAppHeadersJson, Options } from './types.js';
import { createRequest } from 'astro/app/node';

/**
* Resolves a URL path to a filesystem path within the client directory,
* and checks whether it is a directory.
*
* Returns `isDirectory: false` if the resolved path escapes the client root
* (e.g. via `..` path traversal segments).
*/
export function resolveStaticPath(client: string, urlPath: string) {
const filePath = path.join(client, urlPath);
const resolved = path.resolve(filePath);
const resolvedClient = path.resolve(client);

// Prevent path traversal: if the resolved path is outside the client
// directory, treat it as non-existent rather than probing the filesystem.
if (resolved !== resolvedClient && !resolved.startsWith(resolvedClient + path.sep)) {
return { filePath: resolved, isDirectory: false };
}

let isDirectory = false;
try {
isDirectory = fs.lstatSync(filePath).isDirectory();
} catch {}

return { filePath: resolved, isDirectory };
}

/**
* Creates a Node.js http listener for static files and prerendered pages.
* In standalone mode, the static handler is queried first for the static files.
Expand All @@ -32,12 +58,7 @@ export function createStaticHandler(
}

const [urlPath, urlQuery] = fullUrl.split('?');
const filePath = path.join(client, app.removeBase(urlPath));

let isDirectory = false;
try {
isDirectory = fs.lstatSync(filePath).isDirectory();
} catch {}
const { isDirectory } = resolveStaticPath(client, app.removeBase(urlPath));

const hasSlash = urlPath.endsWith('/');
let pathname = urlPath;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { describe, it, before, after } from 'node:test';
import { resolveStaticPath } from '../../dist/serve-static.js';

describe('resolveStaticPath', () => {
let tmpRoot;
let clientDir;

before(() => {
tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'astro-test-'));
clientDir = path.join(tmpRoot, 'client');
fs.mkdirSync(clientDir);
fs.mkdirSync(path.join(clientDir, 'assets'));
fs.writeFileSync(path.join(clientDir, 'index.html'), '<h1>hello</h1>');
fs.mkdirSync(path.join(tmpRoot, 'secret'));
});

after(() => {
fs.rmSync(tmpRoot, { recursive: true, force: true });
});

it('detects a subdirectory within client root', () => {
const result = resolveStaticPath(clientDir, '/assets');
assert.equal(result.isDirectory, true);
});

it('returns false for a non-existent path', () => {
const result = resolveStaticPath(clientDir, '/nope');
assert.equal(result.isDirectory, false);
});

it('returns false for a sibling directory via ../', () => {
const result = resolveStaticPath(clientDir, '/../secret');
assert.equal(result.isDirectory, false);
});

it('returns false for the parent directory', () => {
const result = resolveStaticPath(clientDir, '/..');
assert.equal(result.isDirectory, false);
});

it('returns false for deep .. traversal', () => {
const result = resolveStaticPath(clientDir, '/../../../../../../../usr');
assert.equal(result.isDirectory, false);
});

it('returns false for .. traversal with trailing slash', () => {
const result = resolveStaticPath(clientDir, '/../secret/');
assert.equal(result.isDirectory, false);
});

it('detects the client root itself', () => {
const result = resolveStaticPath(clientDir, '/');
assert.equal(result.isDirectory, true);
});
});