Skip to content

Commit

Permalink
pool: stricter rules for custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
pinheadmz committed Nov 11, 2022
1 parent 5c925c4 commit 8b7ca1e
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions integration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions integration/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"name": "hnsd_integration_test",
"meta": "!! version must match hnsd version in Makefile.am !!",
"version": "1.0.0",
"scripts": {
"test": "bmocha --reporter spec test/*.js",
"test-file": "bmocha --reporter spec"
Expand Down
6 changes: 3 additions & 3 deletions integration/test-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ class TestUtil {
this.hnsd = spawn(
path.join(__dirname, '..', 'hnsd'),
this.hnsdArgs,
{stdio: 'ignore'}
{stdio: 'ignore'} // pro tip: switch to 'inherit' to see hnsd output
);

this.hnsd.on('spawn', () => resolve());
this.hnsd.on('error', () => reject());
this.hnsd.on('spawn', () => resolve(this.hnsd));
this.hnsd.on('error', e => reject(e));
});
}

Expand Down
66 changes: 66 additions & 0 deletions integration/test/config-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
/* eslint max-len: "off" */
/* eslint no-return-assign: "off" */

'use strict';

const assert = require('bsert');
const {version} = require('../package.json');

const TestUtil = require('../test-util');
const util = new TestUtil();

describe('Configuration', function() {
this.timeout(5000);

before(async () => {
await util.open();
});

after(async () => {
await util.close();
});

describe('--agent', function () {
it('should have default user agent', async () => {
await util.generate(1);
await util.waitForSync();

const peer = util.node.pool.peers.head();
assert.strictEqual(peer.agent, `/hnsd:${version}/`);
});

it('should fail to start with too-long agent', async () => {
const agent = 'x'.repeat(255 - `/hnsd:${version}/`.length);
const hnsd = await util.restartHNSD(['-a', agent]);
await new Promise((resolve) => {
hnsd.once('exit', (code, signal) => {
assert.strictEqual(code, 3); // HSK_EFAILURE
resolve();
});
});
});

it('should fail to start with backslash-containing agent', async () => {
const agent = 'beacon/browser/verifies/dane';
const hnsd = await util.restartHNSD(['-a', agent]);
await new Promise((resolve) => {
hnsd.once('exit', (code, signal) => {
assert.strictEqual(code, 3); // HSK_EFAILURE
resolve();
});
});
});

it('should have custom user agent', async () => {
const agent = 'x'.repeat(255 - `/hnsd:${version}/`.length - 1);
await util.restartHNSD(['-a', agent]);
await util.generate(1);
await util.waitForSync();

const peer = util.node.pool.peers.head();
assert.strictEqual(peer.agent, `/hnsd:${version}/${agent}/`);
});
});
});
7 changes: 6 additions & 1 deletion src/pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,19 @@ hsk_pool_set_agent(hsk_pool_t *pool, const char *user_agent) {
if (!user_agent)
return true;

if (strchr(user_agent, '/'))
return false;

size_t len = strlen(pool->user_agent);
len += strlen(user_agent);
len += 1; // terminal "/"

// Agent size in p2p version message is 1 byte
if (len > 0xff)
if (len > HSK_MAX_AGENT)
return false;

pool->user_agent = strcat(pool->user_agent, user_agent);
pool->user_agent = strcat(pool->user_agent, "/");

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#define HSK_STATE_READING 4
#define HSK_STATE_HANDSHAKE 5
#define HSK_STATE_DISCONNECTING 6
#define HSK_MAX_AGENT 255

/*
* Types
Expand Down Expand Up @@ -59,6 +60,7 @@ typedef struct hsk_peer_s {
hsk_brontide_t *brontide;
uint64_t id;
char host[HSK_MAX_HOST];
char agent[HSK_MAX_AGENT];
hsk_addr_t addr;
int state;
uint8_t read_buffer[HSK_BUFFER_SIZE];
Expand Down

0 comments on commit 8b7ca1e

Please sign in to comment.