Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(web-search): respect HTML context and bound CI image build

Alfredo Lopez6e9a42052ac8
diff --git a/.github/workflows/ci-public.yml b/.github/workflows/ci-public.yml
index 3344ced35..d9cf71612 100644
--- a/.github/workflows/ci-public.yml
+++ b/.github/workflows/ci-public.yml
@@ -155,8 +155,10 @@ jobs:
working-directory: mcp-servers/web-search
timeout-minutes: 10
run: |
- docker build -t issue198-ci-image:test .
- web_search_image_id="$(docker image inspect issue198-ci-image:test --format '{{.Id}}')"
+ node ../../scripts/minikube/run-with-deadline.mjs --timeout-seconds 300 --heartbeat-seconds 20 --kill-grace-seconds 5 --label web-search-ci-image-build -- \
+ docker build -t issue198-ci-image:test .
+ web_search_image_id="$(node ../../scripts/minikube/run-with-deadline.mjs --timeout-seconds 10 --heartbeat-seconds 5 --kill-grace-seconds 5 --label web-search-ci-image-inspect -- \
+ docker image inspect issue198-ci-image:test --format '{{.Id}}')"
node test/run-network.mjs --image "$web_search_image_id"
# These node:test suites live outside service packages and resolve the MCP
diff --git a/mcp-servers/web-search/README.md b/mcp-servers/web-search/README.md
index 7943a487d..a2337260e 100644
--- a/mcp-servers/web-search/README.md
+++ b/mcp-servers/web-search/README.md
@@ -57,7 +57,10 @@ it is not the download limit. Redirect and error bodies are destroyed without
reading them. Encodings supported: identity, gzip, deflate and br.
Errors expose a stable code without upstream details. Private destinations and
-oversized documents previously accepted are intentionally rejected.
+oversized documents previously accepted are intentionally rejected. Text extraction
+ignores comments and non-page prose in script, style, textarea and similar
+containers. It is a bounded lightweight scan, not a browser DOM; HTML entities
+remain verbatim.
### Regression tests
diff --git a/mcp-servers/web-search/package-lock.json b/mcp-servers/web-search/package-lock.json
index d4962bcdf..d855ce239 100644
--- a/mcp-servers/web-search/package-lock.json
+++ b/mcp-servers/web-search/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "@clerum/web-search-mcp",
- "version": "1.0.14",
+ "version": "1.0.15",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@clerum/web-search-mcp",
- "version": "1.0.14",
+ "version": "1.0.15",
"dependencies": {
"@modelcontextprotocol/sdk": "^1.12.1",
"express": "^4.21.2"
diff --git a/mcp-servers/web-search/package.json b/mcp-servers/web-search/package.json
index 5e0e8b118..ef70b12c7 100644
--- a/mcp-servers/web-search/package.json
+++ b/mcp-servers/web-search/package.json
@@ -1,6 +1,6 @@
{
"name": "@clerum/web-search-mcp",
- "version": "1.0.14",
+ "version": "1.0.15",
"type": "module",
"main": "dist/index.js",
"scripts": {
diff --git a/mcp-servers/web-search/src/fetchPage.ts b/mcp-servers/web-search/src/fetchPage.ts
index c18c0ba19..5191d7db5 100644
--- a/mcp-servers/web-search/src/fetchPage.ts
+++ b/mcp-servers/web-search/src/fetchPage.ts
@@ -17,39 +17,96 @@ export const FETCH_PAGE_LIMITS = Object.freeze({
let active = 0 // Process-wide, including distinct MCP sessions.
const redirects = new Set([301, 302, 303, 307, 308])
-/** Linear tag stripping, including malformed input with many unclosed '<'. */
-export function pageText(html: string): string {
+// HTML tag names are ASCII-case-insensitive. This copy preserves offsets into
+// the original text, including when Unicode characters precede a tag.
+function asciiLower(html: string): string {
+ return html.replace(/[A-Z]/g, character => character.toLowerCase())
+}
+
+function tagEnd(html: string, start: number): number {
+ let quote = ''
+ for (let cursor = start + 1; cursor < html.length; cursor++) {
+ const character = html[cursor]
+ if (quote) {
+ if (character === quote) quote = ''
+ } else if (character === '"' || character === "'") {
+ quote = character
+ } else if (character === '>') {
+ return cursor
+ }
+ }
+ return -1
+}
+
+function closingTag(html: string, lower: string, name: string, from: number) {
+ const prefix = `</${name}`
+ let start = lower.indexOf(prefix, from)
+ while (start !== -1) {
+ if (/[\s>]/.test(lower[start + prefix.length] ?? '')) {
+ const end = tagEnd(html, start)
+ return end === -1 ? undefined : { start, end }
+ }
+ start = lower.indexOf(prefix, start + prefix.length)
+ }
+ return undefined
+}
+
+const inertText = new Set(['script', 'style', 'textarea', 'xmp', 'iframe', 'noembed', 'noframes'])
+
+/** Extract visible page prose and the first real title in linear time. */
+function scanPage(html: string): { title: string; content: string } {
+ const lower = asciiLower(html)
const pieces: string[] = []
+ let title = ''
+ let foundTitle = false
let offset = 0
while (offset < html.length) {
- const start = html.indexOf('<', offset)
+ const start = lower.indexOf('<', offset)
if (start === -1) {
pieces.push(html.slice(offset))
break
}
- const end = html.indexOf('>', start + 1)
+ pieces.push(html.slice(offset, start))
+ if (lower.startsWith('<!--', start)) {
+ const end = lower.indexOf('-->', start + 4)
+ if (end === -1) break
+ pieces.push(' ')
+ offset = end + 3
+ continue
+ }
+ const end = tagEnd(html, start)
if (end === -1) {
- pieces.push(html.slice(offset))
+ pieces.push(html.slice(start))
break
}
- pieces.push(html.slice(offset, start), ' ')
+ const match = /^<\/?([a-z][a-z0-9:-]*)(?=[\s/>])/.exec(lower.slice(start, end + 1))
+ if (!match && lower[start + 1] !== '!' && lower[start + 1] !== '?') {
+ pieces.push('<')
+ offset = start + 1
+ continue
+ }
+ pieces.push(' ')
offset = end + 1
+ if (!match || lower[start + 1] === '/') continue
+ const name = match[1]
+ if (name === 'plaintext') break
+ if (name !== 'title' && !inertText.has(name)) continue
+ const close = closingTag(html, lower, name, offset)
+ if (!close) break
+ if (name === 'title') {
+ if (!foundTitle) {
+ title = html.slice(offset, close.start).replace(/\s+/g, ' ').trim()
+ foundTitle = true
+ }
+ pieces.push(html.slice(offset, close.start), ' ')
+ }
+ offset = close.end + 1
}
- return pieces.join('').replace(/\s+/g, ' ').trim()
+ return { title, content: pieces.join('').replace(/\s+/g, ' ').trim() }
}
-function titleText(html: string): string {
- // HTML tag names are ASCII-case-insensitive. Unicode lowercasing can expand
- // characters and invalidate offsets subsequently applied to the original HTML.
- const lower = html.replace(/[A-Z]/g, character => character.toLowerCase())
- let start = lower.indexOf('<title')
- while (start !== -1 && !/[\s>]/.test(lower[start + 6] ?? '')) {
- start = lower.indexOf('<title', start + 6)
- }
- if (start === -1) return ''
- const body = lower.indexOf('>', start + 6)
- const end = lower.indexOf('</title>', body + 1)
- return body < 0 || end < 0 ? '' : pageText(html.slice(body + 1, end))
+export function pageText(html: string): string {
+ return scanPage(html).content
}
function connect(url: URL, address: string, signal: AbortSignal): Promise<IncomingMessage> {
@@ -190,9 +247,10 @@ export async function fetchPage(
throw new FetchPageError('upstream_failure')
const html = await readBody(res, controller.signal)
controller.signal.throwIfAborted()
+ const page = scanPage(html)
return {
- title: titleText(html).slice(0, maxChars),
- content: pageText(html).slice(0, maxChars),
+ title: page.title.slice(0, maxChars),
+ content: page.content.slice(0, maxChars),
}
} finally {
res.destroy()
diff --git a/mcp-servers/web-search/test/fetchPage.test.mjs b/mcp-servers/web-search/test/fetchPage.test.mjs
index 8e59befe4..461bab43c 100644
--- a/mcp-servers/web-search/test/fetchPage.test.mjs
+++ b/mcp-servers/web-search/test/fetchPage.test.mjs
@@ -212,3 +212,44 @@ test('comment-like text in an attribute does not hide the real title', async t =
upstream(t, [{ body: '<meta name="description" content="<!--"><title>Actual</title>' }])
assert.equal((await fetchPage('http://8.8.8.8', 100)).title, 'Actual')
})
+
+test('comments and inert raw text do not supply a title or page prose', async t => {
+ upstream(t, [
+ {
+ body: '<!-- <title>Decoy</title> --><title>Actual</title><script>hiddenMarker()</script><style>.hidden{display:none}</style><p>Visible</p>',
+ },
+ ])
+ assert.deepEqual(await fetchPage('http://8.8.8.8', 100), {
+ title: 'Actual',
+ content: 'Actual Visible',
+ })
+})
+
+test('quoted comment delimiters and textarea markup preserve the real title', async t => {
+ upstream(t, [
+ {
+ body: '<meta content="<!--"><textarea><title>Decoy</title></textarea><TITLE>Actual</TITLE><p>Visible</p>',
+ },
+ ])
+ assert.deepEqual(await fetchPage('http://8.8.8.8', 100), {
+ title: 'Actual',
+ content: 'Actual Visible',
+ })
+})
+
+test('an unclosed HTML comment cannot supply a title', async t => {
+ upstream(t, [{ body: '<!-- <title>Decoy</title>' }])
+ assert.deepEqual(await fetchPage('http://8.8.8.8', 100), { title: '', content: '' })
+})
+
+test('quoted greater-than and partial raw closing tags do not reveal inert text', async t => {
+ upstream(t, [
+ {
+ body: '<meta content="a>b <!--"><script>discard</scriptx>still-discard</script><title>Actual</title><p>Visible</p>',
+ },
+ ])
+ assert.deepEqual(await fetchPage('http://8.8.8.8', 100), {
+ title: 'Actual',
+ content: 'Actual Visible',
+ })
+})
diff --git a/mcp-servers/web-search/test/mcp.network.mjs b/mcp-servers/web-search/test/mcp.network.mjs
index db850a9e0..45a9a1d34 100644
--- a/mcp-servers/web-search/test/mcp.network.mjs
+++ b/mcp-servers/web-search/test/mcp.network.mjs
@@ -43,6 +43,12 @@ const upstream = http.createServer((req, res) => {
res.end()
return
}
+ if (req.url === '/html-context') {
+ res.end(
+ '<!-- <title>Decoy</title> --><title>Actual</title><script>hiddenMarker()</script><p>Visible</p>'
+ )
+ return
+ }
if (req.url === '/oversize') {
res.writeHead(200)
res.end('x'.repeat(1024 * 1024 + 1))
@@ -137,6 +143,15 @@ test('the real connector is listed and returns the real fixture content', async
content: 'Network fixture issue198-real-result',
})
})
+test('real MCP extraction ignores inert HTML context', async () => {
+ const result = await call('http://11.198.0.2:8080/html-context')
+ assert.notEqual(result.isError, true)
+ assert.deepEqual(JSON.parse(result.content[0].text), {
+ title: 'Actual',
+ content: 'Actual Visible',
+ })
+})
+
test('direct and redirected private requests never touch the trap', async () => {
assert.equal(errorCode(await call('http://127.0.0.1:8081/trap')), 'destination_blocked')
assert.equal(errorCode(await call('http://11.198.0.2:8080/redirect')), 'destination_blocked')