Skip to content

Commit 55f7dbc

Browse files
authored
fix: keep secrets of WHIP and WHEP URLs out of failures (#538)
* fix: keep secrets of WHIP and WHEP URLs out of failures Failures name a URL by its scheme, host and port only, and no longer quote Location values or JDK messages, which carry the URL or the Authorization value. * fix: keep the password of an ICE server out of its description RTCIceServer.toString() masks the password and no longer shows hashCode(), which is computed from it. The description reaches messages such as the one for ICE servers WebRTC refuses. * test: use a random password in the ICE server test
1 parent 61799cf commit 55f7dbc

8 files changed

Lines changed: 391 additions & 39 deletions

File tree

‎docs/guide/client-api.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,8 @@ When the connection fails, or stays disconnected for two seconds, the session re
747747

748748
The timeout bounds the requests as a whole, not each read, so a server that sends its answer a byte at a time cannot stretch it. A request still out when the session times out or is cancelled is cut off at once; one still waiting for the head of its answer only from Java 16 on, and before that at its timeout. Once the server has named the session, in the head of its answer, it is deleted all the same; a session the server created for an answer that had not come yet is left for the server to time out.
749749

750+
Failures and reports name the URL of an endpoint or session by its scheme, host and port only, since the rest may be a secret: services take stream keys and signatures in the query, and some, such as Cloudflare Stream, in the path. A failure can go to a log as it is, apart from what the endpoint itself says in an error, which it passes on unchanged.
751+
750752
`session.close()` deletes the session on the server, which frees what the server holds for it, and closes the peer. Both start at once, so a slow server does not keep the peer open, and the deletion takes at most the session's timeout. The tracks a WHIP session published stay open. `session.peer()` is a regular peer, for stats and anything else. A deletion the server refuses or that fails goes to the runtime's error handler, and also fails the future of `closeAsync()`, which completes once both are done; a peer that did not close fails it too.
751753

752754
`session.extensions()` gives the protocol extensions the endpoint offers for the session (RFC 9725, 4.9): the URL of each by its relation type, such as `urn:ietf:params:whip:ext:example:server-sent-events` (`urn:ietf:params:whep:ext:...` for WHEP), as the endpoint named them in `Link` header fields of its answer, resolved against it. Of a relation type named twice, the first counts. Links of other relation types are ignored, as WHIP and WHEP ask, apart from `ice-server`. The session uses none of the extensions itself; an application uses those it knows, and the map is empty if the endpoint named none.

‎webrtc/src/main/java/dev/onvoid/webrtc/RTCIceServer.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,15 @@ public boolean equals(Object obj) {
110110
Objects.equals(username, other.username);
111111
}
112112

113+
/**
114+
* Describes the server for logs and messages, so without the password,
115+
* and without {@link #hashCode()}, which is computed from it.
116+
*/
113117
@Override
114118
public String toString() {
115119
return String.format("%s@%d [urls=%s, username=%s, password=%s, tlsCertPolicy=%s, hostname=%s, tlsAlpnProtocols=%s, tlsEllipticCurves=%s]",
116-
RTCIceServer.class.getSimpleName(), hashCode(),
117-
urls, username, password, tlsCertPolicy, hostname,
120+
RTCIceServer.class.getSimpleName(), System.identityHashCode(this),
121+
urls, username, password != null ? "***" : null, tlsCertPolicy, hostname,
118122
tlsAlpnProtocols, tlsEllipticCurves);
119123
}
120124
}

