From 1e74111c8df642ff74ed7226e34bf9457d00374f Mon Sep 17 00:00:00 2001 From: guitavano Date: Wed, 28 Feb 2024 10:35:59 -0300 Subject: [PATCH] force https on request --- runtime/fresh/middlewares/2_stateBuilder.ts | 3 ++- runtime/fresh/routes/entrypoint.tsx | 4 ++-- utils/http.ts | 10 ++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/runtime/fresh/middlewares/2_stateBuilder.ts b/runtime/fresh/middlewares/2_stateBuilder.ts index 20c2462e..08a70cd8 100644 --- a/runtime/fresh/middlewares/2_stateBuilder.ts +++ b/runtime/fresh/middlewares/2_stateBuilder.ts @@ -20,6 +20,7 @@ import { STATE_CONTEXT_KEY, } from "../../../observability/otel/context.ts"; import { AppManifest, DecoSiteState, DecoState } from "../../../types.ts"; +import { forceHttps } from "deco/utils/http.ts"; import { buildInvokeFunc } from "../../../utils/invoke.server.ts"; import { createServerTimings } from "../../../utils/timings.ts"; import { setLogger } from "../../fetch/fetchLog.ts"; @@ -200,7 +201,7 @@ export const buildDecoState = ( const { resolver } = await liveContext.runtime; const ctxResolver = resolver .resolverFor( - { context, request }, + { context, request: forceHttps(request) }, { monitoring: context.state.monitoring, }, diff --git a/runtime/fresh/routes/entrypoint.tsx b/runtime/fresh/routes/entrypoint.tsx index a977db27..7fa9965c 100644 --- a/runtime/fresh/routes/entrypoint.tsx +++ b/runtime/fresh/routes/entrypoint.tsx @@ -6,7 +6,7 @@ import { Handler } from "../../../blocks/handler.ts"; import { Page } from "../../../blocks/page.tsx"; import { PageContext } from "../../../engine/block.ts"; import { DecoSiteState, DecoState, Flag } from "../../../types.ts"; -import { setCSPHeaders } from "../../../utils/http.ts"; +import { forceHttps, setCSPHeaders } from "../../../utils/http.ts"; export interface RouterContext { pagePath: string; @@ -73,7 +73,7 @@ const innerHandler = async ( return setCSPHeaders( req, - await handler(req, ctx as ConnInfo), + await handler(forceHttps(req), ctx as ConnInfo), ); }; diff --git a/utils/http.ts b/utils/http.ts index 3965cc7c..5b3573ec 100644 --- a/utils/http.ts +++ b/utils/http.ts @@ -206,3 +206,13 @@ export const allowCorsForOrigin = ( }); export { readFromStream } from "../clients/withManifest.ts"; + +export const forceHttps = (req: Request) => { + let httpsReq = req; + if (req.url.startsWith("http:") && !req.url.includes("localhost")) { + const url = new URL(req.url); + url.protocol = "https:"; + httpsReq = new Request(url, req); + } + return httpsReq; +}; \ No newline at end of file