|
1 | | -using Xunit.Abstractions; |
2 | | -using Xunit; |
| 1 | +using System; |
| 2 | +using System.IO; |
3 | 3 | using System.Threading.Tasks; |
4 | | -using CefSharp.OffScreen; |
5 | 4 | using CefSharp.Example; |
| 5 | +using CefSharp.OffScreen; |
6 | 6 | using CefSharp.SchemeHandler; |
7 | | -using System.IO; |
| 7 | +using Xunit; |
| 8 | +using Xunit.Abstractions; |
8 | 9 |
|
9 | 10 | namespace CefSharp.Test.SchemeHandler |
10 | 11 | { |
@@ -56,6 +57,64 @@ public async Task ShouldWork() |
56 | 57 | } |
57 | 58 | } |
58 | 59 |
|
| 60 | + [Fact] |
| 61 | + public async Task ShouldPreventPathTraversalAttack() |
| 62 | + { |
| 63 | + const string hostUrl = "https://folderschemehandlerfactory.test/"; |
| 64 | + |
| 65 | + // 1. Setup temporary directory structure |
| 66 | + var tempParent = Path.Combine(Path.GetTempPath(), "CefSharpShouldPreventPathTraversalAttack-" + Guid.NewGuid()); |
| 67 | + var root = Path.Combine(tempParent, "www"); |
| 68 | + var sibling = Path.Combine(tempParent, "www2"); |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + Directory.CreateDirectory(root); |
| 73 | + Directory.CreateDirectory(sibling); |
| 74 | + |
| 75 | + File.WriteAllText(Path.Combine(root, "index.html"), "root-index"); |
| 76 | + File.WriteAllText(Path.Combine(sibling, "secret.txt"), "sibling-secret"); |
| 77 | + |
| 78 | + // 2. Initialize the CefSharp context and browser instances |
| 79 | + using (var requestContext = new RequestContext(Cef.GetGlobalRequestContext())) |
| 80 | + using (var browser = new ChromiumWebBrowser(CefExample.DefaultUrl, requestContext: requestContext, useLegacyRenderHandler: false)) |
| 81 | + { |
| 82 | + _ = await browser.WaitForInitialLoadAsync(); |
| 83 | + |
| 84 | + // Register factory targeting our custom root directory |
| 85 | + requestContext.RegisterSchemeHandlerFactory( |
| 86 | + "https", |
| 87 | + "folderschemehandlerfactory.test", |
| 88 | + new FolderSchemeHandlerFactory(root, defaultPage: "index.html")); |
| 89 | + |
| 90 | + // 3. Attempt to break out of 'www' using an escaped path traversal sequence |
| 91 | + var traversalUrl = hostUrl + "..%2fwww2/secret.txt"; |
| 92 | + var response = await browser.LoadUrlAsync(traversalUrl); |
| 93 | + |
| 94 | + var mainFrame = browser.GetMainFrame(); |
| 95 | + Assert.True(mainFrame.IsValid); |
| 96 | + |
| 97 | + // 4. Security Assertions: The factory should sanitize the path and return a 404. |
| 98 | + // If the code is secure, HttpStatusCode should be 404 (NotFound). |
| 99 | + Assert.Equal(404, response.HttpStatusCode); |
| 100 | + |
| 101 | + // Fetch DOM contents to double-check that the file contents leaked nowhere |
| 102 | + var jsResponse = await browser.EvaluateScriptAsync("document.documentElement.innerText"); |
| 103 | + var bodyText = jsResponse.Result?.ToString() ?? string.Empty; |
| 104 | + |
| 105 | + Assert.DoesNotContain("sibling-secret", bodyText); |
| 106 | + } |
| 107 | + } |
| 108 | + finally |
| 109 | + { |
| 110 | + // 5. Clean up temporary directories and files safely |
| 111 | + if (Directory.Exists(tempParent)) |
| 112 | + { |
| 113 | + Directory.Delete(tempParent, recursive: true); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
59 | 118 | [Fact] |
60 | 119 | public async Task ShouldAllowFileDeletionAfterLoading() |
61 | 120 | { |
|
0 commit comments