Compare commits

..

13 Commits

Author SHA1 Message Date
PiggyPiglet
5623a00f9b Merge pull request #978 from JulianVennen/block-unverified-expansions
Add environment variable to override cloud_allowunverified_expansions
2023-09-21 17:38:54 +08:00
PiggyPiglet
4926907b47 Merge pull request #969 from PlaceholderAPI/feature/support-1.20
1.20 support
2023-09-04 17:26:11 +08:00
Andre_601
195158b18b Use new format for note block [skip ci] 2023-08-01 03:27:15 +02:00
Gabriel Dumitru
36fa9ac96d feat: distinguish expansions (external or internal) (fixes #945) (#953) 2023-07-24 11:48:29 +03:00
Julian
42cfe1dc80 simplify env values to true/false 2023-06-23 17:06:11 +02:00
Julian
92a7d54664 Add environment variable to override cloud_allowunverified_expansions
This adds the environment variable PAPI_ALLOW_UNVERIFIED_EXPANSIONS.
It allows overriding the option set in the config.yml.
2023-06-23 14:04:00 +02:00
Gabriel Dumitru
f91b4e3752 feat: fetch all expansions (#952) 2023-06-21 21:26:52 +03:00
PiggyPiglet
a497e05e55 Merge pull request #947 from JulianVennen/block-extensions-env
Add environment variable to block individual ecloud expansions.
2023-06-21 20:25:16 +08:00
Andre601
74d8fec6bd Bump Spigot version 2023-06-10 18:13:10 +02:00
Andre601
28287c7fbd 1.20 NMS version support 2023-06-10 17:59:55 +02:00
Gabriel Dumitru
744cf6d8c0 Merge pull request #946 from JulianVennen/block-load-path-traversal
Prevent loading of extensions outside of the expansions folder using the register command
2023-04-04 17:28:00 +03:00
Julian
ecd4c002c8 Add environment variable to block individual ecloud expansions.
This commit adds the environment variable "PAPI_BLOCKED_EXPANSIONS"
which can contain a case insensitive, comma separated list of blocked
expansions.
Expansions on this list can no longer be downloaded using commands.
2023-03-30 13:41:01 +02:00
Julian
b4e60b7db5 Prevent loading of extensions outside of the expansions folder using the register command 2023-03-30 12:47:23 +02:00
13 changed files with 165 additions and 87 deletions

View File

@@ -1,8 +1,8 @@
comment: comment:
footer: "\ footer: "\
----\n\n ----<br><br>
> **Note**\n > [!NOTE]<br>
> *This is an automated response created by a **GitHub Action***\n > *This is an automated response created by a **GitHub Action***<br>
> *Mentioning the bot won't have any effect!* > *Mentioning the bot won't have any effect!*
" "

View File

@@ -26,7 +26,7 @@ dependencies {
implementation("org.bstats:bstats-bukkit:3.0.1") implementation("org.bstats:bstats-bukkit:3.0.1")
implementation("net.kyori:adventure-platform-bukkit:4.3.0") implementation("net.kyori:adventure-platform-bukkit:4.3.0")
compileOnlyApi("dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT") // this is temp compileOnly("org.spigotmc:spigot-api:1.20-R0.1-SNAPSHOT")
compileOnlyApi("org.jetbrains:annotations:23.0.0") compileOnlyApi("org.jetbrains:annotations:23.0.0")
testImplementation("org.openjdk.jmh:jmh-core:1.32") testImplementation("org.openjdk.jmh:jmh-core:1.32")
@@ -38,8 +38,8 @@ dependencies {
java { java {
sourceCompatibility = JavaVersion.VERSION_17 // this is temp sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_17 // this is temp targetCompatibility = JavaVersion.VERSION_1_8
withJavadocJar() withJavadocJar()
withSourcesJar() withSourcesJar()

View File

@@ -162,7 +162,7 @@ public final class PlaceholderAPIPlugin extends JavaPlugin {
HandlerList.unregisterAll(this); HandlerList.unregisterAll(this);
Bukkit.getAsyncScheduler().cancelTasks(this); // this is hopefully temp Bukkit.getScheduler().cancelTasks(this);
adventure.close(); adventure.close();
adventure = null; adventure = null;

View File

@@ -20,6 +20,7 @@
package me.clip.placeholderapi.commands.impl.cloud; package me.clip.placeholderapi.commands.impl.cloud;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -37,6 +38,25 @@ public final class CommandECloudDownload extends PlaceholderCommand {
super("download"); super("download");
} }
private boolean isBlockedExpansion(String name) {
String env = System.getenv("PAPI_BLOCKED_EXPANSIONS");
if (env == null) {
return false;
}
return Arrays.stream(env.split(","))
.anyMatch(s -> s.equalsIgnoreCase(name));
}
private boolean areUnverifiedExpansionsAllowed(@NotNull final PlaceholderAPIPlugin plugin) {
String env = System.getenv("PAPI_ALLOW_UNVERIFIED_EXPANSIONS");
if (env != null) {
return env.equalsIgnoreCase("true");
}
return plugin.getPlaceholderAPIConfig().cloudAllowUnverifiedExpansions();
}
@Override @Override
public void evaluate(@NotNull final PlaceholderAPIPlugin plugin, public void evaluate(@NotNull final PlaceholderAPIPlugin plugin,
@NotNull final CommandSender sender, @NotNull final String alias, @NotNull final CommandSender sender, @NotNull final String alias,
@@ -47,6 +67,12 @@ public final class CommandECloudDownload extends PlaceholderCommand {
return; return;
} }
if (isBlockedExpansion(params.get(0))) {
Msg.msg(sender,
"&cThis expansion can't be downloaded.");
return;
}
final CloudExpansion expansion = plugin.getCloudExpansionManager() final CloudExpansion expansion = plugin.getCloudExpansionManager()
.findCloudExpansionByName(params.get(0)).orElse(null); .findCloudExpansionByName(params.get(0)).orElse(null);
if (expansion == null) { if (expansion == null) {
@@ -55,6 +81,11 @@ public final class CommandECloudDownload extends PlaceholderCommand {
return; return;
} }
if (!expansion.isVerified() && !this.areUnverifiedExpansionsAllowed(plugin)) {
Msg.msg(sender, "&cThe expansion '&f" + params.get(0) + "&c' is not verified and can only be downloaded manually from &fhttps://placeholderapi.com/ecloud");
return;
}
final CloudExpansion.Version version; final CloudExpansion.Version version;
if (params.size() < 2) { if (params.size() < 2) {
version = expansion.getVersion(expansion.getLatestVersion()); version = expansion.getVersion(expansion.getLatestVersion());
@@ -86,9 +117,7 @@ public final class CommandECloudDownload extends PlaceholderCommand {
.getVersion() + "] &ato file: &f" + file.getName(), .getVersion() + "] &ato file: &f" + file.getName(),
"&aMake sure to type &f/papi reload &ato enable your new expansion!"); "&aMake sure to type &f/papi reload &ato enable your new expansion!");
plugin.getCloudExpansionManager().clean(); plugin.getCloudExpansionManager().load();
plugin.getCloudExpansionManager()
.fetch(plugin.getPlaceholderAPIConfig().cloudAllowUnverifiedExpansions());
}); });
} }

View File

@@ -38,9 +38,7 @@ public final class CommandECloudRefresh extends PlaceholderCommand {
public void evaluate(@NotNull final PlaceholderAPIPlugin plugin, public void evaluate(@NotNull final PlaceholderAPIPlugin plugin,
@NotNull final CommandSender sender, @NotNull final String alias, @NotNull final CommandSender sender, @NotNull final String alias,
@NotNull @Unmodifiable final List<String> params) { @NotNull @Unmodifiable final List<String> params) {
plugin.getCloudExpansionManager().clean(); plugin.getCloudExpansionManager().load();
plugin.getCloudExpansionManager()
.fetch(plugin.getPlaceholderAPIConfig().cloudAllowUnverifiedExpansions());
Msg.msg(sender, Msg.msg(sender,
"&aThe eCloud manager has been refreshed!"); "&aThe eCloud manager has been refreshed!");

View File

@@ -54,7 +54,7 @@ public final class CommandExpansionRegister extends PlaceholderCommand {
final LocalExpansionManager manager = plugin.getLocalExpansionManager(); final LocalExpansionManager manager = plugin.getLocalExpansionManager();
final File file = new File(manager.getExpansionsFolder(), params.get(0)); final File file = new File(manager.getExpansionsFolder(), params.get(0));
if (!file.exists()) { if (!file.exists() || !file.getParentFile().equals(manager.getExpansionsFolder())) {
Msg.msg(sender, Msg.msg(sender,
"&cThe file &f" + file.getName() + "&c doesn't exist!"); "&cThe file &f" + file.getName() + "&c doesn't exist!");
return; return;

View File

@@ -46,7 +46,8 @@ public enum NMSVersion {
SPIGOT_1_18_R1("v1_18_R1"), SPIGOT_1_18_R1("v1_18_R1"),
SPIGOT_1_19_R1("v1_19_R1"), SPIGOT_1_19_R1("v1_19_R1"),
SPIGOT_1_19_R2("v1_19_R2"), SPIGOT_1_19_R2("v1_19_R2"),
SPIGOT_1_19_R3("v1_19_R3"); SPIGOT_1_19_R3("v1_19_R3"),
SPIGOT_1_20_R1("v1_20_R1");
private final String version; private final String version;

View File

@@ -40,6 +40,14 @@ import org.jetbrains.annotations.Nullable;
*/ */
public abstract class PlaceholderExpansion extends PlaceholderHook { public abstract class PlaceholderExpansion extends PlaceholderHook {
/**
* The type is {@link Type#INTERNAL} by default.
* For external expansions, the type is updated on {@link me.clip.placeholderapi.expansion.manager.LocalExpansionManager#register(Class) register}.
* @since 2.11.4
*/
@ApiStatus.Internal
protected Type expansionType = Type.INTERNAL;
/** /**
* The placeholder identifier of this expansion. May not contain {@literal %}, * The placeholder identifier of this expansion. May not contain {@literal %},
* {@literal {}} or _ * {@literal {}} or _
@@ -159,6 +167,27 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
return PlaceholderAPIPlugin.getInstance(); return PlaceholderAPIPlugin.getInstance();
} }
/**
* Get the type of the expansion
*
* @return the type of the expansion
* @since 2.11.4
*/
@ApiStatus.Internal
public Type getExpansionType() {
return expansionType;
}
/**
* Set the type of the expansion
* @param expansionType the new type
* @since 2.11.4
*/
@ApiStatus.Internal
public void setExpansionType(Type expansionType) {
this.expansionType = expansionType;
}
// === Configuration === // === Configuration ===
/** /**
@@ -166,7 +195,7 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
* null when not specified. * null when not specified.
* <br>You may use the {@link Configurable} interface to define default values set * <br>You may use the {@link Configurable} interface to define default values set
* *
* @return ConfigurationSection that this epxpansion has. * @return ConfigurationSection that this expansion has.
*/ */
@Nullable @Nullable
public final ConfigurationSection getConfigSection() { public final ConfigurationSection getConfigSection() {
@@ -394,8 +423,8 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
*/ */
@Override @Override
public final String toString() { public final String toString() {
return String.format("PlaceholderExpansion[name: '%s', author: '%s', version: '%s']", getName(), return String.format("PlaceholderExpansion[name: '%s', author: '%s', version: '%s', type: '%s']", getName(),
getAuthor(), getVersion()); getAuthor(), getVersion(), getExpansionType());
} }
// === Deprecated API === // === Deprecated API ===
@@ -432,4 +461,19 @@ public abstract class PlaceholderExpansion extends PlaceholderHook {
public String getLink() { public String getLink() {
return null; return null;
} }
public enum Type {
/**
* An expansion provided by a plugin is considered internal
*/
INTERNAL,
/**
* An expansion loaded from the expansions folder is considered external
*/
EXTERNAL
}
} }

View File

@@ -100,7 +100,7 @@ public final class CloudExpansionManager {
public void load() { public void load() {
clean(); clean();
fetch(plugin.getPlaceholderAPIConfig().cloudAllowUnverifiedExpansions()); fetch();
} }
public void kill() { public void kill() {
@@ -170,7 +170,7 @@ public final class CloudExpansionManager {
await.clear(); await.clear();
} }
public void fetch(final boolean allowUnverified) { public void fetch() {
plugin.getLogger().info("Fetching available expansion information..."); plugin.getLogger().info("Fetching available expansion information...");
ASYNC_EXECUTOR.submit( ASYNC_EXECUTOR.submit(
@@ -190,9 +190,6 @@ public final class CloudExpansionManager {
|| expansion.getVersion(expansion.getLatestVersion()) == null) { || expansion.getVersion(expansion.getLatestVersion()) == null) {
toRemove.add(entry.getKey()); toRemove.add(entry.getKey());
} }
if (!allowUnverified && !expansion.isVerified()) {
toRemove.add(entry.getKey());
}
} }
for (String name : toRemove) { for (String name : toRemove) {
@@ -203,33 +200,40 @@ public final class CloudExpansionManager {
plugin.getLogger().log(Level.WARNING, "Failed to download expansion information", e); plugin.getLogger().log(Level.WARNING, "Failed to download expansion information", e);
} }
//todo: Figure out why this was being scheduled back on the main thread // loop through what's left on the main thread
try { plugin
for (Map.Entry<String, CloudExpansion> entry : values.entrySet()) { .getServer()
String name = entry.getKey(); .getScheduler()
CloudExpansion expansion = entry.getValue(); .runTask(
plugin,
() -> {
try {
for (Map.Entry<String, CloudExpansion> entry : values.entrySet()) {
String name = entry.getKey();
CloudExpansion expansion = entry.getValue();
expansion.setName(name); expansion.setName(name);
Optional<PlaceholderExpansion> localOpt = Optional<PlaceholderExpansion> localOpt =
plugin.getLocalExpansionManager().findExpansionByName(name); plugin.getLocalExpansionManager().findExpansionByName(name);
if (localOpt.isPresent()) { if (localOpt.isPresent()) {
PlaceholderExpansion local = localOpt.get(); PlaceholderExpansion local = localOpt.get();
if (local.isRegistered()) { if (local.isRegistered()) {
expansion.setHasExpansion(true); expansion.setHasExpansion(true);
expansion.setShouldUpdate( expansion.setShouldUpdate(
!local.getVersion().equalsIgnoreCase(expansion.getLatestVersion())); !local.getVersion().equalsIgnoreCase(expansion.getLatestVersion()));
} }
} }
cache.put(toIndexName(expansion), expansion); cache.put(toIndexName(expansion), expansion);
} }
} catch (Throwable e) { } catch (Throwable e) {
// ugly swallowing of every throwable, but we have to be defensive // ugly swallowing of every throwable, but we have to be defensive
plugin plugin
.getLogger() .getLogger()
.log(Level.WARNING, "Failed to download expansion information", e); .log(Level.WARNING, "Failed to download expansion information", e);
} }
});
}); });
} }

View File

@@ -187,6 +187,8 @@ public final class LocalExpansionManager implements Listener {
return Optional.empty(); return Optional.empty();
} }
} }
expansion.setExpansionType(PlaceholderExpansion.Type.EXTERNAL);
if (!expansion.register()) { if (!expansion.register()) {
Msg.warn("Cannot load expansion %s due to an unknown issue.", expansion.getIdentifier()); Msg.warn("Cannot load expansion %s due to an unknown issue.", expansion.getIdentifier());
@@ -209,6 +211,11 @@ public final class LocalExpansionManager implements Listener {
return Optional.empty(); return Optional.empty();
} }
/**
* Attempt to register a {@link PlaceholderExpansion}
* @param expansion the expansion to register
* @return if the expansion was registered
*/
@ApiStatus.Internal @ApiStatus.Internal
public boolean register(@NotNull final PlaceholderExpansion expansion) { public boolean register(@NotNull final PlaceholderExpansion expansion) {
final String identifier = expansion.getIdentifier().toLowerCase(Locale.ROOT); final String identifier = expansion.getIdentifier().toLowerCase(Locale.ROOT);
@@ -216,10 +223,10 @@ public final class LocalExpansionManager implements Listener {
if (!expansion.canRegister()) { if (!expansion.canRegister()) {
return false; return false;
} }
if (expansions.containsKey(identifier)) { // Avoid loading two external expansions with the same identifier
Msg.warn("Failed to load expansion %s. Identifier is already in use.", if (expansion.getExpansionType() == PlaceholderExpansion.Type.EXTERNAL && expansions.containsKey(identifier)) {
expansion.getIdentifier()); Msg.warn("Failed to load external expansion %s. Identifier is already in use.", expansion.getIdentifier());
return false; return false;
} }
@@ -269,13 +276,12 @@ public final class LocalExpansionManager implements Listener {
return false; return false;
} }
// this is temp final ExpansionRegisterEvent event = new ExpansionRegisterEvent(expansion);
// final ExpansionRegisterEvent event = new ExpansionRegisterEvent(expansion); Bukkit.getPluginManager().callEvent(event);
// Bukkit.getPluginManager().callEvent(event);
// if (event.isCancelled()) {
// if (event.isCancelled()) { return false;
// return false; }
// }
expansionsLock.lock(); expansionsLock.lock();
try { try {
@@ -288,21 +294,24 @@ public final class LocalExpansionManager implements Listener {
Bukkit.getPluginManager().registerEvents(((Listener) expansion), plugin); Bukkit.getPluginManager().registerEvents(((Listener) expansion), plugin);
} }
Msg.info("Successfully registered expansion: %s [%s]", expansion.getIdentifier(), Msg.info(
expansion.getVersion()); "Successfully registered %s expansion: %s [%s]",
expansion.getExpansionType().name().toLowerCase(),
expansion.getIdentifier(),
expansion.getVersion()
);
if (expansion instanceof Taskable) { if (expansion instanceof Taskable) {
((Taskable) expansion).start(); ((Taskable) expansion).start();
} }
if (plugin.getPlaceholderAPIConfig().isCloudEnabled()) { // Check eCloud for updates only if the expansion is external
final Optional<CloudExpansion> cloudExpansionOptional = if (plugin.getPlaceholderAPIConfig().isCloudEnabled() && expansion.getExpansionType() == PlaceholderExpansion.Type.EXTERNAL) {
plugin.getCloudExpansionManager().findCloudExpansionByName(identifier); final Optional<CloudExpansion> cloudExpansionOptional = plugin.getCloudExpansionManager().findCloudExpansionByName(identifier);
if (cloudExpansionOptional.isPresent()) { if (cloudExpansionOptional.isPresent()) {
CloudExpansion cloudExpansion = cloudExpansionOptional.get(); CloudExpansion cloudExpansion = cloudExpansionOptional.get();
cloudExpansion.setHasExpansion(true); cloudExpansion.setHasExpansion(true);
cloudExpansion.setShouldUpdate( cloudExpansion.setShouldUpdate(!cloudExpansion.getLatestVersion().equals(expansion.getVersion()));
!cloudExpansion.getLatestVersion().equals(expansion.getVersion()));
} }
} }

View File

@@ -24,7 +24,6 @@ import java.io.BufferedReader;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.URL; import java.net.URL;
import java.util.Arrays; import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import me.clip.placeholderapi.PlaceholderAPIPlugin; import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.util.Msg; import me.clip.placeholderapi.util.Msg;
@@ -55,43 +54,35 @@ public class UpdateChecker implements Listener {
return spigotVersion; return spigotVersion;
} }
//todo: Figure out a better approach for this?
public void fetch() { public void fetch() {
CompletableFuture<Boolean> update = new CompletableFuture<>(); Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
CompletableFuture.supplyAsync(() -> {
try { try {
HttpsURLConnection con = (HttpsURLConnection) new URL( HttpsURLConnection con = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection(); "https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
con.setRequestMethod("GET"); con.setRequestMethod("GET");
spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
} catch (Exception ex) { } catch (Exception ex) {
plugin.getLogger().info("Failed to check for updates on spigot."); plugin.getLogger().info("Failed to check for updates on spigot.");
return false; return;
} }
if (spigotVersion == null || spigotVersion.isEmpty()) { if (spigotVersion == null || spigotVersion.isEmpty()) {
plugin.getLogger().info("Failed to check for updates on spigot."); return;
return false;
} }
updateAvailable = spigotIsNewer(); updateAvailable = spigotIsNewer();
if (!updateAvailable) { if (!updateAvailable) {
plugin.getLogger().info("PlaceholderAPI is up to date."); return;
return false;
} }
return true; Bukkit.getScheduler().runTask(plugin, () -> {
});
update.whenComplete((result, error) -> {
if (result) {
plugin.getLogger() plugin.getLogger()
.info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:"); .info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
plugin.getLogger() plugin.getLogger()
.info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/"); .info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
Bukkit.getPluginManager().registerEvents(UpdateChecker.this, plugin); Bukkit.getPluginManager().registerEvents(this, plugin);
} });
}); });
} }

View File

@@ -40,7 +40,11 @@ public final class Futures {
@NotNull final CompletableFuture<T> future, @NotNull final CompletableFuture<T> future,
@NotNull final BiConsumer<T, Throwable> consumer) { @NotNull final BiConsumer<T, Throwable> consumer) {
future.whenComplete((value, exception) -> { future.whenComplete((value, exception) -> {
consumer.accept(value, exception); if (Bukkit.isPrimaryThread()) {
consumer.accept(value, exception);
} else {
Bukkit.getScheduler().runTask(plugin, () -> consumer.accept(value, exception));
}
}); });
} }

View File

@@ -4,8 +4,6 @@ main: "me.clip.placeholderapi.PlaceholderAPIPlugin"
version: ${version} version: ${version}
author: HelpChat author: HelpChat
folia-supported: true
api-version: "1.13" api-version: "1.13"
description: "An awesome placeholder provider!" description: "An awesome placeholder provider!"