Skip to content

Commit 2fa8861

Browse files
committed
define no_proxy cases in hostMatchesNoProxyList
1 parent ef510d9 commit 2fa8861

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

Diff for: packages/grpc-js/src/http_proxy.ts

+22-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { log } from './logging';
1919
import { LogVerbosity } from './constants';
20-
import { Socket } from 'net';
20+
import { isIPv4, Socket } from 'net';
2121
import * as http from 'http';
2222
import * as logging from './logging';
2323
import {
@@ -129,17 +129,20 @@ interface CIDRNotation {
129129
* 1. ip
130130
* 2. prefixLength
131131
*/
132-
const CIDR_REGEX = /^([0-9.]+)(?:\/([0-9]+))?$/;
133132

134133
export function parseCIDR(cidrString: string): CIDRNotation | null {
135-
const parsedCIDR = CIDR_REGEX.exec(cidrString);
136-
if (parsedCIDR && parsedCIDR.length === 3) {
137-
return {
138-
ip: ipToInt(parsedCIDR[1]),
139-
prefixLength: parseInt(parsedCIDR[2] ?? '32', 10),
140-
};
141-
}
142-
return null;
134+
const splitRange = cidrString.split('/');
135+
if (splitRange.length !== 2) {
136+
return null;
137+
}
138+
const prefixLength = parseInt(splitRange[1], 10);
139+
if (!isIPv4(splitRange[0]) || Number.isNaN(prefixLength) || prefixLength < 0 || prefixLength > 32) {
140+
return null;
141+
}
142+
return {
143+
ip: ipToInt(splitRange[0]),
144+
prefixLength: prefixLength
145+
};
143146
}
144147

145148
function ipToInt(ip: string) {
@@ -157,8 +160,15 @@ function isIpInCIDR(cidr: CIDRNotation, serverHost: string) {
157160
function hostMatchesNoProxyList(serverHost: string): boolean {
158161
for (const host of getNoProxyHostList()) {
159162
const parsedCIDR = parseCIDR(host);
160-
// host is a single IP address or a CIDR notation or a domain
161-
if (parsedCIDR && isIpInCIDR(parsedCIDR, serverHost) || serverHost.endsWith(host)) {
163+
// host is a single IP or a domain name suffix
164+
if (!parsedCIDR) {
165+
if (host === serverHost || serverHost.includes(host)) {
166+
trace('Not using proxy for target in no_proxy list: ' + serverHost);
167+
return true;
168+
}
169+
}
170+
// host is a CIDR or a domain
171+
else if (isIpInCIDR(parsedCIDR, serverHost)) {
162172
trace('Not using proxy for target in no_proxy list: ' + serverHost);
163173
return true;
164174
}

0 commit comments

Comments
 (0)