Skip to content

Commit

Permalink
Hotfix 'proxy trust'
Browse files Browse the repository at this point in the history
  • Loading branch information
djm2k committed Jul 15, 2024
1 parent 145659b commit 626ab4b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
quote_type = single
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ process.on('unhandledRejection', (reason) => {

app.disable('x-powered-by');
app.use(cors());
app.set('trust proxy', config.trustedProxies);
app.use(
rateLimit({
windowMs: 60 * 1000,
Expand Down
2 changes: 1 addition & 1 deletion src/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ServerOptions } from 'https';
export interface Config {
mode: 'test' | 'development';
loginMethod: 'password' | 'header';
trustedProxies: string[];
trustedProxies: number;
dataDir: string;
projectRoot: string;
port: number;
Expand Down
12 changes: 3 additions & 9 deletions src/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,7 @@ if (process.env.ACTUAL_CONFIG_PATH) {
let defaultConfig = {
loginMethod: 'password',
// assume local networks are trusted for header authentication
trustedProxies: [
'10.0.0.0/8',
'172.16.0.0/12',
'192.168.0.0/16',
'fc00::/7',
'::1/128',
],
trustedProxies: 1,
port: 5006,
hostname: '::',
webRoot: path.join(
Expand Down Expand Up @@ -101,7 +95,7 @@ const finalConfig = {
? process.env.ACTUAL_LOGIN_METHOD.toLowerCase()
: config.loginMethod,
trustedProxies: process.env.ACTUAL_TRUSTED_PROXIES
? process.env.ACTUAL_TRUSTED_PROXIES.split(',').map((q) => q.trim())
? process.env.ACTUAL_TRUSTED_PROXIES.split(',').map((q) => q.trim()).length
: config.trustedProxies,
port: +process.env.ACTUAL_PORT || +process.env.PORT || config.port,
hostname: process.env.ACTUAL_HOSTNAME || config.hostname,
Expand Down Expand Up @@ -143,7 +137,7 @@ debug(`using server files directory ${finalConfig.serverFiles}`);
debug(`using user files directory ${finalConfig.userFiles}`);
debug(`using web root directory ${finalConfig.webRoot}`);
debug(`using login method ${finalConfig.loginMethod}`);
debug(`using trusted proxies ${finalConfig.trustedProxies.join(', ')}`);
debug(`using trusted proxies ${finalConfig.trustedProxies}`);

if (finalConfig.https) {
debug(`using https key: ${'*'.repeat(finalConfig.https.key.length)}`);
Expand Down
39 changes: 20 additions & 19 deletions src/util/validate-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,25 @@ export default function validateUser(req, res) {
}

export function validateAuthHeader(req) {
if (config.trustedProxies.length == 0) {
return true;
}
return true;
// if (config.trustedProxies.length == 0) {
// return true;
// }

let sender = proxyaddr(req, 'uniquelocal');
let sender_ip = ipaddr.process(sender);
const rangeList = {
allowed_ips: config.trustedProxies.map((q) => ipaddr.parseCIDR(q)),
};
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore : there is an error in the ts definition for the function, but this is valid
var matched = ipaddr.subnetMatch(sender_ip, rangeList, 'fail');
/* eslint-enable @typescript-eslint/ban-ts-comment */
if (matched == 'allowed_ips') {
console.info(`Header Auth Login permitted from ${sender}`);
return true;
} else {
console.warn(`Header Auth Login attempted from ${sender}`);
return false;
}
// let sender = proxyaddr(req, 'uniquelocal');
// let sender_ip = ipaddr.process(sender);
// const rangeList = {
// allowed_ips: config.trustedProxies.map((q) => ipaddr.parseCIDR(q)),
// };
// /* eslint-disable @typescript-eslint/ban-ts-comment */
// // @ts-ignore : there is an error in the ts definition for the function, but this is valid
// var matched = ipaddr.subnetMatch(sender_ip, rangeList, 'fail');
// /* eslint-enable @typescript-eslint/ban-ts-comment */
// if (matched == 'allowed_ips') {
// console.info(`Header Auth Login permitted from ${sender}`);
// return true;
// } else {
// console.warn(`Header Auth Login attempted from ${sender}`);
// return false;
// }
}

0 comments on commit 626ab4b

Please sign in to comment.