The swiss army knife of lossless video/audio editing
harden http server
Mikael Finstad260802348955
diff --git a/src/main/httpServer.ts b/src/main/httpServer.tsindex ccade682..51a4e892 100644--- a/src/main/httpServer.ts+++ b/src/main/httpServer.ts@@ -5,6 +5,7 @@ import assert from 'node:assert';import { homepageUrl } from '../common/constants.js';import logger from './logger.js';+import { isRequestAllowed } from './httpServerUtil.js';import type { AppEvent } from './index.js';@@ -22,6 +23,16 @@ export default ({ port, onKeyboardAction, onAwaitAppEvent }: {stream: { write: (message) => logger.info(message.trim()) },}));+ app.use((req, res, next) => {+ const { host, origin } = req.headers;+ if (!isRequestAllowed({ host, origin, port })) {+ logger.warn('Rejecting HTTP API request', { host, origin });+ res.status(403).send('Forbidden: the HTTP API can only be called by programs running on this computer, not from a web browser.');+ return;+ }+ next();+ });+const apiRouter = express.Router();app.get('/', (_req, res) => res.send(`See ${homepageUrl}`));diff --git a/src/main/httpServerUtil.test.ts b/src/main/httpServerUtil.test.tsnew file mode 100644index 00000000..ba3db4f3--- /dev/null+++ b/src/main/httpServerUtil.test.ts@@ -0,0 +1,38 @@+// eslint-disable-next-line import/no-extraneous-dependencies+import { test, expect, describe } from 'vitest';++import { isRequestAllowed } from './httpServerUtil.js';+++describe('http api request validation', () => {+ test('allows a local non-browser client', () => {+ expect(isRequestAllowed({ host: '127.0.0.1:8080', origin: undefined, port: 8080 })).toBe(true);+ expect(isRequestAllowed({ host: 'localhost:8080', origin: undefined, port: 8080 })).toBe(true);+ expect(isRequestAllowed({ host: '[::1]:8080', origin: undefined, port: 8080 })).toBe(true);+ expect(isRequestAllowed({ host: 'LocalHost:8080', origin: undefined, port: 8080 })).toBe(true);+ });++ test('allows a custom port', () => {+ expect(isRequestAllowed({ host: '127.0.0.1:1234', origin: undefined, port: 1234 })).toBe(true);+ expect(isRequestAllowed({ host: '127.0.0.1:8080', origin: undefined, port: 1234 })).toBe(false);+ });++ test('allows an omitted port only when listening on port 80', () => {+ expect(isRequestAllowed({ host: '127.0.0.1', origin: undefined, port: 80 })).toBe(true);+ expect(isRequestAllowed({ host: '127.0.0.1', origin: undefined, port: 8080 })).toBe(false);+ });++ // an attacker controlled domain that has been rebound to 127.0.0.1+ test('rejects a rebound host', () => {+ expect(isRequestAllowed({ host: 'attacker.example.com:8080', origin: undefined, port: 8080 })).toBe(false);+ expect(isRequestAllowed({ host: '127.0.0.1.attacker.example.com:8080', origin: undefined, port: 8080 })).toBe(false);+ expect(isRequestAllowed({ host: undefined, origin: undefined, port: 8080 })).toBe(false);+ });++ // a web page trying to make the user's browser call the api on their behalf+ test('rejects a request from a browser', () => {+ expect(isRequestAllowed({ host: '127.0.0.1:8080', origin: 'https://attacker.example.com', port: 8080 })).toBe(false);+ expect(isRequestAllowed({ host: '127.0.0.1:8080', origin: 'null', port: 8080 })).toBe(false);+ expect(isRequestAllowed({ host: '127.0.0.1:8080', origin: 'http://127.0.0.1:8080', port: 8080 })).toBe(false);+ });+});diff --git a/src/main/httpServerUtil.ts b/src/main/httpServerUtil.tsnew file mode 100644index 00000000..ea53fdbe--- /dev/null+++ b/src/main/httpServerUtil.ts@@ -0,0 +1,19 @@+// The HTTP API has no authentication, so we need to make sure that requests actually come from a+// program running on this machine, and not from a web page that the user happens to be visiting.+// - Checking `Host` prevents DNS rebinding, where an attacker controlled domain name is made to+// resolve to 127.0.0.1 so that their page becomes same-origin with us.+// - Checking `Origin` prevents CSRF. Browsers always send `Origin` on cross origin requests (and on+// all POSTs), while non-browser clients like curl never send it.+// eslint-disable-next-line import/prefer-default-export+export function isRequestAllowed({ host, origin, port }: {+ host: string | undefined,+ origin: string | undefined,+ port: number,+}) {+ if (origin != null) return false;+ if (host == null) return false;+ const allowedHosts = ['127.0.0.1', 'localhost', '[::1]'];+ const hostLower = host.toLowerCase();+ // a browser omits the port from `Host` when it's the default port for the scheme+ return allowedHosts.some((allowedHost) => hostLower === `${allowedHost}:${port}` || (port === 80 && hostLower === allowedHost));+}