Skip to content

Commit

Permalink
Add protection around meta data being written that is immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Sep 7, 2024
1 parent dbf4bb7 commit 80219f3
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,42 @@ public void addToPendingKick(World world)
public Optional<Map<String, MetaDataValue>> getMetaData() {
if (metaData == null) {
metaData = new HashMap<>();
} else if (isImmutable(metaData)) {
metaData = new HashMap<>(metaData); // Convert immutable map to mutable
}
return Optional.of(metaData);
}

private boolean isImmutable(Map<String, MetaDataValue> map) {
try {
String testKey = "testKey";
MetaDataValue testValue = new MetaDataValue("test");

// If the map already contains keys, use one of them
if (!map.isEmpty()) {
String existingKey = map.keySet().iterator().next();
map.put(existingKey, map.get(existingKey)); // Attempt to replace value
} else {
// Use a unique key-value pair
map.put(testKey, testValue);
map.remove(testKey);
}
return false; // No exception means the map is mutable
} catch (UnsupportedOperationException e) {
return true; // Exception means the map is immutable
}
}

/**
* @param metaData the metaData to set
* @since 1.15.4
* @see User#setMetaData(Map)
*/
@Override
public void setMetaData(Map<String, MetaDataValue> metaData) {
if (isImmutable(metaData)) {
throw new IllegalArgumentException("Provided map is immutable and cannot be set.");
}
this.metaData = metaData;
}

Expand Down

0 comments on commit 80219f3

Please sign in to comment.