diff --git a/webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp b/webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp index d51b0997..66affad4 100644 --- a/webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp +++ b/webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp @@ -28,10 +28,10 @@ extern "C" { namespace { - // The source as it may be shown: a URL loses the user name and password - // it carries in front of its host, since error messages end up in logs. - // Anything that is not a URL with credentials is returned as it is. - std::string WithoutCredentials(const std::string & source) + // Error messages end up in logs. URLs expose only their scheme, host + // and port: credentials can also be carried in paths, queries or fragments. + // Local paths without a URL scheme remain useful for diagnosing file errors. + std::string SourceForError(const std::string & source) { const size_t scheme_end = source.find("://"); @@ -41,14 +41,12 @@ namespace const size_t authority_start = scheme_end + 3; const size_t authority_end = source.find_first_of("/?#", authority_start); - const size_t at = source.rfind('@', authority_end == std::string::npos - ? std::string::npos : authority_end - 1); + const std::string authority = source.substr(authority_start, + authority_end == std::string::npos ? std::string::npos : authority_end - authority_start); + const size_t at = authority.rfind('@'); - if (at == std::string::npos || at < authority_start) { - return source; - } - - return source.substr(0, authority_start) + source.substr(at + 1); + return source.substr(0, authority_start) + + (at == std::string::npos ? authority : authority.substr(at + 1)); } ffmpeg::MediaReader * ReaderOf(JNIEnv * env, jlong handle) @@ -99,7 +97,7 @@ JNIEXPORT jlong JNICALL Java_dev_onvoid_webrtc_media_player_MediaReader_open if (result < 0) { reader.reset(); - ffmpeg::ThrowIOException(env, "Opening '" + WithoutCredentials(url) + "' failed: " + ffmpeg::ErrorText(result)); + ffmpeg::ThrowIOException(env, "Opening '" + SourceForError(url) + "' failed: " + ffmpeg::ErrorText(result)); return 0; } diff --git a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaReaderTest.java b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaReaderTest.java index 1600540a..a0e22857 100644 --- a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaReaderTest.java +++ b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/MediaReaderTest.java @@ -141,6 +141,38 @@ void missingSourceFails() { assertTrue(e.getMessage().contains("no-such-file.webm"), e.getMessage()); } + @Test + void errorsExposeOnlyNetworkOrigin() { + String[][] sources = { + { "rtsp://user:pass@camera/live?token=secret", "rtsp://camera" }, + { "rtsp://camera:8554/path-key?token=secret#fragment-key", "rtsp://camera:8554" }, + { "rtsp://user:pass@camera:8554", "rtsp://camera:8554" }, + { "rtsp://camera?token=secret", "rtsp://camera" }, + { "rtsp://camera#fragment-key", "rtsp://camera" }, + { "rtsp://camera/path@secret", "rtsp://camera" }, + { "rtsp://user:pass@[::1]:8554/path-key?token=secret", "rtsp://[::1]:8554" }, + { "rtsp://camera:8554", "rtsp://camera:8554" }, + { "rtsp:///path-key?token=secret", "rtsp://" } + }; + + for (String[] source : sources) { + // Fail before FFmpeg opens the URL: these invented addresses must + // never require a camera, a DNS lookup or a network timeout. + AllocationFault.failNextOption(); + + try { + IOException e = assertThrows(IOException.class, () -> new MediaReader(source[0])); + + assertFalse(AllocationFault.isArmed()); + assertTrue(e.getMessage().startsWith("Opening '" + source[1] + "' failed: "), + e.getMessage()); + } + finally { + AllocationFault.disarm(); + } + } + } + @Test void opensPathBeyondBasicPlane(@TempDir Path dir) throws Exception { // FFmpeg takes UTF-8, which is not the modified UTF-8 of JNI for diff --git a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/RtspStreamTest.java b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/RtspStreamTest.java index f813a588..fcaedeb9 100644 --- a/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/RtspStreamTest.java +++ b/webrtc-media/src/test/java/dev/onvoid/webrtc/media/player/RtspStreamTest.java @@ -142,15 +142,20 @@ void shortTimeoutTimesOut() throws Exception { @Test void errorHidesCredentials() throws Exception { try (TestRtspServer server = new TestRtspServer(TestRtspServer.Behavior.SILENT, 0)) { - String url = server.url().replace("rtsp://", "rtsp://admin:s3cret@"); + String url = server.url().replace("rtsp://", "rtsp://admin:s3cret@") + + "/path-key?token=query-key#fragment-key"; IOException e = assertThrows(IOException.class, () -> new MediaReader(url, Duration.ofMillis(300))); - // Messages end up in logs; the password must not. + // Messages end up in logs; only the network origin may remain. assertFalse(e.getMessage().contains("s3cret"), e.getMessage()); assertFalse(e.getMessage().contains("admin"), e.getMessage()); - assertTrue(e.getMessage().contains("127.0.0.1:" + server.port() + "/stream"), + assertFalse(e.getMessage().contains("/stream"), e.getMessage()); + assertFalse(e.getMessage().contains("path-key"), e.getMessage()); + assertFalse(e.getMessage().contains("query-key"), e.getMessage()); + assertFalse(e.getMessage().contains("fragment-key"), e.getMessage()); + assertTrue(e.getMessage().startsWith("Opening 'rtsp://127.0.0.1:" + server.port() + "' failed: "), e.getMessage()); } }