Skip to content

Commit 3dca9ef

Browse files
Only allow http and https (#4457)
1 parent fcbe8d7 commit 3dca9ef

4 files changed

Lines changed: 68 additions & 15 deletions

File tree

src/main/java/com/gregtechceu/gtceu/api/misc/ImageCache.java

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.gregtechceu.gtceu.api.misc;
22

33
import com.gregtechceu.gtceu.GTCEu;
4+
import com.gregtechceu.gtceu.config.ConfigHolder;
45

56
import com.google.common.cache.CacheBuilder;
67
import com.google.common.cache.CacheLoader;
78
import com.google.common.cache.LoadingCache;
89

910
import java.io.IOException;
1011
import java.io.InputStream;
12+
import java.net.MalformedURLException;
1113
import java.net.URL;
1214
import java.util.concurrent.ExecutionException;
1315
import java.util.concurrent.TimeUnit;
@@ -18,26 +20,50 @@ public class ImageCache {
1820
public static final long REFRESH_SECS = 120;
1921
public static final long EXPIRE_SECS = 300;
2022
private static final byte[] NULL_MARKER = new byte[0];
23+
private static final String[] ALLOWED_PROTOCOLS = new String[] { "http", "https" };
2124

2225
private static boolean downloading = false;
2326

2427
private static final LoadingCache<String, byte[]> CACHE = CacheBuilder.newBuilder()
2528
.refreshAfterWrite(REFRESH_SECS, TimeUnit.SECONDS)
2629
.expireAfterAccess(EXPIRE_SECS, TimeUnit.SECONDS)
2730
.concurrencyLevel(3)
28-
.build(CacheLoader.from(url -> {
29-
if (downloading) return NULL_MARKER;
30-
downloading = true;
31-
32-
try (InputStream stream = new URL(url).openStream()) {
33-
return stream.readAllBytes();
34-
} catch (IOException e) {
35-
GTCEu.LOGGER.error("Could not load image {}", url, e);
36-
downloading = false;
31+
.build(CacheLoader.from(urlString -> {
32+
try {
33+
URL url = new URL(urlString);
34+
boolean singleplayer = GTCEu.getMinecraftServer().isSingleplayer() &&
35+
!GTCEu.getMinecraftServer().isPublished();
36+
boolean allowedProtocol = singleplayer;
37+
for (String protocol : ALLOWED_PROTOCOLS) {
38+
if (url.getProtocol().equalsIgnoreCase(protocol)) {
39+
allowedProtocol = true;
40+
break;
41+
}
42+
}
43+
if (!allowedProtocol) return NULL_MARKER;
44+
boolean allowedDomain = singleplayer;
45+
for (String domain : ConfigHolder.INSTANCE.gameplay.allowedImageDomains) {
46+
if (url.getHost().equalsIgnoreCase(domain)) {
47+
allowedDomain = true;
48+
break;
49+
}
50+
}
51+
if (!allowedDomain) return NULL_MARKER;
52+
if (downloading) return NULL_MARKER;
53+
downloading = true;
54+
55+
try (InputStream stream = url.openStream()) {
56+
byte[] image = stream.readAllBytes();
57+
GTCEu.LOGGER.debug("Downloaded image {}! Executing callback", url);
58+
return image;
59+
} catch (IOException e) {
60+
GTCEu.LOGGER.error("Could not load image {}", url, e);
61+
return NULL_MARKER;
62+
} finally {
63+
downloading = false;
64+
}
65+
} catch (MalformedURLException e) {
3766
return NULL_MARKER;
38-
} finally {
39-
GTCEu.LOGGER.debug("Downloaded image {}! Executing callback", url);
40-
downloading = false;
4167
}
4268
}));
4369

src/main/java/com/gregtechceu/gtceu/common/machine/multiblock/electric/monitor/MonitorGroup.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import com.gregtechceu.gtceu.api.capability.ICoverable;
55
import com.gregtechceu.gtceu.api.capability.IMonitorComponent;
66
import com.gregtechceu.gtceu.api.cover.CoverBehavior;
7+
import com.gregtechceu.gtceu.api.item.IComponentItem;
8+
import com.gregtechceu.gtceu.api.item.component.IItemComponent;
9+
import com.gregtechceu.gtceu.api.item.component.IMonitorModuleItem;
710
import com.gregtechceu.gtceu.api.transfer.item.CustomItemStackHandler;
811

912
import net.minecraft.core.BlockPos;
@@ -43,13 +46,29 @@ public class MonitorGroup {
4346
@Getter
4447
private int dataSlot = 0;
4548

49+
public static boolean isModule(ItemStack stack) {
50+
if (stack.getItem() instanceof IComponentItem componentItem) {
51+
for (IItemComponent itemComponent : componentItem.getComponents()) {
52+
if (itemComponent instanceof IMonitorModuleItem) return true;
53+
}
54+
}
55+
return false;
56+
}
57+
58+
public static CustomItemStackHandler createModuleHandler() {
59+
CustomItemStackHandler customItemStackHandler = new CustomItemStackHandler(1);
60+
customItemStackHandler.setFilter(MonitorGroup::isModule);
61+
return customItemStackHandler;
62+
}
63+
4664
public MonitorGroup(String name) {
47-
this(name, new CustomItemStackHandler(1), new CustomItemStackHandler(8));
65+
this(name, createModuleHandler(), new CustomItemStackHandler(8));
4866
}
4967

5068
public MonitorGroup(String name, CustomItemStackHandler handler, CustomItemStackHandler placeholderSlotsHandler) {
5169
this.name = name;
5270
this.itemStackHandler = handler;
71+
this.itemStackHandler.setFilter(MonitorGroup::isModule);
5372
this.placeholderSlotsHandler = placeholderSlotsHandler;
5473
}
5574

src/main/java/com/gregtechceu/gtceu/common/network/packets/SCPacketMonitorGroupNBTChange.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import net.minecraft.world.item.ItemStack;
1313
import net.minecraft.world.level.Level;
1414
import net.minecraftforge.common.util.LogicalSidedProvider;
15+
import net.minecraftforge.items.IItemHandlerModifiable;
1516
import net.minecraftforge.network.NetworkEvent;
1617

1718
import java.util.Optional;
@@ -50,8 +51,11 @@ public void execute(NetworkEvent.Context context) {
5051

5152
MetaMachine machine = MetaMachine.getMachine(level, pos);
5253
if (machine instanceof CentralMonitorMachine centralMonitor) {
53-
centralMonitor.getMonitorGroups().get(monitorGroupId)
54-
.getItemStackHandler().setStackInSlot(0, stack);
54+
IItemHandlerModifiable itemHandler = centralMonitor.getMonitorGroups().get(monitorGroupId)
55+
.getItemStackHandler();
56+
if (ItemStack.isSameItem(itemHandler.getStackInSlot(0), stack)) {
57+
itemHandler.setStackInSlot(0, stack);
58+
}
5559
}
5660
}
5761

src/main/java/com/gregtechceu/gtceu/config/ConfigHolder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ public static class GameplayConfigs {
739739
@Configurable.Comment({ "How much environmental hazards decay per chunk, per tick.",
740740
"Default: 0.001" })
741741
public float environmentalHazardDecayRate = 0.001f;
742+
@Configurable
743+
@Configurable.Comment({ "List of domains that are allowed in the image module" })
744+
public String[] allowedImageDomains = new String[] { "imgur.com", "discord.com", "github.com",
745+
"raw.githubusercontent.com" };
742746
}
743747

744748
public static class ClientConfigs {

0 commit comments

Comments
 (0)