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:
Sampson authored and GitHub committed 2026-06-24 07:58:51 -05:00
1 parent feeab77f13
commit e3f77ced06
5 files changed
+56 -3

No files matched your search

+25
View File
@@ -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;
}