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
@@ -62,7 +62,7 @@ Searches for videos with comprehensive metadata and thumbnail information.
|
||||
- `freshness` (string, optional): Time filter ("pd", "pw", "pm", "py", or date range)
|
||||
|
||||
### Image Search (`brave_image_search`)
|
||||
Searches for images with automatic fetching and base64 encoding for direct display.
|
||||
Searches for images with metadata including URLs, dimensions, and confidence scores.
|
||||
|
||||
**Parameters:**
|
||||
- `query` (string, required): Search terms (max 400 chars, 50 words)
|
||||
@@ -171,7 +171,7 @@ The server supports the following environment variables:
|
||||
|
||||
- `BRAVE_API_KEY`: Your Brave Search API key (required)
|
||||
- `BRAVE_MCP_TRANSPORT`: Transport mode ("http" or "stdio", default: "stdio")
|
||||
- `BRAVE_MCP_PORT`: HTTP server port (default: 8000)
|
||||
- `BRAVE_MCP_PORT`: HTTP server port (default: 8080)
|
||||
- `BRAVE_MCP_HOST`: HTTP server host (default: "0.0.0.0")
|
||||
- `BRAVE_MCP_LOG_LEVEL`: Desired logging level("debug", "info", "notice", "warning", "error", "critical", "alert", or "emergency", default: "info")
|
||||
- `BRAVE_MCP_ENABLED_TOOLS`: When used, specifies a space-separated whitelist for supported tools
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
"watch": "tsc --watch",
|
||||
"format": "prettier --write \"src/**/*.ts\"",
|
||||
"format:check": "prettier --check \"src/**/*.ts\"",
|
||||
"test": "node --import tsx --test \"src/**/*.test.ts\"",
|
||||
"inspector": "npx @modelcontextprotocol/inspector",
|
||||
"inspector:http": "npx @modelcontextprotocol/inspector --transport http"
|
||||
},
|
||||
|
||||
+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