Uri to safe string

This commit is contained in:
M66B 2023-12-11 22:04:44 +01:00
parent c4a1384232
commit 894736ef18
3 changed files with 42 additions and 2 deletions

View File

@ -1751,7 +1751,7 @@ public class Helper {
String title = intent.getStringExtra(Intent.EXTRA_TITLE);
Uri data = intent.getData();
String type = intent.getType();
String fullName = (data == null ? intent.toString() : data.toString());
String fullName = (data == null ? intent.toString() : UriHelper.toSafeString(data));
String extension = (data == null ? null : getExtension(data.getLastPathSegment()));
tvName.setText(title == null ? fullName : title);

View File

@ -64,7 +64,7 @@ public class NoStreamException extends SecurityException {
TextView tvUri = dview.findViewById(R.id.tvUri);
ImageButton ibInfo = dview.findViewById(R.id.ibInfo);
tvUri.setText(uri == null ? null : uri.toString());
tvUri.setText(uri == null ? null : UriHelper.toSafeString(uri));
ibInfo.setOnClickListener(new View.OnClickListener() {
@Override

View File

@ -479,6 +479,46 @@ public class UriHelper {
return uri.getHost();
}
// Copied from android.net.Uri.toSafeString
public static String toSafeString(Uri uri) {
String scheme = uri.getScheme();
String ssp = uri.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")) {
if (ssp != null) {
for (int i = 0; i < ssp.length(); i++) {
char c = ssp.charAt(i);
if (c == '-' || c == '@' || c == '.') {
builder.append(c);
} else {
builder.append('x');
}
}
}
} 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 = uri.getHost();
final int port = uri.getPort();
final String path = uri.getPath();
final String authority = uri.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("/...");
}
}
return builder.toString();
}
static void test(Context context) {
String[] hosts = new String[]{
"child.parent.example.com", "parent.example.com", "example.com", "com",