fix: better error message in case of unexpected network address #624

Massimo Melina committed Jun 12, 2024 at 11:05 UTC 316a6c278ac43d8b52dbe837c5246a923780677f
4 files changed +12 -10
src/cross.ts
+1 -1
@@ -164,7 +164,7 @@ export function setHidden<T, ADD>(dest: T, src: ADD) {
164 }))) as T & ADD
165 }
166
167 -export function try_(cb: () => any, onException?: (e:any) => any) {
167 +export function try_<T,E=undefined>(cb: () => T, onException?: (e:any) => E) {
168 try {
169 return cb()
170 }
src/misc.ts
+9 -4
@@ -13,7 +13,7 @@ import { Readable, Transform } from 'stream'
13 import { SocketAddress, BlockList } from 'node:net'
14 import { ApiError } from './apiMiddleware'
15 import { HTTP_BAD_REQUEST, HTTP_METHOD_NOT_ALLOWED } from './const'
16 -import { isIpLocalHost, makeMatcher } from './cross'
16 +import { isIpLocalHost, makeMatcher, try_ } from './cross'
17 import { isIPv6 } from 'net'
18 import { statusCodeForMissingPerm, VfsNode } from './vfs'
19 import events from './events'
@@ -48,11 +48,15 @@ export function makeNetMatcher(mask: string, emptyMaskReturns=false) {
48 console.warn("error in network mask", x)
49 continue
50 }
51 - const address = parseAddress(m[1]!)
51 + const address = try_(() => parseAddress(m[1]!),
52 + () => console.error("invalid address " + m[1]))
53 + if (!address) continue
54 if (m[2])
53 - bl.addSubnet(address, Number(m[2]))
55 + try { bl.addSubnet(address, Number(m[2])) }
56 + catch { console.error("invalid net mask " + x) }
57 else if (m[3])
55 - bl.addRange(address, parseAddress(m[2]!))
58 + try { bl.addRange(address, parseAddress(m[2]!)) }
59 + catch { console.error("invalid address " + m[2]) }
60 else
61 bl.addAddress(address)
62 }
@@ -60,6 +64,7 @@ export function makeNetMatcher(mask: string, emptyMaskReturns=false) {
64 neg !== bl.check(parseAddress(ip))
65 }
66
67 +// can throw ERR_INVALID_ADDRESS
68 function parseAddress(s: string) {
69 return new SocketAddress({ address: s, family: isIPv6(s) ? 'ipv6' : 'ipv4' })
70 }
src/nat.ts
+1 -1
@@ -31,7 +31,7 @@ const originalMethod = upnpClient.getGateway
31 upnpClient.getGateway = debounceAsync(() => originalMethod.apply(upnpClient), 0, { retain: HOUR, retainFailure: 30_000 })
32 upnpClient.getGateway().then(res => {
33 console.debug('upnp', res.gateway.description)
34 -}, () => {})
34 +}, e => console.debug('upnp failed:', e.message || String(e)))
35
36 // poll external ip
37 repeat(10 * MINUTE, () => upnpClient.getPublicIp().then(v => {
src/util-files.ts
+1 -4
@@ -112,10 +112,7 @@ export async function unzip(stream: Readable, cb: (path: string) => Promisable<f
112 .on('entry', (entry: any) =>
113 pending = pending.then(async () => { // don't overlap writings
114 const { path, type } = entry
115 - const dest = await try_(() => cb(path), e => {
116 - console.warn(String(e))
117 - return false
118 - })
115 + const dest = await try_(() => cb(path), e => console.warn(String(e)))
116 if (!dest || type !== 'File')
117 return entry.autodrain()
118 console.debug('unzip', dest)