Skip to content

Commit

Permalink
Handle index.html rerouting at protocol level, fix extension docs in …
Browse files Browse the repository at this point in the history
…Windows legacy build
  • Loading branch information
GarboMuffin committed Dec 22, 2024
1 parent a794821 commit 5526428
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
41 changes: 30 additions & 11 deletions src-main/protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const packageJSON = require('../package.json');
* @property {boolean} [brotli] Defaults to false
* @property {boolean} [embeddable] Defaults to false
* @property {boolean} [stream] Defaults to false
* @property {string} [index] Defaults to none
* @property {string} [directoryIndex] Defaults to none
* @property {string} [defaultExtension] Defaults to n one
* @property {string} [csp] Defaults to none
*/

Expand Down Expand Up @@ -56,7 +57,8 @@ const FILE_SCHEMES = {
supportFetch: true,
embeddable: true,
stream: true,
index: '.html',
directoryIndex: 'index.html',
defaultExtension: '.html',
csp: "default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; script-src 'self' 'unsafe-inline'"
},
'tw-update': {
Expand Down Expand Up @@ -207,15 +209,19 @@ const createModernProtocolHandler = (metadata) => {
};

try {
const parsedURL = new URL(request.url);
let parsedURL = new URL(request.url);
if (parsedURL.pathname.endsWith('/') && metadata.directoryIndex) {
parsedURL = new URL(metadata.directoryIndex, parsedURL);
}

let resolved = path.join(root, parsedURL.pathname);
if (!resolved.startsWith(root)) {
return createErrorResponse(new Error('Path traversal blocked'));
}

let fileExtension = path.extname(resolved);
if (!fileExtension && metadata.index) {
fileExtension = metadata.index;
if (!fileExtension && metadata.defaultExtension) {
fileExtension = metadata.defaultExtension;
resolved = `${resolved}${fileExtension}`;
}

Expand Down Expand Up @@ -275,16 +281,20 @@ const createLegacyBrotliProtocolHandler = (metadata) => {
};

try {
const parsedURL = new URL(request.url);
let parsedURL = new URL(request.url);
if (parsedURL.pathname.endsWith('/') && metadata.directoryIndex) {
parsedURL = new URL(metadata.directoryIndex, parsedURL);
}

let resolved = path.join(root, parsedURL.pathname);
if (!resolved.startsWith(root)) {
returnErrorPage(new Error('Path traversal blocked'));
return;
}

let fileExtension = path.extname(resolved);
if (!fileExtension && metadata.index) {
fileExtension = metadata.index;
if (!fileExtension && metadata.defaultExtension) {
fileExtension = metadata.defaultExtension;
resolved = `${resolved}${fileExtension}`;
}

Expand Down Expand Up @@ -337,14 +347,23 @@ const createLegacyFileProtocolHandler = (metadata) => {
};

try {
const parsedURL = new URL(request.url);
const resolved = path.join(root, parsedURL.pathname);
let parsedURL = new URL(request.url);
if (parsedURL.pathname.endsWith('/') && metadata.directoryIndex) {
parsedURL = new URL(metadata.directoryIndex, parsedURL);
}

let resolved = path.join(root, parsedURL.pathname);
if (!resolved.startsWith(root)) {
returnErrorResponse(new Error('Path traversal blocked'), 'path-traversal');
return;
}

const fileExtension = path.extname(resolved);
let fileExtension = path.extname(resolved);
if (!fileExtension && metadata.defaultExtension) {
fileExtension = metadata.defaultExtension;
resolved = `${resolved}${fileExtension}`;
}

const mimeType = MIME_TYPES.get(fileExtension);
if (!mimeType) {
returnErrorResponse(new Error(`Invalid file extension: ${fileExtension}`), 'invalid-extension');
Expand Down
4 changes: 2 additions & 2 deletions src-main/windows/project-running-window.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ class ProjectRunningWindow extends AbtractWindow {
}

if (parsed.origin === 'https://extensions.turbowarp.org') {
const pathname = parsed.pathname.endsWith('/') ? `${parsed.pathname}index.html` : parsed.pathname;
return callback({
redirectURL: `tw-extensions://.${pathname}`
// pathname always has a leading / already
redirectURL: `tw-extensions://.${parsed.pathname}`
});
}

Expand Down

0 comments on commit 5526428

Please sign in to comment.