Skip to content

Commit

Permalink
fix(deno-deploy): shim x-forwarded-for and x-forwarded-proto head…
Browse files Browse the repository at this point in the history
…ers (#2026)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
  • Loading branch information
jasonleong and pi0 authored Jan 4, 2024
1 parent 17f922a commit 9eeb9d8
Showing 1 changed file with 25 additions and 4 deletions.
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

0 comments on commit 9eeb9d8

Please sign in to comment.