This commit is contained in:
Vadim Shulkin 2025-05-08 17:16:33 -04:00
parent 7cb3caa285
commit 5b4b7f96eb
3 changed files with 24 additions and 21 deletions

View File

@ -27,18 +27,23 @@ import org.graylog.plugins.pipelineprocessor.ast.functions.FunctionDescriptor;
import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor; import org.graylog.plugins.pipelineprocessor.ast.functions.ParameterDescriptor;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.InputStreamReader;
import java.io.IOException; import java.io.BufferedReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.zip.Inflater; import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;
import java.net.URLDecoder; import java.net.URLDecoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// @Function(name = "Base64Inflate", description = "Decodes a URL-safe Base64-encoded and GZIP-compressed string") // @Function(name = "Base64Inflate", description = "Decodes a URL-safe Base64-encoded and GZIP-compressed string")
public class Base64InflateFunction extends AbstractFunction<String> { public class Base64InflateFunction extends AbstractFunction<String> {
private static final Logger LOG = LoggerFactory.getLogger(Base64InflateFunction.class);
public static final String NAME = "urlsafe_base64_decode"; public static final String NAME = "urlsafe_base64_decode";
private final ParameterDescriptor<String, String> inputParam = ParameterDescriptor private final ParameterDescriptor<String, String> inputParam = ParameterDescriptor
@ -57,30 +62,28 @@ public class Base64InflateFunction extends AbstractFunction<String> {
.build(); .build();
} }
@Override @Override
public String evaluate(FunctionArgs args, EvaluationContext context) { public String evaluate(FunctionArgs args, EvaluationContext context) {
final String input = inputParam.required(args, context); final String input = inputParam.required(args, context);
try { try {
String urlDecoded = URLDecoder.decode(input, StandardCharsets.UTF_8.name()); LOG.debug("Received input for Base64Inflate: {}", input);
byte[] base64Decoded = Base64.getDecoder().decode(urlDecoded); String cleanInput = URLDecoder.decode(input, StandardCharsets.UTF_8.name());
byte[] decoded = Base64.getUrlDecoder().decode(cleanInput);
try (InflaterInputStream inflater = new InflaterInputStream( try (GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(decoded))) {
new ByteArrayInputStream(base64Decoded), new Inflater(true)); byte[] uncompressed = gis.readAllBytes();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { String result = new String(uncompressed, StandardCharsets.UTF_8);
LOG.debug("Decompressed result: {}", result);
byte[] buffer = new byte[1024]; return result;
int len;
while ((len = inflater.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
return outputStream.toString(StandardCharsets.UTF_8.name());
} }
} catch (IOException | IllegalArgumentException e) { } catch (Exception e) {
e.printStackTrace(); LOG.error("Base64Inflate failed: {}", e.getMessage(), e);
return null; return null;
} }
} }
} }