Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 10 additions & 12 deletions webrtc-media/src/main/cpp/src/JNI_MediaReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("://");

Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Loading