From 0bfd155c2f9720346025c7a288652a63e08eadb1 Mon Sep 17 00:00:00 2001 From: Dr_rOot Date: Sat, 19 Sep 2026 16:57:02 +0800 Subject: [PATCH] fix(ssdp): accept standard header tokens and prepare 0.1.3 --- package.json | 2 +- src/clients/upnp-client.test.ts | 42 +++++++++++++++ src/codecs/ssdp-codec.test.ts | 95 +++++++++++++++++++++++++++++++-- src/codecs/ssdp-codec.ts | 7 ++- 4 files changed, 138 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 453845a..96a9b6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@motrix/nat", - "version": "0.1.2", + "version": "0.1.3", "description": "Dependency-free UPnP IGD, NAT-PMP, PCP, and STUN for Node.js", "license": "MIT", "author": "Motrix", diff --git a/src/clients/upnp-client.test.ts b/src/clients/upnp-client.test.ts index df52d0c..25869f0 100644 --- a/src/clients/upnp-client.test.ts +++ b/src/clients/upnp-client.test.ts @@ -156,6 +156,48 @@ describe('UpnpClient.discover', () => { } }) + it('discovers Huawei HiLink with its vendor header and PPP service', async () => { + http.history + .expect({ + method: 'GET', + host: '192.168.3.1', + port: 37215, + path: '/router/upnpdev.xml', + }) + .reply({ + statusCode: 200, + body: VALID_DEVICE_XML.replaceAll( + 'WANIPConnection:1', + 'WANPPPConnection:1' + ), + }) + const pending = client.discover({ timeoutMs: 100 }) + await tick() + const response = buildSsdpResponse( + 'http://192.168.3.1:37215/router/upnpdev.xml' + ) + .toString('ascii') + .replace( + 'SERVER: test', + 'SERVER: Linux UPnP/1.0 Huawei-ATP-IGD\r\nHILINK_EXT: 0' + ) + udpFactory.sockets[0]!.emitMessage(Buffer.from(response, 'ascii'), { + address: '192.168.3.1', + port: 1900, + size: Buffer.byteLength(response), + }) + expect(await pending).toMatchObject({ + ok: true, + value: { + gatewayIp: '192.168.3.1', + controlHost: '192.168.3.1', + controlPort: 37215, + serviceType: 'urn:schemas-upnp-org:service:WANPPPConnection:1', + }, + }) + expect(http.history.calls).toHaveLength(1) + }) + it('returns discovered gateway on valid response', async () => { http.history .expect({ diff --git a/src/codecs/ssdp-codec.test.ts b/src/codecs/ssdp-codec.test.ts index 573559a..7a36096 100644 --- a/src/codecs/ssdp-codec.test.ts +++ b/src/codecs/ssdp-codec.test.ts @@ -90,6 +90,96 @@ describe('parseMSearchResponse happy path', () => { }) }) +describe('SSDP extension header names', () => { + it('accepts the Huawei HiLink discovery response', () => { + const result = parseMSearchResponse( + buildSsdpResponse({ + LOCATION: 'http://192.168.3.1:37215/router/upnpdev.xml', + SERVER: 'Linux UPnP/1.0 Huawei-ATP-IGD', + HILINK_EXT: '0', + EXT: '', + }) + ) + expect(result).toMatchObject({ + ok: true, + value: { + server: 'Linux UPnP/1.0 Huawei-ATP-IGD', + endpoint: { + host: '192.168.3.1', + port: 37215, + path: '/router/upnpdev.xml', + }, + }, + }) + }) + + it.each([ + '!', + '#', + '$', + '%', + '&', + "'", + '*', + '+', + '-', + '.', + '^', + '_', + '`', + '|', + '~', + ])( + 'accepts RFC 9110 token punctuation %s in extension headers', + (character) => { + expect( + parseMSearchResponse(buildSsdpResponse({ [`X${character}EXT`]: '0' })) + .ok + ).toBe(true) + } + ) + + it.each([ + ' ', + '\t', + '/', + '(', + ')', + '[', + ']', + '{', + '}', + '<', + '>', + '@', + ',', + ';', + '=', + '?', + '"', + '\\', + ])('rejects header-name whitespace or delimiter %j', (character) => { + expect( + parseMSearchResponse(buildSsdpResponse({ [`X${character}EXT`]: '0' })) + ).toMatchObject({ + ok: false, + error: NatErrorCode.ParseError, + detail: 'invalid header name', + }) + }) + + it('continues rejecting public LOCATION addresses with a valid vendor header', () => { + expect( + parseMSearchResponse( + buildSsdpResponse({ + HILINK_EXT: '0', + LOCATION: 'http://8.8.8.8/upnpdev.xml', + }) + ) + ).toMatchObject({ ok: false, error: NatErrorCode.SecurityViolation }) + }) +}) + describe('parseMSearchResponse security', () => { it('rejects response exceeding max size', () => { const huge = Buffer.alloc(5000, 0x20) @@ -174,9 +264,8 @@ describe('parseMSearchResponse additional branches', () => { }) it('rejects header name with invalid characters', () => { - const text = 'HTTP/1.1 200 OK\r\nX_INVALID: value\r\n\r\n' - const r = parseMSearchResponse(Buffer.from(text, 'ascii')) - expect(r.ok).toBe(false) + const r = parseMSearchResponse(buildSsdpResponse({ 'X/INVALID': 'value' })) + expect(r).toMatchObject({ ok: false, detail: 'invalid header name' }) }) it('rejects header value exceeding max length', () => { diff --git a/src/codecs/ssdp-codec.ts b/src/codecs/ssdp-codec.ts index bb1e1bd..86003bd 100644 --- a/src/codecs/ssdp-codec.ts +++ b/src/codecs/ssdp-codec.ts @@ -96,10 +96,9 @@ export function parseMSearchResponse(raw: Buffer): ParseResult { if (value.length > SSDP_HEADER_VALUE_MAX) { return parseErr(NatErrorCode.SecurityViolation, 'header value too long') } - // Dot is required: UPnP 1.1 (UDA §1.2.2) mandates BOOTID.UPNP.ORG / - // CONFIGID.UPNP.ORG on every advertisement, and '.' is a legal RFC 9110 - // token character — without it every spec-compliant IGD is rejected. - if (!/^[A-Za-z0-9.-]+$/.test(name)) { + // HTTP field names use RFC 9110 §5.6.2 tchar, including vendor headers + // such as Huawei's HILINK_EXT and UPnP's BOOTID.UPNP.ORG. + if (!/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/.test(name)) { return parseErr(NatErrorCode.ParseError, 'invalid header name') } headers[name] = value