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

XFF_DEPTH_ENV #4357

Merged
merged 4 commits into from
Mar 16, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/adapter-node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface AdapterOptions {
host?: string;
port?: string;
origin?: string;
xffDepth?: string;
headers?: {
address?: string;
protocol?: string;
Expand Down
2 changes: 2 additions & 0 deletions packages/adapter-node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default function ({
host: host_env = 'HOST',
port: port_env = 'PORT',
origin: origin_env = 'ORIGIN',
xffDepth: xff_depth_env = 'XFF_DEPTH',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No objections to this because I know it's consistent with the others, but it really seems unnecessary

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess the question becomes 'why is it necessary for the others but not this?' and if it isn't necessary for the others, then why do we need to make the env vars configurable at all?

headers: {
address: address_header_env = 'ADDRESS_HEADER',
protocol: protocol_header_env = 'PROTOCOL_HEADER',
Expand Down Expand Up @@ -52,6 +53,7 @@ export default function ({
HOST_ENV: JSON.stringify(host_env),
PORT_ENV: JSON.stringify(port_env),
ORIGIN: origin_env ? `process.env[${JSON.stringify(origin_env)}]` : 'undefined',
XFF_DEPTH_ENV: xff_depth_env,
PROTOCOL_HEADER: JSON.stringify(protocol_header_env),
HOST_HEADER: JSON.stringify(host_header_env),
ADDRESS_HEADER: JSON.stringify(address_header_env)
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-node/src/handler.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ declare global {
const ADDRESS_HEADER: string;
const HOST_HEADER: string;
const PROTOCOL_HEADER: string;
const X_FORWARDED_FOR_PROXIES: number;
const XFF_DEPTH_ENV: string;
}

export const handler: Handle;
25 changes: 8 additions & 17 deletions packages/adapter-node/src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,18 @@ import { getRequest, setResponse } from '@sveltejs/kit/node';
import { Server } from 'SERVER';
import { manifest } from 'MANIFEST';

/* global ORIGIN, ADDRESS_HEADER, PROTOCOL_HEADER, HOST_HEADER */
/* global ORIGIN, ADDRESS_HEADER, PROTOCOL_HEADER, HOST_HEADER, XFF_DEPTH_ENV */

const server = new Server(manifest);
const origin = ORIGIN;
const xff_depth = XFF_DEPTH_ENV ? parseInt(process.env[XFF_DEPTH_ENV]) : 1;

const xff_depth = get_xff_depth();
const address_header = ADDRESS_HEADER && (process.env[ADDRESS_HEADER] || '').toLowerCase();
const protocol_header = PROTOCOL_HEADER && process.env[PROTOCOL_HEADER];
const host_header = (HOST_HEADER && process.env[HOST_HEADER]) || 'host';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

function get_xff_depth() {
const value = process.env['XFF_DEPTH'];
let xff_depth;
try {
xff_depth = value ? parseInt(value) : 1;
} catch (err) {
throw new Error('Expected XFF_DEPTH to be an integer. Received ${value}');
}
if (xff_depth < 1) {
throw new Error('XFF_DEPTH cannot be less than 1');
}
return xff_depth;
}

/**
* @param {string} path
* @param {number} max_age
Expand Down Expand Up @@ -77,9 +63,14 @@ const ssr = async (req, res) => {

if (address_header === 'x-forwarded-for') {
const addresses = value.split(',');

if (xff_depth < 1) {
throw new Error(`${XFF_DEPTH_ENV} must be a positive integer`);
}

if (xff_depth > addresses.length) {
throw new Error(
`XFF_DEPTH specified as ${xff_depth}, but only found ${addresses.length} addresses`
`${XFF_DEPTH_ENV} is ${xff_depth}, but only found ${addresses.length} addresses`
);
}
return addresses[addresses.length - xff_depth].trim();
Expand Down