@@ -27,7 +27,7 @@ export class DevServer {
27
27
port : this . port ,
28
28
notify : false ,
29
29
ghostMode : false ,
30
- server : true ,
30
+ server : false ,
31
31
middleware : ( req , res ) => this . _bazelMiddleware ( req , res ) ,
32
32
} ;
33
33
@@ -59,10 +59,21 @@ export class DevServer {
59
59
*/
60
60
private _bazelMiddleware ( req : http . IncomingMessage , res : http . ServerResponse ) {
61
61
if ( ! req . url ) {
62
- res . end ( 'No url specified. Error' ) ;
62
+ res . statusCode = 500 ;
63
+ res . end ( 'Error: No url specified' ) ;
63
64
return ;
64
65
}
65
66
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
+
66
77
// Implements the HTML history API fallback logic based on the requirements of the
67
78
// "connect-history-api-fallback" package. See the conditions for a request being redirected
68
79
// to the index: https://github.com/bripkens/connect-history-api-fallback#introduction
@@ -84,15 +95,19 @@ export class DevServer {
84
95
85
96
/** Resolves a given URL from the runfiles using the corresponding manifest path. */
86
97
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 ) ;
90
98
for ( let rootPath of this . _rootPaths ) {
91
99
try {
92
- return require . resolve ( path . posix . join ( rootPath , manifestPath ) ) ;
100
+ return require . resolve ( path . posix . join ( rootPath , getManifestPath ( url ) ) ) ;
93
101
} catch {
94
102
}
95
103
}
96
104
return null ;
97
105
}
98
106
}
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