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

fix(deno-deploy): shim x-forwarded-for and x-forwarded-proto headers #2026

Merged
merged 5 commits into from
Jan 4, 2024
Merged
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
29 changes: 25 additions & 4 deletions src/runtime/entries/deno-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,35 @@ import "#internal/nitro/virtual/polyfill";
// @ts-ignore
import { nitroApp } from "../app";

// https://deno.land/api?s=Deno.ServeHandlerInfo
type ServeHandlerInfo = {
remoteAddr: {
transport: "tcp" | "udp";
hostname: string;
port: number;
};
};

// @ts-expect-error unknown global Deno
Deno.serve((request: Request) => {
return handleRequest(request);
Deno.serve((request, info) => {
return handleRequest(request, info);
});

async function handleRequest(request: Request) {
async function handleRequest(request: Request, info: ServeHandlerInfo) {
const url = new URL(request.url);

const headers = new Headers(request.headers);

// Add client IP address to headers
// (rightmost is most trustable)
headers.append("x-forwarded-for", info.remoteAddr.hostname);

// There is currently no way to know if the request was made over HTTP or HTTPS
// Deno deploy force redirects to HTTPS so we assume HTTPS by default
if (!headers.has("x-forwarded-proto")) {
headers.set("x-forwarded-proto", "https");
}

// https://deno.land/api?s=Body
let body;
if (request.body) {
Expand All @@ -19,7 +40,7 @@ async function handleRequest(request: Request) {
return nitroApp.localFetch(url.pathname + url.search, {
host: url.hostname,
protocol: url.protocol,
headers: request.headers,
headers,
method: request.method,
redirect: request.redirect,
body,
Expand Down