This is an automated email from the ASF dual-hosted git repository.
chibenwa pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-jspf.git
The following commit(s) were added to refs/heads/master by this push:
new 6857344 [FIX] CachingSPFRecordParser: do not keep the records with
more terms than a legitimate one
6857344 is described below
commit 685734424a68f708671410e10b3cbb355a001c9b
Author: Benoit TELLIER <[email protected]>
AuthorDate: Fri Nov 20 19:26:16 2026 +0100
[FIX] CachingSPFRecordParser: do not keep the records with more terms than
a legitimate one
---
.../james/jspf/parser/CachingSPFRecordParser.java | 27 +++++++++++++++++++++-
.../jspf/parser/CachingSPFRecordParserTest.java | 25 ++++++++++++++++++++
2 files changed, 51 insertions(+), 1 deletion(-)
diff --git
a/resolver/src/main/java/org/apache/james/jspf/parser/CachingSPFRecordParser.java
b/resolver/src/main/java/org/apache/james/jspf/parser/CachingSPFRecordParser.java
index 0b7b998..b913749 100644
---
a/resolver/src/main/java/org/apache/james/jspf/parser/CachingSPFRecordParser.java
+++
b/resolver/src/main/java/org/apache/james/jspf/parser/CachingSPFRecordParser.java
@@ -38,7 +38,9 @@ import
org.apache.james.jspf.core.exceptions.PermErrorException;
* holding these terms, as the policies add modifiers to the record they get.
*
* The records are the sender's: the cache keeps at most a given number of
them, the least
- * recently used ones being dropped, and not the longer ones nor the invalid
ones.
+ * recently used ones being dropped, and not the longer ones, the ones with
more terms than a
+ * legitimate record has nor the invalid ones: a short text can hold many
terms, each one kept as
+ * several objects.
*/
public class CachingSPFRecordParser implements SPFRecordParser {
@@ -53,10 +55,18 @@ public class CachingSPFRecordParser implements
SPFRecordParser {
*/
public static final int DEFAULT_MAX_RECORD_LENGTH = 2048;
+ /**
+ * The number of terms, directives and modifiers, of the largest record
kept by default: more
+ * than the legitimate records have, the evaluation of a record being
limited to 10 lookups
+ */
+ public static final int DEFAULT_MAX_RECORD_TERMS = 64;
+
private final SPFRecordParser parser;
private final int maxRecordLength;
+ private final int maxRecordTerms;
+
private final Map<String, SPF1Record> records;
/**
@@ -72,8 +82,19 @@ public class CachingSPFRecordParser implements
SPFRecordParser {
* @param maxRecordLength the length of the longest record kept
*/
public CachingSPFRecordParser(SPFRecordParser parser, final int
maxRecords, int maxRecordLength) {
+ this(parser, maxRecords, maxRecordLength, DEFAULT_MAX_RECORD_TERMS);
+ }
+
+ /**
+ * @param parser the parser of the records not cached
+ * @param maxRecords the number of records kept at most
+ * @param maxRecordLength the length of the longest record kept
+ * @param maxRecordTerms the number of terms of the largest record kept
+ */
+ public CachingSPFRecordParser(SPFRecordParser parser, final int
maxRecords, int maxRecordLength, int maxRecordTerms) {
this.parser = parser;
this.maxRecordLength = maxRecordLength;
+ this.maxRecordTerms = maxRecordTerms;
this.records = new LinkedHashMap<String, SPF1Record>(16, 0.75f, true) {
private static final long serialVersionUID = 1L;
@@ -98,6 +119,10 @@ public class CachingSPFRecordParser implements
SPFRecordParser {
if (parsed == null) {
// an invalid record throws: it is not kept
parsed = parser.parse(spfRecord);
+ if (parsed.getDirectives().size() + parsed.getModifiers().size() >
maxRecordTerms) {
+ // a record with that many terms is not a legitimate one: it
is not kept
+ return parsed;
+ }
synchronized (records) {
records.put(spfRecord, parsed);
}
diff --git
a/resolver/src/test/java/org/apache/james/jspf/parser/CachingSPFRecordParserTest.java
b/resolver/src/test/java/org/apache/james/jspf/parser/CachingSPFRecordParserTest.java
index 9a444b9..08d3046 100644
---
a/resolver/src/test/java/org/apache/james/jspf/parser/CachingSPFRecordParserTest.java
+++
b/resolver/src/test/java/org/apache/james/jspf/parser/CachingSPFRecordParserTest.java
@@ -130,6 +130,31 @@ public class CachingSPFRecordParserTest {
assertEquals(0, parser.size());
}
+ @Test
+ public void shouldNotKeepARecordWithManyTerms() throws Exception {
+ CachingSPFRecordParser parser = new CachingSPFRecordParser(counting);
+ StringBuilder record = new StringBuilder("v=spf1");
+ for (int i = 0; i < 200; i++) {
+ record.append(" a");
+ }
+
+ assertEquals(200,
parser.parse(record.toString()).getDirectives().size());
+ parser.parse(record.toString());
+
+ assertEquals(2, counting.parsed.get());
+ assertEquals(0, parser.size());
+ }
+
+ @Test
+ public void shouldKeepARecordWithFewTerms() throws Exception {
+ CachingSPFRecordParser parser = new CachingSPFRecordParser(counting);
+
+ parser.parse("v=spf1 a mx ip4:192.0.2.0/24 ip4:198.51.100.0/24
include:a.example.com include:b.example.com"
+ + " a:c.example.com mx:d.example.com exists:e.example.com -all");
+
+ assertEquals(1, parser.size());
+ }
+
@Test
public void shouldKeepAtMostTheGivenNumberOfRecords() throws Exception {
CachingSPFRecordParser parser = new CachingSPFRecordParser(counting,
2, 2048);
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]