summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Amith Yamasani <yamasani@google.com> 2022-05-19 17:16:05 +0000
committer Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> 2022-05-19 17:16:05 +0000
commit5e40d2efd99825167879969be1cd92097486e8fe (patch)
treee769f3025ea2f42917f7d84fbf7e5c9841c3e409
parentd8b5b700446939f8e969e5f37ee62c3b3a2a51d9 (diff)
parentb6da699d30fe8aa3ee8bf719862b22ae281c0365 (diff)
Merge "Sanitize Uri.toSafeString() some more." into tm-dev am: b6da699d30
Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/18282431 Change-Id: I8ba6027190c51298e7b1be47b536a789bf9df625 Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r--core/java/android/net/Uri.java39
-rw-r--r--core/tests/coretests/src/android/net/UriTest.java8
2 files changed, 24 insertions, 23 deletions
diff --git a/core/java/android/net/Uri.java b/core/java/android/net/Uri.java
index d71faee4cc8d..3da696ad0bc7 100644
--- a/core/java/android/net/Uri.java
+++ b/core/java/android/net/Uri.java
@@ -390,7 +390,8 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
* Return a string representation of this URI that has common forms of PII redacted,
* making it safer to use for logging purposes. For example, {@code tel:800-466-4411} is
* returned as {@code tel:xxx-xxx-xxxx} and {@code http://example.com/path/to/item/} is
- * returned as {@code http://example.com/...}.
+ * returned as {@code http://example.com/...}. For all other uri schemes, only the scheme,
+ * host and port are returned.
* @return the common forms PII redacted string of this URI
* @hide
*/
@@ -398,13 +399,14 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
public @NonNull String toSafeString() {
String scheme = getScheme();
String ssp = getSchemeSpecificPart();
+ StringBuilder builder = new StringBuilder(64);
+
if (scheme != null) {
+ builder.append(scheme);
+ builder.append(":");
if (scheme.equalsIgnoreCase("tel") || scheme.equalsIgnoreCase("sip")
|| scheme.equalsIgnoreCase("sms") || scheme.equalsIgnoreCase("smsto")
|| scheme.equalsIgnoreCase("mailto") || scheme.equalsIgnoreCase("nfc")) {
- StringBuilder builder = new StringBuilder(64);
- builder.append(scheme);
- builder.append(':');
if (ssp != null) {
for (int i=0; i<ssp.length(); i++) {
char c = ssp.charAt(i);
@@ -415,25 +417,20 @@ public abstract class Uri implements Parcelable, Comparable<Uri> {
}
}
}
- return builder.toString();
- } else if (scheme.equalsIgnoreCase("http") || scheme.equalsIgnoreCase("https")
- || scheme.equalsIgnoreCase("ftp") || scheme.equalsIgnoreCase("rtsp")) {
- ssp = "//" + ((getHost() != null) ? getHost() : "")
- + ((getPort() != -1) ? (":" + getPort()) : "")
- + "/...";
+ } else {
+ // For other schemes, let's be conservative about
+ // the data we include -- only the host and port, not the query params, path or
+ // fragment, because those can often have sensitive info.
+ final String host = getHost();
+ final int port = getPort();
+ final String path = getPath();
+ final String authority = getAuthority();
+ if (authority != null) builder.append("//");
+ if (host != null) builder.append(host);
+ if (port != -1) builder.append(":").append(port);
+ if (authority != null || path != null) builder.append("/...");
}
}
- // Not a sensitive scheme, but let's still be conservative about
- // the data we include -- only the ssp, not the query params or
- // fragment, because those can often have sensitive info.
- StringBuilder builder = new StringBuilder(64);
- if (scheme != null) {
- builder.append(scheme);
- builder.append(':');
- }
- if (ssp != null) {
- builder.append(ssp);
- }
return builder.toString();
}
diff --git a/core/tests/coretests/src/android/net/UriTest.java b/core/tests/coretests/src/android/net/UriTest.java
index 3733bfa586d1..89632a46267e 100644
--- a/core/tests/coretests/src/android/net/UriTest.java
+++ b/core/tests/coretests/src/android/net/UriTest.java
@@ -989,10 +989,14 @@ public class UriTest extends TestCase {
checkToSafeString("ftp://ftp.android.com:2121/...",
"ftp://root:love@ftp.android.com:2121/");
- checkToSafeString("unsupported://ajkakjah/askdha/secret?secret",
+ checkToSafeString("unsupported://ajkakjah/...",
"unsupported://ajkakjah/askdha/secret?secret");
- checkToSafeString("unsupported:ajkakjah/askdha/secret?secret",
+ checkToSafeString("unsupported:",
"unsupported:ajkakjah/askdha/secret?secret");
+ checkToSafeString("unsupported:/...",
+ "unsupported:/ajkakjah/askdha/secret?secret");
+ checkToSafeString("file:///...",
+ "file:///path/to/secret.doc");
}
private void checkToSafeString(String expectedSafeString, String original) {