Commit 646ba862 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Cronet: Fix static analysis warnings fround by newer ErrorProne

There is one non-trivial one which I've just suppressed here, where
CronetEngine.Builder.addPublicKeyPins() takes a Set<byte[]>.

Apparently byte[] does not implement hashCode() nor compare(), so they
should not be used as keys in maps/sets:
https://errorprone.info/bugpattern/ArrayAsKeyOfSetOrMap

Bug: 894616
Change-Id: I1feefa33a4d9a28f859b21ee0eeb051d969483cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1934314Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#719140}
parent a6b47782
...@@ -332,14 +332,14 @@ final class FakeUrlRequest extends UrlRequestBase { ...@@ -332,14 +332,14 @@ final class FakeUrlRequest extends UrlRequestBase {
transitionStates(State.STARTED, State.REDIRECT_RECEIVED); transitionStates(State.STARTED, State.REDIRECT_RECEIVED);
if (mUrlResponseInfo.getAllHeaders().get("location") == null) { if (mUrlResponseInfo.getAllHeaders().get("location") == null) {
// Response did not have a location header, so this request must fail. // Response did not have a location header, so this request must fail.
final String prevUrl = mCurrentUrl;
mUserExecutor.execute(new Runnable() { mUserExecutor.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
tryToFailWithException(new CronetExceptionImpl( tryToFailWithException(new CronetExceptionImpl(
"Request failed due to bad redirect HTTP headers", "Request failed due to bad redirect HTTP headers",
new IllegalStateException("Response recieved from URL: " + mCurrentUrl new IllegalStateException("Response recieved from URL: " + prevUrl
+ " was a " + " was a redirect, but lacked a location header.")));
+ "redirect, but lacked a location header.")));
} }
}); });
return; return;
......
...@@ -6,12 +6,12 @@ package org.chromium.net.impl; ...@@ -6,12 +6,12 @@ package org.chromium.net.impl;
import static android.os.Process.THREAD_PRIORITY_LOWEST; import static android.os.Process.THREAD_PRIORITY_LOWEST;
import android.content.Context; import android.content.Context;
import android.util.Base64;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import org.chromium.net.CronetEngine; import org.chromium.net.CronetEngine;
import org.chromium.net.CronetEngine.Builder;
import org.chromium.net.ICronetEngineBuilder; import org.chromium.net.ICronetEngineBuilder;
import java.io.File; import java.io.File;
...@@ -19,9 +19,10 @@ import java.lang.annotation.Retention; ...@@ -19,9 +19,10 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.net.IDN; import java.net.IDN;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.regex.Pattern; import java.util.regex.Pattern;
...@@ -100,7 +101,7 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder { ...@@ -100,7 +101,7 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder {
enableQuic(false); enableQuic(false);
enableHttp2(true); enableHttp2(true);
enableBrotli(false); enableBrotli(false);
enableHttpCache(Builder.HTTP_CACHE_DISABLED, 0); enableHttpCache(CronetEngine.Builder.HTTP_CACHE_DISABLED, 0);
enableNetworkQualityEstimator(false); enableNetworkQualityEstimator(false);
enablePublicKeyPinningBypassForLocalTrustAnchors(true); enablePublicKeyPinningBypassForLocalTrustAnchors(true);
} }
...@@ -196,14 +197,15 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder { ...@@ -196,14 +197,15 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder {
return mBrotiEnabled; return mBrotiEnabled;
} }
@IntDef({Builder.HTTP_CACHE_DISABLED, Builder.HTTP_CACHE_IN_MEMORY, @IntDef({CronetEngine.Builder.HTTP_CACHE_DISABLED, CronetEngine.Builder.HTTP_CACHE_IN_MEMORY,
Builder.HTTP_CACHE_DISK_NO_HTTP, Builder.HTTP_CACHE_DISK}) CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP, CronetEngine.Builder.HTTP_CACHE_DISK})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface HttpCacheSetting {} public @interface HttpCacheSetting {}
@Override @Override
public CronetEngineBuilderImpl enableHttpCache(@HttpCacheSetting int cacheMode, long maxSize) { public CronetEngineBuilderImpl enableHttpCache(@HttpCacheSetting int cacheMode, long maxSize) {
if (cacheMode == Builder.HTTP_CACHE_DISK || cacheMode == Builder.HTTP_CACHE_DISK_NO_HTTP) { if (cacheMode == CronetEngine.Builder.HTTP_CACHE_DISK
|| cacheMode == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP) {
if (storagePath() == null) { if (storagePath() == null) {
throw new IllegalArgumentException("Storage path must be set"); throw new IllegalArgumentException("Storage path must be set");
} }
...@@ -212,19 +214,19 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder { ...@@ -212,19 +214,19 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder {
throw new IllegalArgumentException("Storage path must not be set"); throw new IllegalArgumentException("Storage path must not be set");
} }
} }
mDisableCache = (cacheMode == Builder.HTTP_CACHE_DISABLED mDisableCache = (cacheMode == CronetEngine.Builder.HTTP_CACHE_DISABLED
|| cacheMode == Builder.HTTP_CACHE_DISK_NO_HTTP); || cacheMode == CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP);
mHttpCacheMaxSize = maxSize; mHttpCacheMaxSize = maxSize;
switch (cacheMode) { switch (cacheMode) {
case Builder.HTTP_CACHE_DISABLED: case CronetEngine.Builder.HTTP_CACHE_DISABLED:
mHttpCacheMode = HttpCacheType.DISABLED; mHttpCacheMode = HttpCacheType.DISABLED;
break; break;
case Builder.HTTP_CACHE_DISK_NO_HTTP: case CronetEngine.Builder.HTTP_CACHE_DISK_NO_HTTP:
case Builder.HTTP_CACHE_DISK: case CronetEngine.Builder.HTTP_CACHE_DISK:
mHttpCacheMode = HttpCacheType.DISK; mHttpCacheMode = HttpCacheType.DISK;
break; break;
case Builder.HTTP_CACHE_IN_MEMORY: case CronetEngine.Builder.HTTP_CACHE_IN_MEMORY:
mHttpCacheMode = HttpCacheType.MEMORY; mHttpCacheMode = HttpCacheType.MEMORY;
break; break;
default: default:
...@@ -271,17 +273,17 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder { ...@@ -271,17 +273,17 @@ public abstract class CronetEngineBuilderImpl extends ICronetEngineBuilder {
throw new NullPointerException("The pin expiration date cannot be null"); throw new NullPointerException("The pin expiration date cannot be null");
} }
String idnHostName = validateHostNameForPinningAndConvert(hostName); String idnHostName = validateHostNameForPinningAndConvert(hostName);
// Convert the pin to BASE64 encoding. The hash set will eliminate duplications. // Convert the pin to BASE64 encoding to remove duplicates.
Set<byte[]> hashes = new HashSet<>(pinsSha256.size()); Map<String, byte[]> hashes = new HashMap<>();
for (byte[] pinSha256 : pinsSha256) { for (byte[] pinSha256 : pinsSha256) {
if (pinSha256 == null || pinSha256.length != 32) { if (pinSha256 == null || pinSha256.length != 32) {
throw new IllegalArgumentException("Public key pin is invalid"); throw new IllegalArgumentException("Public key pin is invalid");
} }
hashes.add(pinSha256); hashes.put(Base64.encodeToString(pinSha256, 0), pinSha256);
} }
// Add new element to PKP list. // Add new element to PKP list.
mPkps.add(new Pkp(idnHostName, hashes.toArray(new byte[hashes.size()][]), includeSubdomains, mPkps.add(new Pkp(idnHostName, hashes.values().toArray(new byte[hashes.size()][]),
expirationDate)); includeSubdomains, expirationDate));
return this; return this;
} }
......
...@@ -450,6 +450,7 @@ public class PkpTest { ...@@ -450,6 +450,7 @@ public class PkpTest {
return sha256; return sha256;
} }
@SuppressWarnings("ArrayAsKeyOfSetOrMap")
private void addPkpSha256( private void addPkpSha256(
String host, byte[] pinHashValue, boolean includeSubdomain, int maxAgeInSec) { String host, byte[] pinHashValue, boolean includeSubdomain, int maxAgeInSec) {
Set<byte[]> hashes = new HashSet<>(); Set<byte[]> hashes = new HashSet<>();
...@@ -498,6 +499,7 @@ public class PkpTest { ...@@ -498,6 +499,7 @@ public class PkpTest {
fail("Expected IllegalArgumentException when passing " + hostName + " host name"); fail("Expected IllegalArgumentException when passing " + hostName + " host name");
} }
@SuppressWarnings("ArrayAsKeyOfSetOrMap")
private void verifyExceptionWhenAddPkpArgumentIsNull( private void verifyExceptionWhenAddPkpArgumentIsNull(
boolean hostNameIsNull, boolean pinsAreNull, boolean expirationDataIsNull) { boolean hostNameIsNull, boolean pinsAreNull, boolean expirationDataIsNull) {
String hostName = hostNameIsNull ? null : "some-host.com"; String hostName = hostNameIsNull ? null : "some-host.com";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment