Skip to content

Commit

Permalink
Fix: add check content-type to be html before js injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzal3x committed Dec 19, 2024
1 parent 897f6ca commit d7f63f3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 5 additions & 5 deletions portal/server/app/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function GET(req: Request) {
if (walrusPath) {
console.log(`Redirecting to aggregator url response: ${req.url} from ${objectIdPath}`);
const response = redirectToAggregatorUrlResponse(url, walrusPath);
return inject_unregister_service_worker_script(response);
return await inject_unregister_service_worker_script(response);
}

const parsedUrl = getSubdomainAndPath(url, Number(portalDomainNameLength));
Expand All @@ -54,12 +54,12 @@ export async function GET(req: Request) {

if (parsedUrl) {
if (blocklistChecker && await blocklistChecker.isBlocked(parsedUrl.subdomain)) {
return inject_unregister_service_worker_script(siteNotFound());
return await inject_unregister_service_worker_script(siteNotFound());
}

if (requestDomain == portalDomain && parsedUrl.subdomain) {
const response = await urlFetcher.resolveDomainAndFetchUrl(parsedUrl, null, blocklistChecker);
return inject_unregister_service_worker_script(response);
return await inject_unregister_service_worker_script(response);
}
}

Expand All @@ -74,9 +74,9 @@ export async function GET(req: Request) {
null,
blocklistChecker
);
return inject_unregister_service_worker_script(response);
return await inject_unregister_service_worker_script(response);
}

const response404 = new Response(`Resource at ${originalUrl} not found!`, { status: 404 });
return inject_unregister_service_worker_script(response404);
return await inject_unregister_service_worker_script(response404);
}
6 changes: 6 additions & 0 deletions portal/server/inject_unregister_sw_script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
* @returns a new response with the script injected.
*/
export async function inject_unregister_service_worker_script(response: Response): Promise<Response> {
const contentType = response.headers.get('content-type');
if (!contentType || !contentType.includes('text/html')) {
console.log('Skipping service worker unregistration script injection because the content type is not HTML.');
return response;
}

let responseBody = await response.text();
const script = `
<script>
Expand Down

0 comments on commit d7f63f3

Please sign in to comment.