@@ -61,6 +61,11 @@ @interface GCDWebDAVServer () {
6161
6262@implementation GCDWebDAVServer (Methods)
6363
64+ // Must match implementation in GCDWebUploader
65+ - (BOOL )_checkSandboxedPath : (NSString *)path {
66+ return [[path stringByStandardizingPath ] hasPrefix: _uploadDirectory];
67+ }
68+
6469- (BOOL )_checkFileExtension : (NSString *)fileName {
6570 if (_allowedExtensions && ![_allowedExtensions containsObject: [[fileName pathExtension ] lowercaseString ]]) {
6671 return NO ;
@@ -87,7 +92,7 @@ - (GCDWebServerResponse*)performGET:(GCDWebServerRequest*)request {
8792 NSString * relativePath = request.path ;
8893 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
8994 BOOL isDirectory = NO ;
90- if (![absolutePath hasPrefix: _uploadDirectory ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
95+ if (![self _checkSandboxedPath: absolutePath ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
9196 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
9297 }
9398
@@ -116,7 +121,7 @@ - (GCDWebServerResponse*)performPUT:(GCDWebServerFileRequest*)request {
116121
117122 NSString * relativePath = request.path ;
118123 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
119- if (![absolutePath hasPrefix: _uploadDirectory ]) {
124+ if (![self _checkSandboxedPath: absolutePath ]) {
120125 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
121126 }
122127 BOOL isDirectory;
@@ -161,7 +166,7 @@ - (GCDWebServerResponse*)performDELETE:(GCDWebServerRequest*)request {
161166 NSString * relativePath = request.path ;
162167 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
163168 BOOL isDirectory = NO ;
164- if (![absolutePath hasPrefix: _uploadDirectory ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
169+ if (![self _checkSandboxedPath: absolutePath ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
165170 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
166171 }
167172
@@ -194,7 +199,7 @@ - (GCDWebServerResponse*)performMKCOL:(GCDWebServerDataRequest*)request {
194199
195200 NSString * relativePath = request.path ;
196201 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
197- if (![absolutePath hasPrefix: _uploadDirectory ]) {
202+ if (![self _checkSandboxedPath: absolutePath ]) {
198203 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
199204 }
200205 BOOL isDirectory;
@@ -243,7 +248,7 @@ - (GCDWebServerResponse*)performCOPY:(GCDWebServerRequest*)request isMove:(BOOL)
243248
244249 NSString * srcRelativePath = request.path ;
245250 NSString * srcAbsolutePath = [_uploadDirectory stringByAppendingPathComponent: srcRelativePath];
246- if (![srcAbsolutePath hasPrefix: _uploadDirectory ]) {
251+ if (![self _checkSandboxedPath: srcAbsolutePath ]) {
247252 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , srcRelativePath];
248253 }
249254
@@ -254,7 +259,7 @@ - (GCDWebServerResponse*)performCOPY:(GCDWebServerRequest*)request isMove:(BOOL)
254259 }
255260 dstRelativePath = [[dstRelativePath substringFromIndex: (range.location + range.length)] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
256261 NSString * dstAbsolutePath = [_uploadDirectory stringByAppendingPathComponent: dstRelativePath];
257- if (![dstAbsolutePath hasPrefix: _uploadDirectory ]) {
262+ if (![self _checkSandboxedPath: dstAbsolutePath ]) {
258263 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , srcRelativePath];
259264 }
260265
@@ -425,7 +430,7 @@ - (GCDWebServerResponse*)performPROPFIND:(GCDWebServerDataRequest*)request {
425430 NSString * relativePath = request.path ;
426431 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
427432 BOOL isDirectory = NO ;
428- if (![absolutePath hasPrefix: _uploadDirectory ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
433+ if (![self _checkSandboxedPath: absolutePath ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
429434 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
430435 }
431436
@@ -475,7 +480,7 @@ - (GCDWebServerResponse*)performLOCK:(GCDWebServerDataRequest*)request {
475480 NSString * relativePath = request.path ;
476481 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
477482 BOOL isDirectory = NO ;
478- if (![absolutePath hasPrefix: _uploadDirectory ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
483+ if (![self _checkSandboxedPath: absolutePath ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
479484 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
480485 }
481486
@@ -575,7 +580,7 @@ - (GCDWebServerResponse*)performUNLOCK:(GCDWebServerRequest*)request {
575580 NSString * relativePath = request.path ;
576581 NSString * absolutePath = [_uploadDirectory stringByAppendingPathComponent: relativePath];
577582 BOOL isDirectory = NO ;
578- if (![absolutePath hasPrefix: _uploadDirectory ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
583+ if (![self _checkSandboxedPath: absolutePath ] || ![[NSFileManager defaultManager ] fileExistsAtPath: absolutePath isDirectory: &isDirectory]) {
579584 return [GCDWebServerErrorResponse responseWithClientError: kGCDWebServerHTTPStatusCode_NotFound message: @" \" %@ \" does not exist" , relativePath];
580585 }
581586
0 commit comments