Harden the app
This commit is contained in:
1 parent
d92c4b50cf
commit
e3a2b348d6
5 files changed
+38
-12
No files matched your search
@@ -6,8 +6,8 @@ size limit: the file goes directly to the other person and is never stored on a
|
|||||||
## How to send a file
|
## How to send a file
|
||||||
|
|
||||||
1. **Sender:** open HellCon, drop a file onto the Send card and click **Share via tracker**. You get
|
1. **Sender:** open HellCon, drop a file onto the Send card and click **Share via tracker**. You get
|
||||||
an 8 character code. Tell it to the receiver, it works for 6 minutes. Whoever has the code can
|
a 10 character code. Tell it to the receiver, it works for 6 minutes. Whoever has the code can
|
||||||
get the file, so give it to nobody else.
|
get the file, or pretend to be you, so give it to nobody else.
|
||||||
2. **Receiver:** open HellCon, enter the code under Receive and click **Receive**.
|
2. **Receiver:** open HellCon, enter the code under Receive and click **Receive**.
|
||||||
3. **Receiver:** once connected, you see the file's name and size. Click **Download** to save it.
|
3. **Receiver:** once connected, you see the file's name and size. Click **Download** to save it.
|
||||||
|
|
||||||
|
|||||||
+14
-2
@@ -45,9 +45,21 @@ const isId = (value) => typeof value === 'string' && value.length === 20;
|
|||||||
const isSdp = (value, type) => value?.type === type && typeof value.sdp === 'string';
|
const isSdp = (value, type) => value?.type === type && typeof value.sdp === 'string';
|
||||||
|
|
||||||
// Behind Caddy, the only way in. Caddy replaces any X-Forwarded-For a client sends, the last entry is
|
// Behind Caddy, the only way in. Caddy replaces any X-Forwarded-For a client sends, the last entry is
|
||||||
// its own anyway.
|
// its own anyway. IPv6 counts by /64: a single host usually has a whole one, every address in it
|
||||||
|
// would get MAX_PER_IP of its own. IPv4, also mapped to IPv6, counts as it is.
|
||||||
function clientIp(req) {
|
function clientIp(req) {
|
||||||
return req.headers['x-forwarded-for']?.split(',').at(-1).trim() || req.socket.remoteAddress;
|
const ip = req.headers['x-forwarded-for']?.split(',').at(-1).trim() || req.socket.remoteAddress;
|
||||||
|
if (!ip?.includes(':') || ip.includes('.')) return ip;
|
||||||
|
const [head, tail] = ip.split('::');
|
||||||
|
const groups = head ? head.split(':') : [];
|
||||||
|
if (tail !== undefined) {
|
||||||
|
const rest = tail ? tail.split(':') : [];
|
||||||
|
groups.push(...Array(Math.max(0, 8 - groups.length - rest.length)).fill('0'), ...rest);
|
||||||
|
}
|
||||||
|
return `${groups
|
||||||
|
.slice(0, 4)
|
||||||
|
.map((group) => parseInt(group, 16).toString(16))
|
||||||
|
.join(':')}::/64`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function refuse(socket, status) {
|
function refuse(socket, status) {
|
||||||
|
|||||||
@@ -323,6 +323,17 @@ test('limits connections per IP to 16', async () => {
|
|||||||
expect(await open()).toBeTruthy();
|
expect(await open()).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('counts IPv6 by /64, however it is written', async () => {
|
||||||
|
for (let i = 1; i <= 16; i++) {
|
||||||
|
ip = [`2001:db8:${tests}::${i.toString(16)}`, `2001:0DB8:${tests}:0:${i}::`][i % 2];
|
||||||
|
expect(await open()).toBeTruthy();
|
||||||
|
}
|
||||||
|
ip = `2001:db8:${tests}:0:ffff:ffff:ffff:ffff`;
|
||||||
|
expect(await open()).toBeUndefined();
|
||||||
|
ip = `2001:db8:${tests}:1::1`;
|
||||||
|
expect(await open()).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
test('survives clients resetting refused connections', async () => {
|
test('survives clients resetting refused connections', async () => {
|
||||||
for (let i = 0; i < 100; i++) {
|
for (let i = 0; i < 100; i++) {
|
||||||
const socket = connectTcp(new URL(url).port, '127.0.0.1');
|
const socket = connectTcp(new URL(url).port, '127.0.0.1');
|
||||||
|
|||||||
+1
-1
@@ -73,7 +73,7 @@ const MODES: Record<string, (sender: Page, receiver: Page) => Promise<void>> = {
|
|||||||
},
|
},
|
||||||
async tracker(sender, receiver) {
|
async tracker(sender, receiver) {
|
||||||
await sender.click('#share');
|
await sender.click('#share');
|
||||||
await expect(sender.locator('#send-code')).toHaveText(/^[0-9a-zA-Z]{8}$/);
|
await expect(sender.locator('#send-code')).toHaveText(/^[0-9a-zA-Z]{10}$/);
|
||||||
await sender.click('#copy-send-code');
|
await sender.click('#copy-send-code');
|
||||||
await expect(sender.locator('#copy-send-code')).toHaveText('Copied');
|
await expect(sender.locator('#copy-send-code')).toHaveText('Copied');
|
||||||
await expect(sender.locator('#log')).toContainText('waiting on');
|
await expect(sender.locator('#log')).toContainText('waiting on');
|
||||||
|
|||||||
+10
-7
@@ -258,21 +258,24 @@ function randomBinary(length: number) {
|
|||||||
|
|
||||||
const CODE_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
const CODE_CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
|
||||||
// 8 of 62 characters. Bytes from 248 on are dropped, so every character is equally likely.
|
// 10 of 62 characters. Bytes from 248 on are dropped, so every character is equally likely.
|
||||||
function randomCode() {
|
function randomCode() {
|
||||||
let code = '';
|
let code = '';
|
||||||
while (code.length < 8)
|
while (code.length < 10)
|
||||||
for (const byte of crypto.getRandomValues(new Uint8Array(8)))
|
for (const byte of crypto.getRandomValues(new Uint8Array(10)))
|
||||||
if (byte < 248 && code.length < 8) code += CODE_CHARS[byte % 62];
|
if (byte < 248 && code.length < 10) code += CODE_CHARS[byte % 62];
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The code is all both sides share. It gives the tracker swarm, identified by a 20 byte info_hash,
|
// The code is all both sides share. It gives the tracker swarm, identified by a 20 byte info_hash,
|
||||||
// and the key that proves the connection, see prove. Whoever sees the swarms, a tracker or anyone
|
// and the key that proves the connection, see prove. Whoever sees the swarms, a tracker or anyone
|
||||||
// scraping one, can test a guessed code against all of them at once. PBKDF2 makes every guess cost
|
// scraping one, can test a guessed code against all of them at once. PBKDF2 makes every guess cost
|
||||||
// what a share pays once (~50 ms), so a GPU gets through a vanishing part of the 62⁸ codes within
|
// what a share pays once (~50 ms). The salt is fixed, both sides only know the code, so the guesses
|
||||||
// TRACKER_WAIT. The salt is fixed, both sides only know the code. HKDF splits the result, the public
|
// can also be made ahead of time, once for every share: a table from info_hash to code would let a
|
||||||
// info_hash tells nothing about the keys: one seals the signaling, see seal, one proves the connection.
|
// tracker take over any share as it happens. Only the code's length prices that: for 62¹⁰ codes
|
||||||
|
// it's over a million GPU years. Any length derives the same way, an older client takes a longer
|
||||||
|
// code. HKDF splits the result, the public info_hash tells nothing about the keys: one seals the
|
||||||
|
// signaling, see seal, one proves the connection.
|
||||||
async function secrets(code: string) {
|
async function secrets(code: string) {
|
||||||
const text = new TextEncoder();
|
const text = new TextEncoder();
|
||||||
const password = await crypto.subtle.importKey('raw', text.encode(code), 'PBKDF2', false, ['deriveBits']);
|
const password = await crypto.subtle.importKey('raw', text.encode(code), 'PBKDF2', false, ['deriveBits']);
|
||||||
|
|||||||
Reference in new issue
Block a user