p2p: check the length of the network name bytes
Fixes: 144472136
Bug: 144472136
Test: atest FrameworksWifiApiTests
Change-Id: I80b8e0330499e6c655efb1712811891a8e8229cb
(cherry picked from commit a0900344f4ea850ca9ee81fc936298f894e77f54)
diff --git a/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java b/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java
index c3cfb02..80776fe 100644
--- a/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java
+++ b/wifi/java/android/net/wifi/p2p/WifiP2pConfig.java
@@ -28,6 +28,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.nio.charset.StandardCharsets;
import java.util.regex.PatternSyntaxException;
/**
@@ -228,6 +229,10 @@
private static final MacAddress MAC_ANY_ADDRESS =
MacAddress.fromString("02:00:00:00:00:00");
+ /**
+ * Maximum number of bytes allowed for a SSID.
+ */
+ private static final int MAX_SSID_BYTES = 32;
private MacAddress mDeviceAddress = MAC_ANY_ADDRESS;
private String mNetworkName = "";
@@ -279,6 +284,10 @@
throw new IllegalArgumentException(
"network name must be non-empty.");
}
+ if (networkName.getBytes(StandardCharsets.UTF_8).length > MAX_SSID_BYTES) {
+ throw new IllegalArgumentException(
+ "network name exceeds " + MAX_SSID_BYTES + " bytes.");
+ }
try {
if (!networkName.matches("^DIRECT-[a-zA-Z0-9]{2}.*")) {
throw new IllegalArgumentException(
diff --git a/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java
index 41f109a..6199325 100644
--- a/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java
+++ b/wifi/tests/src/android/net/wifi/p2p/WifiP2pConfigTest.java
@@ -53,6 +53,13 @@
fail("Unexpected IllegalArgumentException");
}
+ // sunny case with maximum bytes for the network name
+ try {
+ b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxy");
+ } catch (IllegalArgumentException e) {
+ fail("Unexpected IllegalArgumentException");
+ }
+
// less than 9 characters.
try {
b.setNetworkName("DIRECT-z");
@@ -77,6 +84,12 @@
b.setNetworkName("direct-a?");
fail("expected IllegalArgumentException");
} catch (IllegalArgumentException e) { }
+
+ // over maximum bytes
+ try {
+ b.setNetworkName("DIRECT-abcdefghijklmnopqrstuvwxyz");
+ fail("expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) { }
}
/**