Skip to content
Open
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
2 changes: 2 additions & 0 deletions src/Components/WebView/WebView/src/StaticContentProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public bool TryGetResponseContent(string requestUri, bool allowFallbackOnHostPag
{
var relativePath = _appBaseUri.MakeRelativeUri(fileUri).ToString();

relativePath = Uri.UnescapeDataString(relativePath);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the relative path intentionally contains encoded parts, ie. %2F, this is going to decode that to a / and the file won't be found/served?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the question, @ladeak !

If the file with %2F is in the file provider, for example, "folder%2Ffile.txt", then I expect the requestUri in the method TryGetResponseContent to be like this: "https://test.domain/folder%252Ffile.txt".
In this case, the relative path before unescaping is "folder%252Ffile.txt", and after unescaping it becomes "folder%2Ffile.txt".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right place to do this. If we are going to do something like this, we should to it to the entire URL and do it before the _appBaseUri.IsBaseOf call. I think the same concerns that we have in Kestrel apply here with regards to certain charcters like /


// Content in the file provider takes first priority
// Next we may fall back on supplying the host page to support deep linking
// If there's no match, fall back on serving embedded framework content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ public void TryGetResponseContentReturnsCorrectContentTypeForNonPhysicalFile()
Assert.Equal("text/css", contentTypeValue);
}

[Fact]
public void TryGetResponseContentCanHandleWhitespaceInFileName()
{
// Arrange
const string cssFilePath = "file with whitespace.css";
const string cssFileContent = "this is css";
var inMemoryFileProvider = new InMemoryFileProvider(
new Dictionary<string, string>
{
{ cssFilePath, cssFileContent },
});
var appBase = "fake://0.0.0.0/";
var scp = new StaticContentProvider(inMemoryFileProvider, new Uri(appBase), "fakehost.html");

// Act
Assert.True(scp.TryGetResponseContent(
requestUri: appBase + Uri.EscapeDataString(cssFilePath),
allowFallbackOnHostPage: false,
out var statusCode,
out var statusMessage,
out var content,
out var headers));

// Assert
var contentString = new StreamReader(content).ReadToEnd();
Assert.Equal(200, statusCode);
Assert.Equal("OK", statusMessage);
Assert.Equal("this is css", contentString);
Assert.True(headers.TryGetValue("Content-Type", out var contentTypeValue));
Assert.Equal("text/css", contentTypeValue);
}

private sealed class InMemoryFileProvider : IFileProvider
{
public InMemoryFileProvider(IDictionary<string, string> filePathsAndContents)
Expand Down
Loading