Skip to content

Commit

Permalink
Respect ProxyConfig when highlighting JWT's (Thanks to @serate-actual).
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Jul 11, 2024
1 parent bcc3a8a commit 84ad225
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/java/burp/proxy/AnnotationsModifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class AnnotationsModifier {
}

void updateAnnotationsIfApplicable(Annotations annotations, ByteArray data) {
String message = byteUtils.convertToString(data.getBytes());
updateAnnotationsIfApplicable(annotations, message);
if (proxyConfig.highlightJWT()) {
String message = byteUtils.convertToString(data.getBytes());
updateAnnotationsIfApplicable(annotations, message);
}
}

void updateAnnotationsIfApplicable(Annotations annotations, String message) {
updateAnnotations(annotations, message);
if (proxyConfig.highlightJWT()) {
updateAnnotations(annotations, message);
}
}

private void updateAnnotations(Annotations annotations, String messageString) {
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/burp/api/montoya/core/FakeAnnotations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package burp.api.montoya.core;

public class FakeAnnotations implements Annotations {
private String notes;
private HighlightColor highlightColor;

@Override
public String notes() {
return notes;
}

@Override
public void setNotes(String notes) {
this.notes = notes;
}

@Override
public HighlightColor highlightColor() {
return highlightColor;
}

@Override
public void setHighlightColor(HighlightColor highlightColor) {
this.highlightColor = highlightColor;
}

@Override
public Annotations withNotes(String s) {
throw new UnsupportedOperationException();
}

@Override
public Annotations withHighlightColor(HighlightColor highlightColor) {
throw new UnsupportedOperationException();
}
}
63 changes: 63 additions & 0 deletions src/test/java/burp/api/montoya/utilities/FakeByteUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package burp.api.montoya.utilities;

import static java.nio.charset.StandardCharsets.UTF_8;

public class FakeByteUtils implements ByteUtils{
@Override
public int indexOf(byte[] bytes, byte[] bytes1) {
throw new UnsupportedOperationException();
}

@Override
public int indexOf(byte[] bytes, byte[] bytes1, boolean b) {
throw new UnsupportedOperationException();
}

@Override
public int indexOf(byte[] bytes, byte[] bytes1, boolean b, int i, int i1) {
throw new UnsupportedOperationException();
}

@Override
public int countMatches(byte[] bytes, byte[] bytes1) {
throw new UnsupportedOperationException();
}

@Override
public int countMatches(byte[] bytes, byte[] bytes1, boolean b) {
throw new UnsupportedOperationException();
}

@Override
public int countMatches(byte[] bytes, byte[] bytes1, boolean b, int i, int i1) {
throw new UnsupportedOperationException();
}

@Override
public String convertToString(byte[] bytes) {
return new String(bytes, UTF_8);
}

@Override
public byte[] convertFromString(String s) {
throw new UnsupportedOperationException();
}
}
99 changes: 99 additions & 0 deletions src/test/java/burp/proxy/AnnotationsModifierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Author : Dolph Flynn
Copyright 2024 Dolph Flynn
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package burp.proxy;

import burp.api.montoya.core.Annotations;
import burp.api.montoya.core.FakeAnnotations;
import burp.api.montoya.core.FakeByteArray;
import burp.api.montoya.utilities.ByteUtils;
import burp.api.montoya.utilities.FakeByteUtils;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static burp.proxy.HighlightColor.GREEN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class AnnotationsModifierTest {
private final ProxyConfig config = new ProxyConfig();
private final ByteUtils byteUtils = new FakeByteUtils();
private final AnnotationsModifier annotationsModifier = new AnnotationsModifier(config, byteUtils);

private static Stream<Arguments> data() {
return Stream.of(
arguments("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJUZXN0In0.Nabf3xakZubPnCzHT-fx0vG1iuNPeJKuSzHxUiQKf-8", "1 JWTs, 0 JWEs"),
arguments("eyJlbmMiOiJBMTI4R0NNIiwiYWxnIjoiQTEyOEtXIn0.H3X6mT5HLgcFfzLoe4ku6Knhh9Ofv1eL.qF5-N_7K8VQ4yMSz.WXUNY6eg5fR4tc8Hqf5XDRM9ALGwcQyYG4IYwwg8Ctkx1UuxoV7t6UnemjzCj2sOYUqi3KYpDzrKVJpzokz0vcIem4lFe5N_ds8FAMpW0GSF9ePA8qvV99WaP0N2ECVPmgihvL6qwNhdptlLKtxcOpE41U5LnU22voPK55VF4_1j0WmTgWgZ7DwLDysp6EIDjrrt-DY.febBmP71KADmKRVfeSnv_g", "0 JWTs, 1 JWEs")
);
}

@MethodSource("data")
@ParameterizedTest
void givenJWTStrings_whenHighlightJWTTrue_thenCommentAndHighlightUpdated(String data, String expectedComment) {
Annotations annotations = new FakeAnnotations();
config.setHighlightColor(GREEN);
config.setHighlightJWT(true);

annotationsModifier.updateAnnotationsIfApplicable(annotations, data);

assertThat(annotations.highlightColor()).isEqualTo(GREEN.burpColor);
assertThat(annotations.notes()).isEqualTo(expectedComment);
}

@MethodSource("data")
@ParameterizedTest
void givenJWTBytes_whenHighlightJWTTrue_thenCommentAndHighlightUpdated(String data, String expectedComment) {
Annotations annotations = new FakeAnnotations();
config.setHighlightColor(GREEN);
config.setHighlightJWT(true);

annotationsModifier.updateAnnotationsIfApplicable(annotations, new FakeByteArray(data));

assertThat(annotations.highlightColor()).isEqualTo(GREEN.burpColor);
assertThat(annotations.notes()).isEqualTo(expectedComment);
}

@MethodSource("data")
@ParameterizedTest
void givenJWTStrings_whenHighlightJWTFalse_thenCommentAndHighlightNotSet(String data, String expectedComment) {
Annotations annotations = new FakeAnnotations();
config.setHighlightColor(GREEN);
config.setHighlightJWT(false);

annotationsModifier.updateAnnotationsIfApplicable(annotations, data);

assertThat(annotations.highlightColor()).isNull();
assertThat(annotations.notes()).isNull();
}

@MethodSource("data")
@ParameterizedTest
void givenJWTBytes_whenHighlightJWTFalse_thenCommentAndHighlightNotSet(String data, String expectedComment) {
Annotations annotations = new FakeAnnotations();
config.setHighlightColor(GREEN);
config.setHighlightJWT(false);

annotationsModifier.updateAnnotationsIfApplicable(annotations, new FakeByteArray(data));

assertThat(annotations.highlightColor()).isNull();
assertThat(annotations.notes()).isNull();
}
}

0 comments on commit 84ad225

Please sign in to comment.