Skip to content

Commit 99f22c7

Browse files
committed
build(dev-app): prevent directory traversal
1 parent fd1593d commit 99f22c7

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

tools/dev-server/dev-server.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export class DevServer {
2727
port: this.port,
2828
notify: false,
2929
ghostMode: false,
30-
server: true,
30+
server: false,
3131
middleware: (req, res) => this._bazelMiddleware(req, res),
3232
};
3333

@@ -59,10 +59,21 @@ export class DevServer {
5959
*/
6060
private _bazelMiddleware(req: http.IncomingMessage, res: http.ServerResponse) {
6161
if (!req.url) {
62-
res.end('No url specified. Error');
62+
res.statusCode = 500;
63+
res.end('Error: No url specified');
6364
return;
6465
}
6566

67+
// Detect if the url escapes the server's root path
68+
for (const rootPath of this._rootPaths) {
69+
const absoluteRootPath = path.resolve(rootPath);
70+
const absoluteJoinedPath = path.resolve(path.posix.join(rootPath, getManifestPath(req.url)));
71+
if (!absoluteJoinedPath.startsWith(absoluteRootPath)) {
72+
res.statusCode = 500;
73+
res.end('Error: Detected directory traversal');
74+
}
75+
}
76+
6677
// Implements the HTML history API fallback logic based on the requirements of the
6778
// "connect-history-api-fallback" package. See the conditions for a request being redirected
6879
// to the index: https://github.com/bripkens/connect-history-api-fallback#introduction
@@ -84,15 +95,19 @@ export class DevServer {
8495

8596
/** Resolves a given URL from the runfiles using the corresponding manifest path. */
8697
private _resolveUrlFromRunfiles(url: string): string|null {
87-
// Remove the leading slash from the URL. Manifest paths never
88-
// start with a leading slash.
89-
const manifestPath = url.substring(1);
9098
for (let rootPath of this._rootPaths) {
9199
try {
92-
return require.resolve(path.posix.join(rootPath, manifestPath));
100+
return require.resolve(path.posix.join(rootPath, getManifestPath(url)));
93101
} catch {
94102
}
95103
}
96104
return null;
97105
}
98106
}
107+
108+
/** Gets the manifest path for a given url */
109+
function getManifestPath(url: string) {
110+
// Remove the leading slash from the URL. Manifest paths never
111+
// start with a leading slash.
112+
return url.substring(1);
113+
}

0 commit comments

Comments
 (0)