Skip to content

Commit 0b7f335

Browse files
Merge pull request #275 from belfazt/add-a-verification-before-adding-the-content-to-the-mail-issue-269
Add a way to verify that the content doesn't contain sensitive information
2 parents 0f23918 + 43429da commit 0b7f335

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/main/java/com/sendgrid/helpers/mail/objects/Content.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
import com.fasterxml.jackson.annotation.JsonInclude.Include;
55
import com.fasterxml.jackson.annotation.JsonProperty;
66

7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
import java.util.regex.Pattern;
13+
import java.lang.IllegalArgumentException;
14+
15+
/**
16+
* An object in which you may specify the content of your email.
17+
*/
718
@JsonInclude(Include.NON_DEFAULT)
819
public class Content {
920
@JsonProperty("type") private String type;
@@ -33,8 +44,24 @@ public String getValue() {
3344
}
3445

3546
public void setValue(String value) {
47+
ContentVerifier.verifyContent(value);
3648
this.value = value;
3749
}
50+
}
51+
52+
class ContentVerifier {
53+
private static final List<Pattern> FORBIDDEN_PATTERNS = Collections.singletonList(
54+
Pattern.compile(".*SG\\.[a-zA-Z0-9(-|_)]*\\.[a-zA-Z0-9(-|_)]*.*")
55+
);
56+
57+
static void verifyContent(String content) {
58+
for (Pattern pattern: FORBIDDEN_PATTERNS) {
59+
if (pattern.matcher(content).matches()) {
60+
throw new IllegalArgumentException("Found a Forbidden Pattern in the content of the email");
61+
}
62+
}
63+
}
64+
}
3865

3966
@Override
4067
public int hashCode() {
@@ -66,4 +93,4 @@ public boolean equals(Object obj) {
6693
return false;
6794
return true;
6895
}
69-
}
96+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.sendgrid;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Rule;
6+
import org.junit.Test;
7+
import org.junit.rules.ExpectedException;
8+
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
12+
public class ContentTest {
13+
private Content content;
14+
15+
@Before
16+
public void setUp() {
17+
this.content = new Content();
18+
}
19+
20+
@Rule
21+
public final ExpectedException exception = ExpectedException.none();
22+
23+
@Test
24+
public void testForbiddenContentIsRejected() {
25+
26+
ArrayList<String> sampleApiKeys = new ArrayList<>(
27+
Arrays.asList(
28+
"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",
29+
"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"
30+
)
31+
32+
);
33+
34+
for (String apiKey: sampleApiKeys) {
35+
exception.expect(IllegalArgumentException.class);
36+
this.content.setValue("My api key is: " + apiKey);
37+
}
38+
}
39+
40+
@Test
41+
public void testNormalContentIsAllowed() {
42+
String message = "I will not send you my api key!";
43+
this.content.setValue(message);
44+
Assert.assertEquals(message, this.content.getValue());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)