Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

force https on request (rebased version) #587

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion runtime/fresh/middlewares/2_stateBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -200,7 +201,7 @@ export const buildDecoState = <TManifest extends AppManifest = AppManifest>(
const { resolver } = await liveContext.runtime;
const ctxResolver = resolver
.resolverFor(
{ context, request },
{ context, request: forceHttps(request) },
{
monitoring: context.state.monitoring,
},
Expand Down
4 changes: 2 additions & 2 deletions runtime/fresh/routes/entrypoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ const innerHandler = async (

return setCSPHeaders(
req,
await handler(req, ctx as ConnInfo),
await handler(forceHttps(req), ctx as ConnInfo),
);
};

Expand Down
10 changes: 10 additions & 0 deletions utils/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Loading