Skip to content

Commit

Permalink
feat: add validation of port name
Browse files Browse the repository at this point in the history
  • Loading branch information
iomekam committed Apr 27, 2024
1 parent 7e69bed commit a7993a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 18 additions & 18 deletions packages/network/src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ export function getPrefixes(addr) {
return ret;
}

/**
* Validate IBC port name
* @param {string} specifiedName
*/
function throwIfInvalidPortName(specifiedName) {
// Contains between 2 and 128 characters
// Can contain alphanumeric characters
// Valid symbols: ., ,, _, +, -, #, [, ], <, >
const portNameRegex = new RegExp('^[a-zA-Z0-9.,_+\\-#<>\\[\\]]{2,128}$');
if (!portNameRegex.test(specifiedName)) {
throw new Error(`Invalid IBC port name: ${specifiedName}`);
}
}

/**
* @typedef {object} ConnectionOpts
* @property {Endpoint[]} addrs
Expand Down Expand Up @@ -1448,17 +1462,10 @@ export const preparePortAllocator = (zone, { watch }) =>
{
allocateIBCPort(specifiedName = '') {
const { state } = this;
let localAddr = `/ibc-port/${specifiedName}`;
let localAddr = `/ibc-port/`;

if (specifiedName) {
// Contains at least two characters and only allows valid characters specified in IBC spec
const match = specifiedName.match(
new RegExp('^[a-zA-Z0-9.,_+\\-#<>\\[\\]]{2,128}$'),
);

if (!match) {
throw new Error(`Invalid IBC port name: ${specifiedName}`);
}
throwIfInvalidPortName(specifiedName);

localAddr = `/ibc-port/custom-${specifiedName}`;
}
Expand All @@ -1478,17 +1485,10 @@ export const preparePortAllocator = (zone, { watch }) =>
allocateLocalPort(specifiedName = '') {
const { state } = this;

let localAddr = `/local/${specifiedName}`;
let localAddr = `/local/`;

if (specifiedName) {
// Contains at least two characters and only allows valid characters specified in IBC spec
const match = specifiedName.match(
new RegExp('^[a-zA-Z0-9.,_+\\-#<>\\[\\]]{2,128}$'),
);

if (!match) {
throw new Error(`Invalid IBC port name: ${specifiedName}`);
}
throwIfInvalidPortName(specifiedName);

localAddr = `/local/custom-${specifiedName}`;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/network/test/test-network-misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ test('verify port allocation', async t => {

const namedLocalPort = await when(portAllocator.allocateLocalPort('local-1'));
t.is(namedLocalPort.getLocalAddress(), '/local/custom-local-1');

await t.throwsAsync(when(portAllocator.allocateIBCPort('/test-1')), {
message: 'Invalid IBC port name: /test-1',
});
});

test('protocol connection listen', async t => {
Expand Down

0 comments on commit a7993a8

Please sign in to comment.