fix: http port validation (#312)
* fix: validate and parse HTTP port as integer Commander leaves --port as a string, so invalid values like abc previously passed numeric range checks. Parse ports strictly before starting HTTP transport. * test: add parsePort unit tests and npm test script Introduce node:test coverage for HTTP port parsing edge cases using the existing tsx dev dependency. * docs: correct README port default and image search description Align BRAVE_MCP_PORT default with runtime behavior (8080) and remove the outdated v1.x base64 image search note.
This commit is contained in:
1 parent
feeab77f13
commit
e3f77ced06
5 files changed
+56
-3
No files matched your search
+4
-1
@@ -2,6 +2,7 @@ import { LoggingLevel, LoggingLevelSchema } from '@modelcontextprotocol/sdk/type
|
||||
import { Command } from 'commander';
|
||||
import dotenv from 'dotenv';
|
||||
import tools from './tools/index.js';
|
||||
import { parsePort } from './utils.js';
|
||||
|
||||
dotenv.config({ debug: false, quiet: true });
|
||||
|
||||
@@ -125,12 +126,14 @@ export function getOptions(): Configuration | false {
|
||||
}
|
||||
|
||||
if (options.transport === 'http') {
|
||||
if (options.port < 1 || options.port > 65535) {
|
||||
const port = parsePort(options.port);
|
||||
if (port === null) {
|
||||
console.error(
|
||||
`Invalid --port value: '${options.port}'. Must be a valid port number between 1 and 65535.`
|
||||
);
|
||||
return false;
|
||||
}
|
||||
options.port = port;
|
||||
|
||||
if (!options.host) {
|
||||
console.error('Error: --host is required');
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { describe, it } from 'node:test';
|
||||
import { parsePort } from './utils.js';
|
||||
|
||||
describe('parsePort', () => {
|
||||
it('accepts valid integer ports', () => {
|
||||
assert.equal(parsePort(8080), 8080);
|
||||
assert.equal(parsePort('8080'), 8080);
|
||||
assert.equal(parsePort(1), 1);
|
||||
assert.equal(parsePort(65535), 65535);
|
||||
});
|
||||
|
||||
it('rejects non-numeric and out-of-range values', () => {
|
||||
assert.equal(parsePort('abc'), null);
|
||||
assert.equal(parsePort('8080abc'), null);
|
||||
assert.equal(parsePort('8080.5'), null);
|
||||
assert.equal(parsePort(0), null);
|
||||
assert.equal(parsePort(65536), null);
|
||||
assert.equal(parsePort(-1), null);
|
||||
assert.equal(parsePort(''), null);
|
||||
assert.equal(parsePort(null), null);
|
||||
assert.equal(parsePort(undefined), null);
|
||||
});
|
||||
});
|
||||
@@ -22,3 +22,28 @@ export function checkRateLimit() {
|
||||
export function stringify(data: any, pretty = false) {
|
||||
return pretty ? JSON.stringify(data, null, 2) : JSON.stringify(data);
|
||||
}
|
||||
|
||||
export function parsePort(value: unknown): number | null {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (typeof value === 'number') {
|
||||
if (!Number.isInteger(value) || value < 1 || value > 65535) {
|
||||
return null;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
const text = String(value).trim();
|
||||
if (!/^\d+$/.test(text)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const parsed = Number.parseInt(text, 10);
|
||||
if (parsed < 1 || parsed > 65535) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parsed;
|
||||
}
|
||||
Reference in new issue
Block a user