Skip to content

Commit 9e9840e

Browse files
committed
Validate that the module config sent by the client is valid
1 parent ec511fb commit 9e9840e

File tree

11 files changed

+55
-21
lines changed

11 files changed

+55
-21
lines changed

src/api/java/mekanism/api/gear/config/ModuleConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public final String name() {
4141

4242
public abstract DATA get();
4343

44+
//throws null pointer if value is null
45+
//throws IllegalArgumentException if value is not valid
4446
public abstract ModuleConfig<DATA> with(DATA value);
4547

4648
public boolean isConfigDisabled() {

src/datagen/generated/mekanism/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/.cache/c10fcd8abbb6a520fc3ac2cf14b627d36958dd55

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/assets/mekanism/lang/en_ud.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/assets/mekanism/lang/en_us.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/generated/mekanism/data/mekanism/loot_tables/blocks/digital_miner.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datagen/main/java/mekanism/client/lang/MekanismLangProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,7 @@ private void addMisc() {
836836
add(MekanismLang.LOG_FORMAT, "[%1$s] %2$s");
837837
add(MekanismLang.FORGE, "NeoForge");
838838
add(MekanismLang.ERROR, "Error");
839+
add(MekanismLang.INVALID_PACKET, "Invalid packet data received: %1$s");
839840
add(MekanismLang.ALPHA_WARNING, "Warning: " + modName + " is currently in alpha, and is not recommended for widespread use in modpacks. There are likely to be game breaking bugs, and various other issues that you can read more about %1$s.");
840841
add(MekanismLang.ALPHA_WARNING_HERE, "here");
841842
//Equipment

src/main/java/mekanism/common/MekanismLang.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public enum MekanismLang implements ILangEntry {
2222
PACK_DESCRIPTION("constants", "pack_description"),
2323
FORGE("constants", "forge"),
2424
ERROR("constants", "error"),
25+
INVALID_PACKET("constants", "error.packet.invalid"),
2526
ALPHA_WARNING("constants", "alpha_warning"),
2627
ALPHA_WARNING_HERE("constants", "alpha_warning.here"),
2728
//Equipment

src/main/java/mekanism/common/content/gear/Module.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,28 @@ Module<MODULE> withReplacedInstallCount(HolderLookup.Provider provider, int inst
225225
}
226226

227227
Module<MODULE> withReplacedConfig(ModuleConfig<?> config) {
228+
return withReplacedConfig(config, false);
229+
}
230+
231+
//throws IllegalArgumentException
232+
//throws IllegalStateException
233+
<CONFIG> Module<MODULE> withReplacedConfig(ModuleConfig<CONFIG> config, boolean fromPacket) {
228234
for (int i = 0; i < configItems.size(); i++) {
229235
ModuleConfig<?> storedConfig = configItems.get(i);
230236
if (storedConfig.name().equals(config.name())) {
231237
if (storedConfig.get().equals(config.get())) {
232238
//Nothing changed
233239
return this;
240+
} else if (fromPacket) {
241+
//Note: This cast is theoretically not unsafe as when reading from the packet we validate
242+
// that the type is what we expect it to be. To be safe though we double-check the classes
243+
if (storedConfig.getClass() != config.getClass()) {
244+
//Invalid, don't apply the change
245+
throw new IllegalStateException("Config " + config.name() + "'s Class " + config.getClass().getSimpleName() + " did not match " + storedConfig.getClass().getSimpleName());
246+
}
247+
//Ensure we sanitize it and that it actually has the correct range applied and the client
248+
// didn't just lie about how many are installed in order to get a higher value set
249+
config = ((ModuleConfig<CONFIG>) storedConfig).with(config.get());
234250
}
235251
List<ModuleConfig<?>> copiedConfigs = new ArrayList<>(configItems);
236252
copiedConfigs.set(i, config);

src/main/java/mekanism/common/content/gear/ModuleContainer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,15 @@ public List<Component> getHUDStrings(Player player, ItemStack stack) {
104104
return ret;
105105
}
106106

107+
//throws IllegalStateException
107108
@Override
108109
public <MODULE extends ICustomModule<MODULE>> ModuleContainer replaceModuleConfig(ItemStack stack, ModuleData<MODULE> type, ModuleConfig<?> config) {
110+
return replaceModuleConfig(stack, type, config, false);
111+
}
112+
113+
//throws IllegalArgumentException
114+
//throws IllegalStateException
115+
public <MODULE extends ICustomModule<MODULE>> ModuleContainer replaceModuleConfig(ItemStack stack, ModuleData<MODULE> type, ModuleConfig<?> config, boolean fromPacket) {
109116
Module<MODULE> module = get(type);
110117
if (module == null) {
111118
throw new IllegalArgumentException("Module container does not contain any modules of type " + type.getRegistryName());
@@ -119,12 +126,15 @@ public <MODULE extends ICustomModule<MODULE>> ModuleContainer replaceModuleConfi
119126
} else if (config.name().equals(ModuleConfig.HANDLES_MODE_CHANGE_KEY)) {
120127
if (module.handlesModeChangeRaw() == (boolean) config.get()) {
121128
return this;//State matches no change needed
129+
} else if (fromPacket && module.getConfig(ModuleConfig.HANDLES_MODE_CHANGE_KEY) == null) {
130+
//Illegal state, got a packet for mode change key, but it doesn't support mode changes
131+
return this;
122132
}
123133
//Toggle the handle mode state including any side effects changing that config may have
124134
return toggleHandlesModeChange(stack, type, module);
125135
}
126136

127-
Module<MODULE> replacedModule = module.withReplacedConfig(config);
137+
Module<MODULE> replacedModule = module.withReplacedConfig(config, fromPacket);
128138
if (module == replacedModule) {
129139
//If nothing actually changed we don't need to bother updating the instance on the stack
130140
return this;

0 commit comments

Comments
 (0)