Filter beacon responses

This commit is contained in:
crschnick 2024-07-13 06:44:05 +00:00
parent 0527dbee39
commit ceee5d56b1
2 changed files with 36 additions and 7 deletions

View file

@ -124,10 +124,9 @@ public class BeaconRequestHandler<T> implements HttpHandler {
try { try {
var emptyResponseClass = beaconInterface.getResponseClass().getDeclaredFields().length == 0; var emptyResponseClass = beaconInterface.getResponseClass().getDeclaredFields().length == 0;
if (!emptyResponseClass && response != null) { if (!emptyResponseClass && response != null) {
TrackEvent.trace("Sending response:\n" + object); TrackEvent.trace("Sending response:\n" + response);
var tree = JacksonMapper.getDefault().valueToTree(response); TrackEvent.trace("Sending raw response:\n" + JacksonMapper.getCensored().valueToTree(response).toPrettyString());
TrackEvent.trace("Sending raw response:\n" + tree.toPrettyString()); var bytes = JacksonMapper.getDefault().valueToTree(response).toPrettyString().getBytes(StandardCharsets.UTF_8);
var bytes = tree.toPrettyString().getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(200, bytes.length); exchange.sendResponseHeaders(200, bytes.length);
try (OutputStream os = exchange.getResponseBody()) { try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes); os.write(bytes);

View file

@ -1,13 +1,15 @@
package io.xpipe.core.util; package io.xpipe.core.util;
import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.Module; import com.fasterxml.jackson.databind.Module;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lombok.Getter; import lombok.Getter;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -88,4 +90,32 @@ public class JacksonMapper {
return INSTANCE; return INSTANCE;
} }
public static ObjectMapper getCensored() {
if (!JacksonMapper.isInit()) {
return BASE;
}
var c = INSTANCE.copy();
c.registerModule(new SimpleModule() {
@Override
public void setupModule(SetupContext context) {
addSerializer(SecretValue.class, new JsonSerializer<>() {
@Override
public void serialize(SecretValue value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString("<secret>");
}
@Override
public void serializeWithType(SecretValue value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws
IOException {
gen.writeString("<secret>");
}
});
super.setupModule(context);
}
});
return c;
}
} }