grpc / grpc/grpc-java

Android: ProxyDetectorImpl crashes when DefaultProxySelector contains an invalid proxy port

Aperta Adatta ai principianti
#13,052 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
Java
Stelle
12.1k
Fork
4k
Merge medio
2g 17h
PR unite (30g)
37

Descrizione

### What version of gRPC-Java are you using?

`1.66.0`

[Source inspection](https://github.com/grpc/grpc-java/blob/v1.84.x/core/src/main/java/io/grpc/internal/ProxyDetectorImpl.java#L203-L209) indicates that latest `1.84.0` is also affected.

### What is your environment?

Android production application.

Most events are from Android 13 devices (89%), predominantly Xiaomi (85%).

### What did you expect to see?

A malformed system proxy configuration should not cause an uncaught exception on the gRPC resolver executor.

Because the configured proxy cannot be used, gRPC should preferably treat it as no usable proxy and continue with a direct connection. At minimum, it should convert the exception into an ordinary name-resolution failure instead of terminating the process.

### What did you see instead?

Android’s `DefaultProxySelector` throws `IllegalArgumentException` while constructing a proxy address with an out-of-range port. The exception escapes through `ProxyDetectorImpl` and becomes a fatal process crash.

```text
Fatal Exception: java.lang.IllegalArgumentException: port out of range:899858473
at java.net.InetSocketAddress.checkPort(InetSocketAddress.java:154)
at java.net.InetSocketAddress.createUnresolved(InetSocketAddress.java:279)
at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:315)
at sun.net.spi.DefaultProxySelector$1.run(DefaultProxySelector.java:219)
at java.security.AccessController.doPrivileged(AccessController.java:46)
at sun.net.spi.DefaultProxySelector.select(DefaultProxySelector.java:218)
at io.grpc.internal.ProxyDetectorImpl.detectProxy(ProxyDetectorImpl.java:230)
at io.grpc.internal.ProxyDetectorImpl.proxyFor(ProxyDetectorImpl.java:200)
at io.grpc.internal.DnsNameResolver.detectProxy(DnsNameResolver.java:269)
at io.grpc.internal.DnsNameResolver.access$600(DnsNameResolver.java:66)
at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1100)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:1572)
```

We observed multiple affected devices with different invalid ports. Each invalid value remains stable for its affected device. This suggests malformed device/system proxy state rather than corruption inside gRPC.

### Analysis

On Android 13, `DefaultProxySelector.select()` [reads the process proxy properties and passes the configured port directly](https://android.googlesource.com/platform/libcore/+/refs/tags/android-13.0.0_r1/ojluni/src/main/java/sun/net/spi/DefaultProxySelector.java#294) to `InetSocketAddress.createUnresolved()`.

`InetSocketAddress` rejects ports outside `0..65535`.

The invalid value is the system proxy port, not the gRPC destination port. Although gRPC does not produce the malformed configuration, `ProxyDetectorImpl` currently allows the platform exception to escape from the resolver task.

The existing handling for a `ProxySelector` returning `null` or an empty list does not cover this case because the exception is thrown inside `ProxySelector.select()`.

### Comparable OkHttp behavior

OkHttp’s current implementation [handles this exact case](https://github.com/lysine-dev/okhttp/blob/parent-5.5.0/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/connection/RouteSelector.kt#L108-L117) by catching `IllegalArgumentException` from `ProxySelector.select()` and treating it as no usable proxy, falling back to `Proxy.NO_PROXY`.

Its source explicitly explains the reason:

```kotlin
// A misconfigured system proxy (such as one with no port set) can make
// ProxySelector.select() itself throw IllegalArgumentException; treat that
// as "no usable proxy" rather than letting it crash.
```

Applying equivalent handling in gRPC would provide consistent behavior between regular OkHttp traffic and the gRPC OkHttp transport.

### Reproduction

We have not reproduced the original malformed Android proxy state locally.

A synthetic reproduction should be possible by configuring:

```text
https.proxyHost=
https.proxyPort=
```

and then triggering name resolution through a gRPC channel using the default proxy detector.

### Suggested change

Catch `IllegalArgumentException` around `ProxySelector.select()` and treat the result as no proxy:

```java
final List proxies;
try {
proxies = proxySelector.select(uri);
} catch (IllegalArgumentException e) {
log.log(
Level.WARNING,
"ProxySelector failed while selecting a proxy; proceeding without proxy",
e);
return null;
}
```

Returning `null` from `ProxyDetectorImpl.detectProxy()` preserves connectivity through a direct connection and matches OkHttp’s behavior for malformed system proxy configuration.

If silently falling back to direct connectivity is considered inappropriate, converting the exception to `IOException` would still prevent a fatal process crash, although the channel would remain unavailable while the malformed proxy configuration persists.

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start in core/src/main/java/io/grpc/internal/ProxyDetectorImpl.java around detectProxy and the lines identified by the source inspection, then trace how DnsNameResolver invokes it. Reproduce the case with an out-of-range https.proxyPort and trigger name resolution. Done means an IllegalArgumentException from ProxySelector.select() no longer crashes the resolver and malformed proxy state falls back to direct connectivity.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, java
Ambito
backend-api-design, networking
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
78/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.