@arcblock/ws 1.32.11 → 1.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -168,25 +168,41 @@ const isTrustedProxySocket = (remoteAddress, trustedProxies) => {
168
168
  if (!remoteAddress) return false;
169
169
  return trustedProxies.some((rule) => isIpInCidr(remoteAddress, rule));
170
170
  };
171
+ const isValidIpLiteral = (ip) => {
172
+ if (!ip || ip.length > 45) return false;
173
+ return parseIpv4ToUint32(ip) !== null || parseIpv6ToBigInt(ip) !== null;
174
+ };
171
175
  const resolveClientIpFromHeaders = (headers, trustProxyHops, trustedIpHeader) => {
172
- if (trustedIpHeader) {
173
- const rawCustom = headers[trustedIpHeader.toLowerCase()];
174
- const customIp = normalizeIpAddress(Array.isArray(rawCustom) ? rawCustom[0] : rawCustom);
175
- if (customIp) return customIp;
176
+ if (trustedIpHeader !== void 0) {
177
+ const configuredHeaders = trustedIpHeader.split(",").map((name) => name.trim().toLowerCase()).filter(Boolean);
178
+ if (configuredHeaders.length === 0 || configuredHeaders.length === 1 && configuredHeaders[0] === "none") return;
179
+ for (const hdrName of configuredHeaders) {
180
+ const rawCustom = headers[hdrName];
181
+ if (rawCustom === void 0) continue;
182
+ if (Array.isArray(rawCustom)) {
183
+ if (rawCustom.length === 0) continue;
184
+ if (rawCustom.length > 1) return void 0;
185
+ }
186
+ const trimmed = typeof rawCustom === "string" ? rawCustom.trim() : rawCustom[0]?.trim() ?? "";
187
+ if (!trimmed) continue;
188
+ const customIp = normalizeIpAddress(trimmed);
189
+ return customIp && isValidIpLiteral(customIp) ? customIp : void 0;
190
+ }
191
+ return;
176
192
  }
177
193
  const rawXff = headers["x-forwarded-for"];
178
194
  const xffStr = Array.isArray(rawXff) ? rawXff.join(",") : rawXff;
179
195
  if (typeof xffStr === "string" && xffStr.trim()) {
180
- const hops = xffStr.split(",").map((part) => normalizeIpAddress(part)).filter(Boolean);
196
+ const hops = xffStr.split(",").map((part) => normalizeIpAddress(part)).filter((part) => part && isValidIpLiteral(part));
181
197
  if (hops.length > 0) return hops[Math.max(0, hops.length - Math.max(1, trustProxyHops))];
182
198
  }
183
199
  if (trustProxyHops <= 1) {
184
200
  const rawCfIp = headers["cf-connecting-ip"];
185
201
  const cfIp = normalizeIpAddress(Array.isArray(rawCfIp) ? rawCfIp[0] : rawCfIp);
186
- if (cfIp) return cfIp;
202
+ if (cfIp && isValidIpLiteral(cfIp)) return cfIp;
187
203
  const rawRealIp = headers["x-real-ip"];
188
204
  const realIp = normalizeIpAddress(Array.isArray(rawRealIp) ? rawRealIp[0] : rawRealIp);
189
- if (realIp) return realIp;
205
+ if (realIp && isValidIpLiteral(realIp)) return realIp;
190
206
  }
191
207
  };
192
208
  /**
@@ -222,7 +238,7 @@ var WsServer = class extends EventEmitter {
222
238
  this.trustProxyHops = opts.trustProxyHops ?? (typeof opts.trustProxy === "number" && opts.trustProxy > 0 ? Math.floor(opts.trustProxy) : parseEnvPositiveInt(wsEnv.WS_TRUST_PROXY_HOPS, 1));
223
239
  const envTrustedProxies = wsEnv.WS_TRUSTED_PROXIES?.split(",").map((s) => s.trim()).filter(Boolean);
224
240
  this.trustedProxies = opts.trustedProxies ?? (envTrustedProxies && envTrustedProxies.length > 0 ? envTrustedProxies : DEFAULT_TRUSTED_PROXIES);
225
- this.trustedIpHeader = opts.trustedIpHeader ?? (wsEnv.WS_TRUSTED_IP_HEADER?.trim() || void 0);
241
+ this.trustedIpHeader = opts.trustedIpHeader ?? (wsEnv.WS_TRUSTED_IP_HEADER !== void 0 ? wsEnv.WS_TRUSTED_IP_HEADER.trim() : void 0);
226
242
  this.sanitizeErrors = opts.sanitizeErrors ?? false;
227
243
  this.wss = new WebSocketServer({
228
244
  noServer: true,
@@ -173,25 +173,41 @@ const isTrustedProxySocket = (remoteAddress, trustedProxies) => {
173
173
  if (!remoteAddress) return false;
174
174
  return trustedProxies.some((rule) => isIpInCidr(remoteAddress, rule));
175
175
  };
176
+ const isValidIpLiteral = (ip) => {
177
+ if (!ip || ip.length > 45) return false;
178
+ return parseIpv4ToUint32(ip) !== null || parseIpv6ToBigInt(ip) !== null;
179
+ };
176
180
  const resolveClientIpFromHeaders = (headers, trustProxyHops, trustedIpHeader) => {
177
- if (trustedIpHeader) {
178
- const rawCustom = headers[trustedIpHeader.toLowerCase()];
179
- const customIp = normalizeIpAddress(Array.isArray(rawCustom) ? rawCustom[0] : rawCustom);
180
- if (customIp) return customIp;
181
+ if (trustedIpHeader !== void 0) {
182
+ const configuredHeaders = trustedIpHeader.split(",").map((name) => name.trim().toLowerCase()).filter(Boolean);
183
+ if (configuredHeaders.length === 0 || configuredHeaders.length === 1 && configuredHeaders[0] === "none") return;
184
+ for (const hdrName of configuredHeaders) {
185
+ const rawCustom = headers[hdrName];
186
+ if (rawCustom === void 0) continue;
187
+ if (Array.isArray(rawCustom)) {
188
+ if (rawCustom.length === 0) continue;
189
+ if (rawCustom.length > 1) return void 0;
190
+ }
191
+ const trimmed = typeof rawCustom === "string" ? rawCustom.trim() : rawCustom[0]?.trim() ?? "";
192
+ if (!trimmed) continue;
193
+ const customIp = normalizeIpAddress(trimmed);
194
+ return customIp && isValidIpLiteral(customIp) ? customIp : void 0;
195
+ }
196
+ return;
181
197
  }
182
198
  const rawXff = headers["x-forwarded-for"];
183
199
  const xffStr = Array.isArray(rawXff) ? rawXff.join(",") : rawXff;
184
200
  if (typeof xffStr === "string" && xffStr.trim()) {
185
- const hops = xffStr.split(",").map((part) => normalizeIpAddress(part)).filter(Boolean);
201
+ const hops = xffStr.split(",").map((part) => normalizeIpAddress(part)).filter((part) => part && isValidIpLiteral(part));
186
202
  if (hops.length > 0) return hops[Math.max(0, hops.length - Math.max(1, trustProxyHops))];
187
203
  }
188
204
  if (trustProxyHops <= 1) {
189
205
  const rawCfIp = headers["cf-connecting-ip"];
190
206
  const cfIp = normalizeIpAddress(Array.isArray(rawCfIp) ? rawCfIp[0] : rawCfIp);
191
- if (cfIp) return cfIp;
207
+ if (cfIp && isValidIpLiteral(cfIp)) return cfIp;
192
208
  const rawRealIp = headers["x-real-ip"];
193
209
  const realIp = normalizeIpAddress(Array.isArray(rawRealIp) ? rawRealIp[0] : rawRealIp);
194
- if (realIp) return realIp;
210
+ if (realIp && isValidIpLiteral(realIp)) return realIp;
195
211
  }
196
212
  };
197
213
  /**
@@ -227,7 +243,7 @@ var WsServer = class extends node_events.default {
227
243
  this.trustProxyHops = opts.trustProxyHops ?? (typeof opts.trustProxy === "number" && opts.trustProxy > 0 ? Math.floor(opts.trustProxy) : parseEnvPositiveInt(wsEnv.WS_TRUST_PROXY_HOPS, 1));
228
244
  const envTrustedProxies = wsEnv.WS_TRUSTED_PROXIES?.split(",").map((s) => s.trim()).filter(Boolean);
229
245
  this.trustedProxies = opts.trustedProxies ?? (envTrustedProxies && envTrustedProxies.length > 0 ? envTrustedProxies : DEFAULT_TRUSTED_PROXIES);
230
- this.trustedIpHeader = opts.trustedIpHeader ?? (wsEnv.WS_TRUSTED_IP_HEADER?.trim() || void 0);
246
+ this.trustedIpHeader = opts.trustedIpHeader ?? (wsEnv.WS_TRUSTED_IP_HEADER !== void 0 ? wsEnv.WS_TRUSTED_IP_HEADER.trim() : void 0);
231
247
  this.sanitizeErrors = opts.sanitizeErrors ?? false;
232
248
  this.wss = new ws.WebSocketServer({
233
249
  noServer: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ws",
3
- "version": "1.32.11",
3
+ "version": "1.33.0",
4
4
  "description": "OCAP Chain websocket server and client",
5
5
  "keywords": [
6
6
  "websocket"
@@ -75,7 +75,7 @@
75
75
  "url": "https://github.com/ArcBlock/blockchain/issues"
76
76
  },
77
77
  "dependencies": {
78
- "@arcblock/event-hub": "1.32.11",
78
+ "@arcblock/event-hub": "1.33.0",
79
79
  "debug": "^4.4.3",
80
80
  "eventemitter3": "^4.0.7",
81
81
  "lodash": "^4.17.23",