Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
42 changes: 42 additions & 0 deletions src/clients/upnp-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
95 changes: 92 additions & 3 deletions src/codecs/ssdp-codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/codecs/ssdp-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,9 @@ export function parseMSearchResponse(raw: Buffer): ParseResult<SsdpResponse> {
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
Expand Down
Loading