Skip to content
Open
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
19 changes: 18 additions & 1 deletion src/WebServer/File/StaticFileRouteHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public async Task<HttpServerResponse> HandleRequest(IHttpServerRequest request)
var localFilePath = GetFilePath(request.Uri);
var absoluteFilePath = GetAbsoluteFilePath(localFilePath);

// Check to make sure the requested path isn't outside the base path.
if (!absoluteFilePath.ToLower().StartsWith(_basePath.ToLower()))
{
return GetBadRequestResponse("Navigation outside of allowed path.");
}


// todo: add validation for invalid path characters / invalid filename characters
IFile item;
try
Expand All @@ -65,6 +72,16 @@ private static HttpServerResponse GetFileNotFoundResponse(string localPath)
return notFoundResponse;
}

private static HttpServerResponse GetBadRequestResponse(string message)
{
var response = HttpServerResponse.Create(new Version(1, 1), HttpResponseStatus.BadRequest);
response.Content = Encoding.UTF8.GetBytes(message);
response.ContentType = "text/plain";
response.ContentCharset = "utf-8";

return response;
}

private static HttpServerResponse GetMethodNotAllowedResponse(HttpMethod? method)
{
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
Expand Down Expand Up @@ -110,7 +127,7 @@ private string GetAbsoluteFilePath(string localFilePath)
{
var absoluteFilePath = Path.Combine(_basePath, localFilePath);

return absoluteFilePath;
return Path.GetFullPath(absoluteFilePath);
}

private static string GetLocalPath(string uriString)
Expand Down