‎webrtc/src/main/java/dev/onvoid/webrtc/client/HttpCall.java‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ CompletableFuture<Response> send(String contentType, byte[] content, int maxBody
276276
void abort() {
277277
IOException reason = remainingNanos() <= 0
278278
? timedOut()
279-
: new IOException("The request to " + uri + " was aborted");
279+
: new IOException("The request to " + redacted(uri) + " was aborted");
280280
CompletableFuture<?> exchange;
281281
Body body;
282282
Future<?> retry;
@@ -337,8 +337,9 @@ private void exchange(URI target, String contentType, byte[] content, int maxBod
337337
request = builder.build();
338338
}
339339
catch (IllegalArgumentException e) {
340-
result.completeExceptionally(new IOException("The request to " + target + " is invalid: "
341-
+ e.getMessage(), e));
340+
// Without the message or the exception itself, which quote the URL
341+
// or the value of a header field, such as the Authorization.
342+
result.completeExceptionally(new IOException("The request to " + redacted(target) + " is invalid"));
342343
return;
343344
}
344345

@@ -374,7 +375,7 @@ private void exchange(URI target, String contentType, byte[] content, int maxBod
374375
return;
375376
}
376377
if (redirects == MAX_REDIRECTS) {
377-
result.completeExceptionally(new IOException("The request to " + uri
378+
result.completeExceptionally(new IOException("The request to " + redacted(uri)
378379
+ " was redirected more than " + MAX_REDIRECTS + " times"));
379380
return;
380381
}
@@ -407,7 +408,7 @@ private HttpResponse.BodySubscriber<String> head(HttpResponse.ResponseInfo info,
407408

408409
synchronized (this) {
409410
if (aborted) {
410-
body.stop(new IOException("The request to " + uri + " was aborted"));
411+
body.stop(new IOException("The request to " + redacted(uri) + " was aborted"));
411412
return body;
412413
}
413414

@@ -505,19 +506,19 @@ private static URI redirectTarget(URI from, String location) throws IOException
505506
target = from.resolve(location.trim());
506507
}
507508
catch (IllegalArgumentException e) {
508-
throw new IOException("The server redirected to an invalid URL: " + location);
509+
throw new IOException("The server redirected to an invalid URL");
509510
}
510511

511512
String scheme = target.getScheme();
512513

513514
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
514-
throw new IOException("The server redirected to a URL that is not http: " + target);
515+
throw new IOException("The server redirected to a URL that is not http: " + redacted(target));
515516
}
516517
if ("https".equalsIgnoreCase(from.getScheme()) && !"https".equalsIgnoreCase(scheme)) {
517-
throw new IOException("The server redirected to a URL without TLS: " + target);
518+
throw new IOException("The server redirected to a URL without TLS: " + redacted(target));
518519
}
519520
if (!"https".equalsIgnoreCase(scheme) && !isLoopback(target.getHost())) {
520-
throw new IOException("The server redirected to a URL without TLS: " + target);
521+
throw new IOException("The server redirected to a URL without TLS: " + redacted(target));
521522
}
522523

523524
return target;
@@ -557,12 +558,36 @@ static boolean isLoopback(String host) {
557558
}
558559
}
559560

561+
/**
562+
* Names a URL in a message, by its scheme, host and port only, since what
563+
* a message says ends up in logs. The rest of a URL may be a secret, and
564+
* not only its user info and query: Cloudflare Stream, for one, has the
565+
* key to publish with as a segment of the path.
566+
*
567+
* @param uri the URL.
568+
*
569+
* @return the URL without what may be a secret, such as {@code
570+
* https://example.com:8443}.
571+
*/
572+
static String redacted(URI uri) {
573+
String scheme = uri.getScheme() != null ? uri.getScheme() + ":" : "";
574+
String authority = uri.getRawAuthority();
575+
576+
if (authority == null) {
577+
return scheme.isEmpty() ? "a URL without a scheme" : scheme;
578+
}
579+
580+
// The raw authority, since a host that is not a valid one, as with an
581+
// underscore, leaves getHost() and getUserInfo() null.
582+
return scheme + "//" + authority.substring(authority.lastIndexOf('@') + 1);
583+
}
584+
560585
private long remainingNanos() {
561586
return limit - (System.nanoTime() - start);
562587
}
563588

564589
private HttpTimeoutException timedOut() {
565-
return new HttpTimeoutException("The request to " + uri + " did not complete within " + timeout);
590+
return new HttpTimeoutException("The request to " + redacted(uri) + " did not complete within " + timeout);
566591
}
567592

568593
private static Throwable unwrap(Throwable error) {

‎webrtc/src/main/java/dev/onvoid/webrtc/client/HttpIce.java‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package dev.onvoid.webrtc.client;
1818

19+
import static dev.onvoid.webrtc.client.HttpCall.redacted;
20+
1921
import java.net.URI;
2022
import java.net.http.HttpClient;
2123
import java.nio.charset.StandardCharsets;
@@ -273,25 +275,25 @@ private UnaryOperator<String> answered(HttpCall request, HttpCall.Response respo
273275
}
274276

275277
if (error != null) {
276-
throw new WebRtcException("Restarting ICE of the session " + target + " failed: " + error.getMessage(),
278+
throw new WebRtcException("Restarting ICE of the session " + redacted(target) + " failed: " + error.getMessage(),
277279
error);
278280
}
279281
if (UNSUPPORTED.contains(response.status)) {
280282
synchronized (this) {
281283
restartsRefused = true;
282284
}
283285

284-
throw response.error("The session " + target + " takes no ICE restarts: " + response.describe());
286+
throw response.error("The session " + redacted(target) + " takes no ICE restarts: " + response.describe());
285287
}
286288
if (response.status / 100 != 2) {
287-
throw response.error("The session " + target + " answered the ICE restart with " + response.describe());
289+
throw response.error("The session " + redacted(target) + " answered the ICE restart with " + response.describe());
288290
}
289291

290292
HttpSession.requireContentType(response, FRAGMENT_TYPE);
291293
Fragment remote = Fragment.parse(response.body);
292294

293295
if (remote.ufrag == null || remote.pwd == null) {
294-
throw new WebRtcException("The session " + target + " answered the ICE restart without "
296+
throw new WebRtcException("The session " + redacted(target) + " answered the ICE restart without "
295297
+ "its new ICE credentials");
296298
}
297299

@@ -391,10 +393,10 @@ private void flush() {
391393
// An error status is the cause, for its status.
392394
Throwable cause = error != null
393395
? error
394-
: response.error("The session " + target + " answered the candidates with "
396+
: response.error("The session " + redacted(target) + " answered the candidates with "
395397
+ response.describe());
396398

397-
rtc.reportError(new WebRtcException("Sending ICE candidates to the session " + target
399+
rtc.reportError(new WebRtcException("Sending ICE candidates to the session " + redacted(target)
398400
+ " failed, and no more are sent: " + (error != null
399401
? error.getMessage()
400402
: response.describe()), cause));
@@ -491,7 +493,7 @@ private void restartByItself() {
491493
}
492494

493495
if (report) {
494-
rtc.reportError(new WebRtcException("Restarting ICE of the session " + target
496+
rtc.reportError(new WebRtcException("Restarting ICE of the session " + redacted(target)
495497
+ " after the connection broke failed", PeerImpl.unwrap(error)));
496498
}
497499
});

‎webrtc/src/main/java/dev/onvoid/webrtc/client/HttpSession.java‎

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package dev.onvoid.webrtc.client;
1818

19+
import static dev.onvoid.webrtc.client.HttpCall.redacted;
1920
import static java.util.Objects.requireNonNull;
2021

2122
import java.net.URI;
@@ -292,18 +293,18 @@ private static CompletableFuture<URI> negotiate(WebRtc rtc, PeerImpl peer, URI e
292293
// as the timeout again.
293294
peer.close();
294295
result.completeExceptionally(PeerImpl.asWebRtcException(error,
295-
"The session with " + endpoint + " could not be set up"));
296+
"The session with " + redacted(endpoint) + " could not be set up"));
296297

297298
discard(rtc, resource.getAndSet(null), authorization, timeout);
298299
};
299300

300301
ScheduledFuture<?> deadline = rtc.timer.schedule(() -> fail.accept(new WebRtcException(
301-
"The session with " + endpoint + " was not connected within " + timeout)),
302+
"The session with " + redacted(endpoint) + " was not connected within " + timeout)),
302303
Timeouts.toMillis(timeout), TimeUnit.MILLISECONDS);
303304

304305
abandoned.thenRun(() -> {
305306
deadline.cancel(false);
306-
fail.accept(new CancellationException("The session with " + endpoint + " was abandoned"));
307+
fail.accept(new CancellationException("The session with " + redacted(endpoint) + " was abandoned"));
307308
});
308309

309310
CompletableFuture<RTCSessionDescription> initial = prepared.thenCompose(ready -> gathering == Gathering.COMPLETE
@@ -385,12 +386,12 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
385386
throw (WebRtcException) error;
386387
}
387388
if (error != null) {
388-
throw new WebRtcException("Sending the offer to " + endpoint + " failed: " + error.getMessage(),
389+
throw new WebRtcException("Sending the offer to " + redacted(endpoint) + " failed: " + error.getMessage(),
389390
error);
390391
}
391392
if (response.status == 201) {
392393
if (resource.get() == null) {
393-
throw new WebRtcException("The endpoint " + endpoint + " answered without a session Location");
394+
throw new WebRtcException("The endpoint " + redacted(endpoint) + " answered without a session Location");
394395
}
395396
requireContentType(response, "application/sdp");
396397
return new Reply(response.body, false, null, 0, response, protocol);
@@ -399,7 +400,7 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
399400
URI session = resourceOf(response.uri, response.header("Location"));
400401

401402
if (session == null) {
402-
throw new WebRtcException("The endpoint " + endpoint
403+
throw new WebRtcException("The endpoint " + redacted(endpoint)
403404
+ " countered the offer without naming the session");
404405
}
405406

@@ -408,7 +409,7 @@ private static CompletableFuture<Reply> post(HttpCall request, URI endpoint, Str
408409
protocol);
409410
}
410411

411-
throw response.error("The endpoint " + endpoint + " answered the offer with " + response.describe());
412+
throw response.error("The endpoint " + redacted(endpoint) + " answered the offer with " + response.describe());
412413
});
413414
}
414415

@@ -426,7 +427,7 @@ private static CompletableFuture<Void> answerCounterOffer(WebRtc rtc, HttpClient
426427
return peer.manual.answerInstead(new RTCSessionDescription(RTCSdpType.OFFER, reply.sdp), Gathering.COMPLETE)
427428
.thenCompose(answer -> {
428429
if (System.currentTimeMillis() > reply.validUntil) {
429-
throw new WebRtcException("The counter-offer for " + reply.session
430+
throw new WebRtcException("The counter-offer for " + redacted(reply.session)
430431
+ " expired before it was answered");
431432
}
432433

@@ -446,12 +447,12 @@ private static CompletableFuture<Void> answerCounterOffer(WebRtc rtc, HttpClient
446447
withRtcpMuxOnly(answer.sdp).getBytes(StandardCharsets.UTF_8), 0,
447448
head -> { }).handle((response, error) -> {
448449
if (error != null) {
449-
throw new WebRtcException("Sending the answer to " + reply.session + " failed: "
450+
throw new WebRtcException("Sending the answer to " + redacted(reply.session) + " failed: "
450451
+ error.getMessage(), error);
451452
}
452453
if (response.status / 100 != 2) {
453454
throw response.error("The endpoint answered the answer for "
454-
+ reply.session + " with " + response.describe());
455+
+ redacted(reply.session) + " with " + response.describe());
455456
}
456457

457458
ice.attach(reply.session, response.header("ETag"), answer.sdp);
@@ -610,14 +611,14 @@ private static CompletableFuture<WebRtcException> discard(WebRtc rtc, URI resour
610611
Throwable failure = error;
611612

612613
if (failure == null && response.status / 100 != 2) {
613-
failure = response.error("The endpoint answered the deletion of " + resource
614+
failure = response.error("The endpoint answered the deletion of " + redacted(resource)
614615
+ " with " + response.describe());
615616
}
616617
if (failure == null) {
617618
return null;
618619
}
619620

620-
WebRtcException reported = new WebRtcException("Deleting the session " + resource + " failed",
621+
WebRtcException reported = new WebRtcException("Deleting the session " + redacted(resource) + " failed",
621622
failure);
622623
rtc.reportError(reported);
623624
return reported;
@@ -659,7 +660,7 @@ static CompletableFuture<HttpCall.Response> head(WebRtc rtc, URI endpoint, Strin
659660
CompletableFuture<HttpCall.Response> response = request.send(null, null, 0, head -> { })
660661
.handle((value, error) -> {
661662
if (error != null) {
662-
throw new WebRtcException("Asking " + endpoint + " with HEAD failed: " + error.getMessage(),
663+
throw new WebRtcException("Asking " + redacted(endpoint) + " with HEAD failed: " + error.getMessage(),
663664
error);
664665
}
665666

@@ -688,7 +689,7 @@ private static WebRtcException leftOnServer(WebRtc rtc, URI resource) {
688689
return null;
689690
}
690691

691-
WebRtcException error = new WebRtcException("The session " + resource
692+
WebRtcException error = new WebRtcException("The session " + redacted(resource)
692693
+ " was left on the server, since the runtime is closed");
693694
rtc.reportError(error);
694695
return error;
@@ -710,19 +711,23 @@ static URI resourceOf(URI endpoint, String location) {
710711
resource = endpoint.resolve(location.trim());
711712
}
712713
catch (IllegalArgumentException e) {
713-
throw new WebRtcException("The endpoint " + endpoint + " named an invalid resource: " + location, e);
714+
// Without the exception, which quotes the Location.
715+
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named an invalid resource");
714716
}
715717

716718
String scheme = resource.getScheme();
717719

718720
if (!"http".equalsIgnoreCase(scheme) && !"https".equalsIgnoreCase(scheme)) {
719-
throw new WebRtcException("The endpoint " + endpoint + " named a resource that is not http: " + resource);
721+
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource that is not http: "
722+
+ redacted(resource));
720723
}
721724
if ("https".equalsIgnoreCase(endpoint.getScheme()) && !"https".equalsIgnoreCase(scheme)) {
722-
throw new WebRtcException("The endpoint " + endpoint + " named a resource without TLS: " + resource);
725+
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource without TLS: "
726+
+ redacted(resource));
723727
}
724728
if (!"https".equalsIgnoreCase(scheme) && !HttpCall.isLoopback(resource.getHost())) {
725-
throw new WebRtcException("The endpoint " + endpoint + " named a resource without TLS: " + resource);
729+
throw new WebRtcException("The endpoint " + redacted(endpoint) + " named a resource without TLS: "
730+
+ redacted(resource));
726731
}
727732

728733
return resource;
@@ -782,7 +787,7 @@ static String withRtcpMuxOnly(String sdp) {
782787
static void requireContentType(HttpCall.Response response, String expected) {
783788
String type = response.header("Content-Type");
784789
if (type == null || !type.split(";", 2)[0].trim().equalsIgnoreCase(expected)) {
785-
throw new WebRtcException("The session " + response.uri + " answered with Content-Type "
790+
throw new WebRtcException("The session " + redacted(response.uri) + " answered with Content-Type "
786791
+ type + "; expected " + expected);
787792
}
788793
}
@@ -888,7 +893,7 @@ static URI checkEndpoint(URI endpoint) {
888893

889894
if (!"https".equalsIgnoreCase(scheme) && !local) {
890895
throw new IllegalArgumentException("The endpoint must be an https URL, or an http one on this machine, "
891-
+ "such as http://localhost, got " + endpoint);
896+
+ "such as http://localhost, got " + redacted(endpoint));
892897
}
893898

894899
return endpoint;

0 commit comments

Comments
 (0)