Skip to content

Commit b27523b

Browse files
[FEATURE] Implement Safe Casting in getPermissions Method (#13143)
* [FEATURE] Implement Safe Casting in getPermissions Method This commit addresses a potential ClassCastException issue in the getPermissions method. Previously, the method executed a type cast from Object to Map<String, Object> without checking if this cast was safe, leading to a potential risk of runtime exceptions. Changes made: 1. Added a type check using 'instanceof' to ensure that the result from executeMethod.execute is indeed a Map<?, ?> before proceeding with the cast. 2. Iterated over the entries of the resultMap, and performed a safe cast for each key-value pair, ensuring they are of types String and Boolean, respectively. 3. In case the resultObject is not an instance of Map, the method now throws an IllegalStateException with a descriptive error message, including the unexpected object's class name. These changes enhance the robustness and reliability of the getPermissions method by preventing unsafe type casts and providing clearer error handling for unexpected types. * Run ./scripts/format.sh --------- Co-authored-by: Diego Molina <[email protected]>
1 parent 3ae7ae2 commit b27523b

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

java/src/org/openqa/selenium/safari/AddHasPermissions.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.openqa.selenium.safari;
1919

2020
import com.google.auto.service.AutoService;
21+
import java.util.HashMap;
2122
import java.util.Map;
2223
import java.util.function.Predicate;
2324
import org.openqa.selenium.Capabilities;
@@ -66,9 +67,21 @@ public void setPermissions(String permission, boolean value) {
6667

6768
@Override
6869
public Map<String, Boolean> getPermissions() {
69-
Map<String, Object> results =
70-
(Map<String, Object>) executeMethod.execute(GET_PERMISSIONS, null);
71-
return (Map<String, Boolean>) results.get("permissions");
70+
Object resultObject = executeMethod.execute(GET_PERMISSIONS, null);
71+
72+
if (resultObject instanceof Map<?, ?>) {
73+
Map<?, ?> resultMap = (Map<?, ?>) resultObject;
74+
Map<String, Boolean> permissionMap = new HashMap<>();
75+
for (Map.Entry<?, ?> entry : resultMap.entrySet()) {
76+
if (entry.getKey() instanceof String && entry.getValue() instanceof Boolean) {
77+
permissionMap.put((String) entry.getKey(), (Boolean) entry.getValue());
78+
}
79+
}
80+
return permissionMap;
81+
} else {
82+
throw new IllegalStateException(
83+
"Unexpected result type: " + resultObject.getClass().getName());
84+
}
7285
}
7386
};
7487
}

0 commit comments

Comments
 (0)