Skip to content

Commit

Permalink
Use a few different error pages when legacy file protocol handler errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed May 13, 2024
1 parent 29064a1 commit 027d125
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
19 changes: 0 additions & 19 deletions src-main/legacy-file-protocol-error-fallback.html
Original file line number Diff line number Diff line change
@@ -1,19 +0,0 @@
<!DOCTYPE html>
<!--
This is the fallback page used in the Windows 7/8/8.1 legacy build when one of our custom
Electron file protocols return an error. Electron only lets us return a path, so we just
return this file. It's better than an empty screen at least.
This page is served with a CSP of "default-src 'none'" so no inline styles/scripts.
-->
<html lang="en">
<head>
<meta charset="utf8">
<meta name="color-scheme" content="dark light">
</head>
<body>
<h1>Protocol handler error</h1>
<pre>Unknown legacy file protocol error</pre>
<p>If you can see this page, <a href="https://github.com/TurboWarp/desktop/issues">please open a GitHub issue</a> with all the information above.</p>
</body>
</html>
13 changes: 7 additions & 6 deletions src-main/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,13 @@ const createLegacyFileProtocolHandler = (metadata) => {
* @param {(result: {path: string; statusCode?: number; headers?: Record<string, string>;}) => void} callback
*/
return (request, callback) => {
const returnErrorResponse = (error) => {
// TODO: see if there's a better way to surface this
const returnErrorResponse = (error, errorPage) => {
console.error(error);
callback({
status: 400,
path: path.join(__dirname, 'legacy-file-protocol-error-fallback.html'),
// All we can return is a file path, so we just have a few different ones baked in
// for each error that we expect.
path: path.join(__dirname, `../src-protocol-error/legacy-file/${errorPage}.html`),
headers: errorPageHeaders
});
};
Expand All @@ -266,14 +267,14 @@ const createLegacyFileProtocolHandler = (metadata) => {
const parsedURL = new URL(request.url);
const resolved = path.join(root, parsedURL.pathname);
if (!resolved.startsWith(root)) {
returnErrorResponse(new Error('Path traversal blocked'));
returnErrorResponse(new Error('Path traversal blocked'), 'path-traversal');
return;
}

const fileExtension = path.extname(resolved);
const mimeType = MIME_TYPES.get(fileExtension);
if (!mimeType) {
returnErrorResponse(new Error(`Invalid file extension: ${fileExtension}`));
returnErrorResponse(new Error(`Invalid file extension: ${fileExtension}`), 'invalid-extension');
return;
}

Expand All @@ -284,7 +285,7 @@ const createLegacyFileProtocolHandler = (metadata) => {
}
});
} catch (error) {
returnErrorResponse(error);
returnErrorResponse(error, 'unknown');
}
};
};
Expand Down
12 changes: 12 additions & 0 deletions src-protocol-error/legacy-file/invalid-extension.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<meta name="color-scheme" content="dark light">
</head>
<body>
<h1>Protocol handler error</h1>
<pre>Legacy file protocol: Invalid file extension</pre>
<p>If you can see this page, <a href="https://github.com/TurboWarp/desktop/issues">please open a GitHub issue</a> with all the information above.</p>
</body>
</html>
12 changes: 12 additions & 0 deletions src-protocol-error/legacy-file/path-traversal.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<meta name="color-scheme" content="dark light">
</head>
<body>
<h1>Protocol handler error</h1>
<pre>Legacy file protocol: Path traversal blocked</pre>
<p>If you can see this page, <a href="https://github.com/TurboWarp/desktop/issues">please open a GitHub issue</a> with all the information above.</p>
</body>
</html>
12 changes: 12 additions & 0 deletions src-protocol-error/legacy-file/unknown.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf8">
<meta name="color-scheme" content="dark light">
</head>
<body>
<h1>Protocol handler error</h1>
<pre>Legacy file protocol: Unknown</pre>
<p>If you can see this page, <a href="https://github.com/TurboWarp/desktop/issues">please open a GitHub issue</a> with all the information above.</p>
</body>
</html>

0 comments on commit 027d125

Please sign in to comment.