Skip to content

Commit

Permalink
feat: Improved blacklist pattern load
Browse files Browse the repository at this point in the history
- Use an Array instead of Collection for Patterns
- Improved invalid configuration check
  • Loading branch information
4drian3d committed Apr 25, 2022
1 parent 78ce6a0 commit 9cf53f2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
Expand Up @@ -20,7 +20,7 @@ public static class Config{
Sets the expressions to be checked in the
Infractions module in commands and general chat""")
@Setting(value = "blocked-words")
private Set<Pattern> blockedPatterns = Set.of(
private Pattern[] blockedPatterns = {
Pattern.compile("f(u|v|4)ck", Pattern.CASE_INSENSITIVE),
Pattern.compile("sh(i|@|l|j|1|y)t", Pattern.CASE_INSENSITIVE),
Pattern.compile("d(i|@|l|j|1|y)c(k)?", Pattern.CASE_INSENSITIVE),
Expand All @@ -31,7 +31,7 @@ public static class Config{
Pattern.compile("(i|@|l|j|1|y)mb(3|@|e|x)c(i|@|l|j|1|y)l", Pattern.CASE_INSENSITIVE),
Pattern.compile("m(o|@|0|x|8)th(3|@|e|x)rf(u|@|v)ck(3|@|e|x)r", Pattern.CASE_INSENSITIVE),
Pattern.compile("\\$\\{(jndi|log4j|sys|env|main|marker|java|base64|lower|upper|web|docker|kubernetes|spring|jvmrunargs|date|ctx)\\:.*\\}", Pattern.CASE_INSENSITIVE)
);
};

@Comment("""
Sets the commands that cannot be executed
Expand All @@ -52,7 +52,7 @@ public static class Config{
* Get the blocked regex strings
* @return the blocked regex strings
*/
public Set<Pattern> getBlockedPatterns(){
public Pattern[] getBlockedPatterns(){
return this.blockedPatterns;
}

Expand Down
Expand Up @@ -31,8 +31,8 @@ private Configuration(){}
* @param logger plugin logger
*/
public static void loadConfig(@NotNull Path path, @NotNull Logger logger){
Objects.requireNonNull(path, () ->"plugin path");
Objects.requireNonNull(logger, () -> "plugin logger");
Objects.requireNonNull(path, "plugin path");
Objects.requireNonNull(logger, "plugin logger");
loadMainConfig(path, logger);
loadMessagesConfig(path, logger);
loadBlacklistConfig(path, logger);
Expand Down Expand Up @@ -115,21 +115,22 @@ private static void loadBlacklistConfig(Path path, Logger logger){
node.set(Blacklist.Config.class, blacklist);
loader.save(node);
} catch (ConfigurateException exception){
if(checkConfig(path, "blacklist.conf")){
if(checkConfig(blacklistConfig)){
logger.error("Your blacklist configuration contains '\\' character. Please change all of its usage for '\\\\'");
}
logger.error("Could not load blacklist.conf file, error: {}", exception.getMessage());
}
}

private static boolean checkConfig(Path path, String name){
final Path configFile = path.resolve(name);
try (BufferedReader reader = Files.newBufferedReader(configFile, StandardCharsets.UTF_8)) {
while(true){
String line = reader.readLine();
if(line == null) return false;
if(line.contains("\\") && !line.contains("\\\\")) return true;
private static boolean checkConfig(final Path path) {
try (final BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line;
while((line = reader.readLine()) != null){
if(line.indexOf('\\') != -1 && !line.contains("\\\\")) {
return true;
}
}
return false;
} catch(IOException e){
return false;
}
Expand Down
Expand Up @@ -21,14 +21,17 @@
* Utilities for the detection of restringed words
*/
public final class InfractionCheck implements ICheck {
private final Collection<Pattern> blockedWords;
private final Pattern[] blockedWords;
private final boolean blockable;

private InfractionCheck(){
this(Configuration.getConfig().getInfractionsConfig().isBlockable(), Configuration.getBlacklist().getBlockedPatterns());
this(
Configuration.getConfig().getInfractionsConfig().isBlockable(),
Configuration.getBlacklist().getBlockedPatterns()
);
}

private InfractionCheck(boolean blockable, Collection<Pattern> blockedWords){
private InfractionCheck(boolean blockable, Pattern... blockedWords){
this.blockedWords = blockedWords;
this.blockable = blockable;
}
Expand Down Expand Up @@ -118,12 +121,12 @@ public Builder replaceable(boolean replaceable){
@Override
public InfractionCheck build(){
if(this.blockedWords == null){
this.blockedWords = new ArrayList<>(Configuration.getBlacklist().getBlockedPatterns());
this.blockedWords = List.of(Configuration.getBlacklist().getBlockedPatterns());
}
if(!edited){
this.replaceable = Configuration.getConfig().getInfractionsConfig().getControlType() == ControlType.REPLACE;
}
return new InfractionCheck(!replaceable, blockedWords);
return new InfractionCheck(!replaceable, blockedWords.toArray(Pattern[]::new));
}
}
}
10 changes: 8 additions & 2 deletions src/main/java/me/dreamerzero/chatregulator/utils/DebugUtils.java
Expand Up @@ -22,14 +22,20 @@ public final class DebugUtils {
* @param detection the detection type
* @param result the result
*/
public static void debug(InfractionPlayer infractor, String string, InfractionType detection, Result result, ChatRegulator plugin){
public static void debug(
InfractionPlayer infractor,
String string,
InfractionType detection,
Result result,
ChatRegulator plugin
) {

final Logger logger = plugin.getLogger();
if(logger.isDebugEnabled()){
logger.debug("User Detected: {}", infractor.username());
logger.debug("Detection: {}", detection);
logger.debug("String: {}", string);
if(result instanceof PatternResult patternResult){
if(result instanceof final PatternResult patternResult){
final Pattern pattern = patternResult.getPattern();
if(pattern != null)
logger.debug("Pattern: {}", pattern.pattern());
Expand Down
Expand Up @@ -24,9 +24,9 @@ public final class PlaceholderUtils {
* @param player the {@link InfractionPlayer}
* @return placeholders based on this player
*/
public static @NotNull TagResolver getPlaceholders(@NotNull final InfractionPlayer player){
public static @NotNull TagResolver getPlaceholders(final @NotNull InfractionPlayer player){
final ViolationCount count = Objects.requireNonNull(player).getViolations();
TagResolver.Builder resolver = TagResolver.builder().resolvers(
final TagResolver.Builder resolver = TagResolver.builder().resolvers(
Placeholder.unparsed("player", player.username()),
Placeholder.unparsed("name", player.username()),
integerPlaceholder("flood", count.getCount(InfractionType.FLOOD)),
Expand All @@ -51,7 +51,7 @@ public final class PlaceholderUtils {
* @return global placeholders
*/
public static @NotNull TagResolver getGlobalPlaceholders(){
Statistics statistics = Statistics.getStatistics();
final Statistics statistics = Statistics.getStatistics();
return TagResolver.resolver(
integerPlaceholder("flood", statistics.getViolationCount(InfractionType.FLOOD)),
integerPlaceholder("spam", statistics.getViolationCount(InfractionType.SPAM)),
Expand Down

0 comments on commit 9cf53f2

Please sign in to comment.