Skip to content

Commit bef938b

Browse files
authored
Limit media source URLs in errors to their network origin (#553)
1 parent 5e760e9 commit bef938b

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

‎webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ extern "C" {
2828

2929
namespace
3030
{
31-
// The source as it may be shown: a URL loses the user name and password
32-
// it carries in front of its host, since error messages end up in logs.
33-
// Anything that is not a URL with credentials is returned as it is.
34-
std::string WithoutCredentials(const std::string & source)
31+
// Error messages end up in logs. URLs expose only their scheme, host
32+
// and port: credentials can also be carried in paths, queries or fragments.
33+
// Local paths without a URL scheme remain useful for diagnosing file errors.
34+
std::string SourceForError(const std::string & source)
3535
{
3636
const size_t scheme_end = source.find("://");
3737

@@ -41,14 +41,12 @@ namespace
4141

4242
const size_t authority_start = scheme_end + 3;
4343
const size_t authority_end = source.find_first_of("/?#", authority_start);
44-
const size_t at = source.rfind('@', authority_end == std::string::npos
45-
? std::string::npos : authority_end - 1);
44+
const std::string authority = source.substr(authority_start,
45+
authority_end == std::string::npos ? std::string::npos : authority_end - authority_start);
46+
const size_t at = authority.rfind('@');
4647

47-
if (at == std::string::npos || at < authority_start) {
48-
return source;
49-
}
50-
51-
return source.substr(0, authority_start) + source.substr(at + 1);
48+
return source.substr(0, authority_start)
49+
+ (at == std::string::npos ? authority : authority.substr(at + 1));
5250
}
5351

5452
ffmpeg::MediaReader * ReaderOf(JNIEnv * env, jlong handle)
@@ -99,7 +97,7 @@ JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaReader_open
9997
if (result < 0) {
10098
reader.reset();
10199

102-
ffmpeg::ThrowIOException(env, "Opening '" + WithoutCredentials(url) + "' failed: " + ffmpeg::ErrorText(result));
100+
ffmpeg::ThrowIOException(env, "Opening '" + SourceForError(url) + "' failed: " + ffmpeg::ErrorText(result));
103101

104102
return 0;
105103
}

‎webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaReaderTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ void missingSourceFails() {
141141
assertTrue(e.getMessage().contains("no-such-file.webm"), e.getMessage());
142142
}
143143

144+
@Test
145+
void errorsExposeOnlyNetworkOrigin() {
146+
String[][] sources = {
147+
{ "rtsp://user:pass@camera/live?token=secret", "rtsp://camera" },
148+
{ "rtsp://camera:8554/path-key?token=secret#fragment-key", "rtsp://camera:8554" },
149+
{ "rtsp://user:pass@camera:8554", "rtsp://camera:8554" },
150+
{ "rtsp://camera?token=secret", "rtsp://camera" },
151+
{ "rtsp://camera#fragment-key", "rtsp://camera" },
152+
{ "rtsp://camera/path@secret", "rtsp://camera" },
153+
{ "rtsp://user:pass@[::1]:8554/path-key?token=secret", "rtsp://[::1]:8554" },
154+
{ "rtsp://camera:8554", "rtsp://camera:8554" },
155+
{ "rtsp:///path-key?token=secret", "rtsp://" }
156+
};
157+
158+
for (String[] source : sources) {
159+
// Fail before FFmpeg opens the URL: these invented addresses must
160+
// never require a camera, a DNS lookup or a network timeout.
161+
AllocationFault.failNextOption();
162+
163+
try {
164+
IOException e = assertThrows(IOException.class, () -> new MediaReader(source[0]));
165+
166+
assertFalse(AllocationFault.isArmed());
167+
assertTrue(e.getMessage().startsWith("Opening '" + source[1] + "' failed: "),
168+
e.getMessage());
169+
}
170+
finally {
171+
AllocationFault.disarm();
172+
}
173+
}
174+
}
175+
144176
@Test
145177
void opensPathBeyondBasicPlane(@TempDir Path dir) throws Exception {
146178
// FFmpeg takes UTF-8, which is not the modified UTF-8 of JNI for

‎webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/RtspStreamTest.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,20 @@ void shortTimeoutTimesOut() throws Exception {
142142
@Test
143143
void errorHidesCredentials() throws Exception {
144144
try (TestRtspServer server = new TestRtspServer(TestRtspServer.Behavior.SILENT, 0)) {
145-
String url = server.url().replace("rtsp://", "rtsp://admin:s3cret@");
145+
String url = server.url().replace("rtsp://", "rtsp://admin:s3cret@")
146+
+ "/path-key?token=query-key#fragment-key";
146147

147148
IOException e = assertThrows(IOException.class,
148149
() -> new MediaReader(url, Duration.ofMillis(300)));
149150

150-
// Messages end up in logs; the password must not.
151+
// Messages end up in logs; only the network origin may remain.
151152
assertFalse(e.getMessage().contains("s3cret"), e.getMessage());
152153
assertFalse(e.getMessage().contains("admin"), e.getMessage());
153-
assertTrue(e.getMessage().contains("127.0.0.1:" + server.port() + "/stream"),
154+
assertFalse(e.getMessage().contains("/stream"), e.getMessage());
155+
assertFalse(e.getMessage().contains("path-key"), e.getMessage());
156+
assertFalse(e.getMessage().contains("query-key"), e.getMessage());
157+
assertFalse(e.getMessage().contains("fragment-key"), e.getMessage());
158+
assertTrue(e.getMessage().startsWith("Opening 'rtsp://127.0.0.1:" + server.port() + "' failed: "),
154159
e.getMessage());
155160
}
156161
}

0 commit comments

Comments
 (0)