Files
BetterDesk/web-nodejs/tests/wanSecurity.test.js
T
UNITRONIX 6b87f53943 feat(console): pairing page, public route allowlist and locale keys
The panel side of short-code pairing.

- New `/pairing` page (`views/pairing.ejs` + `public/js/pairing.js`) next to the existing
  QR activation flow: mint a code with an optional label, see the countdown, copy it, and
  revoke active codes. The plaintext code is shown once, because only its hash is stored.
- `routes/pairing.routes.js` proxies mint/list/revoke to Go behind `requireAuth` and
  `enrollment.approve`; the client redeems directly against Go, so no code is ever handled
  by the panel's public API.
- `wanSecurity.js` allows `/api/pairing/redeem` and `/api/pairing/config` on :21121 as POST
  only. Minting and listing stay off the public client API: a GET could spend somebody
  else's code via a link preview, and listing would expose code metadata.
- `betterdeskApi` gains `createPairingCode`, `listPairingCodes`, `revokePairingCode`.
- 28 new keys plus a `nav.pairing` entry in all 26 locale files. `scripts/check-pairing-i18n.js`
  enforces parity and runs in CI.

Thanks: INSOLVE (Honorary); Marco Jakobs (@jacotec); MyNameisStitch (@MyNameisStitch); Redspin (@playerumpknow); muopc (@muopc)
2026-11-08 15:04:34 +01:00

137 lines
4.5 KiB
JavaScript

'use strict';
const { EventEmitter } = require('node:events');
const { bodySizeLimit, pathWhitelist } = require('../middleware/wanSecurity');
function runWhitelist(path, method) {
const req = { path, method };
const response = {
statusCode: 200,
status(code) {
this.statusCode = code;
return this;
},
end() {
this.ended = true;
return this;
},
};
let nextCalled = false;
pathWhitelist(req, response, () => {
nextCalled = true;
});
return { ...response, nextCalled };
}
function runBodySizeLimit(path, size) {
const request = new EventEmitter();
request.path = path;
request.destroy = jest.fn();
const response = {
statusCode: 200,
status(code) {
this.statusCode = code;
return this;
},
json() {
this.headersSent = true;
return this;
},
};
let nextCalled = false;
bodySizeLimit(request, response, () => {
nextCalled = true;
});
request.emit('data', Buffer.alloc(size));
return { ...response, nextCalled, destroyed: request.destroy.mock.calls.length > 0 };
}
describe('WAN API path whitelist', () => {
test('allows the public telemetry key endpoint', () => {
const result = runWhitelist('/api/telemetry/key', 'GET');
expect(result.nextCalled).toBe(true);
expect(result.statusCode).toBe(200);
});
test('rejects non-GET telemetry key requests', () => {
const result = runWhitelist('/api/telemetry/key', 'POST');
expect(result.nextCalled).toBe(false);
expect(result.statusCode).toBe(405);
});
test('allows public branding reads and rejects branding writes', () => {
expect(runWhitelist('/api/branding', 'GET').nextCalled).toBe(true);
const result = runWhitelist('/api/branding', 'POST');
expect(result.nextCalled).toBe(false);
expect(result.statusCode).toBe(405);
});
test('allows heartbeat payloads larger than the default body limit', () => {
const result = runBodySizeLimit('/api/heartbeat', 2048);
expect(result.nextCalled).toBe(true);
expect(result.statusCode).toBe(200);
expect(result.destroyed).toBe(false);
});
test('rejects oversized unknown request bodies', () => {
const result = runBodySizeLimit('/api/unknown', 2048);
expect(result.nextCalled).toBe(true);
expect(result.statusCode).toBe(413);
expect(result.destroyed).toBe(true);
});
test('allows unauthenticated enrollment bootstrap endpoints', () => {
expect(runWhitelist('/api/devices/register', 'POST').nextCalled).toBe(true);
expect(runWhitelist('/api/devices/register/status', 'GET').nextCalled).toBe(true);
});
test('allows pairing redeem and preview, but only as POST', () => {
// Redeem must be reachable without credentials; a GET would let a link or a
// prefetch spend somebody else's code.
expect(runWhitelist('/api/pairing/redeem', 'POST').nextCalled).toBe(true);
expect(runWhitelist('/api/pairing/config', 'POST').nextCalled).toBe(true);
const asGet = runWhitelist('/api/pairing/redeem', 'GET');
expect(asGet.nextCalled).toBe(false);
expect(asGet.statusCode).toBe(405);
});
test('keeps pairing code minting off the public client API', () => {
// Minting and listing codes requires an operator session on the panel; exposing
// them on :21121 would let anyone enumerate active code metadata.
expect(runWhitelist('/api/pairing/codes', 'POST').nextCalled).toBe(false);
expect(runWhitelist('/api/pairing/codes', 'GET').nextCalled).toBe(false);
});
test('allows both current-user methods used by clients', () => {
expect(runWhitelist('/api/currentUser', 'GET').nextCalled).toBe(true);
expect(runWhitelist('/api/currentUser', 'POST').nextCalled).toBe(true);
expect(runWhitelist('/api/currentUser', 'OPTIONS').nextCalled).toBe(true);
});
test('rejects unsupported current-user methods', () => {
for (const method of ['PUT', 'DELETE']) {
const result = runWhitelist('/api/currentUser', method);
expect(result.nextCalled).toBe(false);
expect(result.statusCode).toBe(405);
}
});
test('continues rejecting unknown paths', () => {
const result = runWhitelist('/api/not-allowed', 'GET');
expect(result.nextCalled).toBe(false);
expect(result.statusCode).toBe(404);
});
});