From 2776d6c195423f945f451c382db209a423aa0f4d Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Mon, 23 Nov 2026 12:23:48 -0800 Subject: [PATCH 1/2] Prevent excessive recursions in expression parser Prevents possible stack overflow with very large expressions or expressions that have a large number of parens or pluses. Changes the tokenizer to use streams. Adds a cap to the number of tokens - currently, 1,000. Signed-off-by: Gary O'Neall --- .../license/LicenseExpressionParser.java | 45 +++++++------------ 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/spdx/utility/license/LicenseExpressionParser.java b/src/main/java/org/spdx/utility/license/LicenseExpressionParser.java index 07dc2e229..e5685a6b8 100644 --- a/src/main/java/org/spdx/utility/license/LicenseExpressionParser.java +++ b/src/main/java/org/spdx/utility/license/LicenseExpressionParser.java @@ -26,6 +26,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Stack; +import java.util.regex.Pattern; import javax.annotation.Nullable; @@ -79,11 +80,13 @@ enum Operator { OR_LATER, WITH, AND, OR //NOTE: These must be in precedence order } + static final int MAX_TOKENS = 1000; // Maximum number of tokens the parser will handle - limit prevents a large number of recursive calls static final String LEFT_PAREN = "("; static final String RIGHT_PAREN = ")"; static final Map OPERATOR_MAP = new HashMap<>(); public static final String UNINITIALIZED_LICENSE_TEXT = "[Initialized with license Parser. The actual license text is not available]"; - + static final Pattern TOKEN_SPLIT_PATTERN = Pattern.compile("[\\s()+]+"); + static { OPERATOR_MAP.put("+", Operator.OR_LATER); OPERATOR_MAP.put("AND", Operator.AND); @@ -123,6 +126,10 @@ public static AnyLicenseInfo parseLicenseExpression(String expression, IModelSto return new NoAssertionLicense(); } else if (tokens.length == 1 && tokens[0].equals(SpdxConstantsCompatV2.NONE_VALUE)) { return new NoneLicense(); + } else if (tokens.length > MAX_TOKENS) { + throw new LicenseParserException(String.format( + "License expression includes %s tokens which exceeds the maximum %s tokens.", + tokens.length, MAX_TOKENS)); } else { try { return parseLicenseExpression(tokens, store, customLicenseUriPrefix, creationInfo, @@ -159,6 +166,10 @@ public static org.spdx.library.model.v2.license.AnyLicenseInfo parseLicenseExpre return new SpdxNoAssertionLicense(store, documentUri); } else if (tokens.length == 1 && tokens[0].equals(SpdxConstantsCompatV2.NONE_VALUE)) { return new SpdxNoneLicense(store, documentUri); + } else if (tokens.length > MAX_TOKENS) { + throw new LicenseParserException(String.format( + "License expression includes %s tokens which exceeds the maximum %s tokens.", + tokens.length, MAX_TOKENS)); } else { try { return parseLicenseExpressionCompatV2(tokens, store, documentUri, copyManager); @@ -177,35 +188,9 @@ public static org.spdx.library.model.v2.license.AnyLicenseInfo parseLicenseExpre * @return array of string tokens */ private static String[] tokenizeExpression(String expression) { - String[] startTokens = expression.split("\\s"); - List endTokens = new ArrayList<>(); - for (String token : startTokens) { - processPreToken(token, endTokens); - } - return endTokens.toArray(new String[0]); - } - - /** - * @param preToken previous token - * @param tokenList resultant list of tokens - modified by this methods - */ - private static void processPreToken(String preToken, - List tokenList) { - if (preToken.isEmpty()) { - //noinspection UnnecessaryReturnStatement - return; - } else if (preToken.startsWith("(")) { - tokenList.add("("); - processPreToken(preToken.substring(1), tokenList); - } else if (preToken.endsWith(")")) { - processPreToken(preToken.substring(0, preToken.length()-1), tokenList); - tokenList.add(")"); - } else if (preToken.endsWith("+")) { - processPreToken(preToken.substring(0, preToken.length()-1), tokenList); - tokenList.add("+"); - } else { - tokenList.add(preToken); - } + return Arrays.stream(expression.split("\\s+|(?=[()+])|(?<=[()+])")) + .filter(s -> !s.isEmpty()) + .toArray(String[]::new); } /** From 64a1616664bf304856b6859944cade6b0d0a2de4 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Tue, 24 Nov 2026 11:50:39 -0800 Subject: [PATCH 2/2] Add unit tests for large expressions Signed-off-by: Gary O'Neall --- .../license/LicenseExpressionParserTest.java | 24 +++++++++++++++++++ .../LicenseExpressionParserTestV2.java | 22 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java b/src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java index 84f220636..797def779 100644 --- a/src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java +++ b/src/test/java/org/spdx/utility/license/LicenseExpressionParserTest.java @@ -413,4 +413,28 @@ public void testInvalidExceptionOnly() throws InvalidSPDXAnalysisException { assertTrue(result instanceof InvalidLicenseExpression); assertEquals(STD_EXCEPTION_IDS[0], ((InvalidLicenseExpression)result).getLicenseExpression()); } + + public void testLargeExpression() throws InvalidSPDXAnalysisException { + StringBuilder sb = new StringBuilder("GPL-2.0-only"); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append(" OR Apache-2.0"); + } + AnyLicenseInfo result = LicenseInfoFactory.parseSPDXLicenseString(sb.toString(), modelStore, + DEFAULT_PREFIX, creationInfo, copyManager, idMap); + assertTrue(result instanceof InvalidLicenseExpression); + } + + public void testLargeNesting() throws InvalidSPDXAnalysisException { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append("("); + } + sb.append("GPL-2.0-only AND Apache-2.0"); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append(")"); + } + AnyLicenseInfo result = LicenseInfoFactory.parseSPDXLicenseString(sb.toString(), modelStore, + DEFAULT_PREFIX, creationInfo, copyManager, idMap); + assertTrue(result instanceof InvalidLicenseExpression); + } } diff --git a/src/test/java/org/spdx/utility/license/LicenseExpressionParserTestV2.java b/src/test/java/org/spdx/utility/license/LicenseExpressionParserTestV2.java index e6611139b..7824f991a 100644 --- a/src/test/java/org/spdx/utility/license/LicenseExpressionParserTestV2.java +++ b/src/test/java/org/spdx/utility/license/LicenseExpressionParserTestV2.java @@ -251,4 +251,26 @@ public void testInvalidExceptionOnly() throws InvalidSPDXAnalysisException { assertTrue(result instanceof InvalidLicenseExpression); assertEquals("389-exception", ((InvalidLicenseExpression)result).getLicenseExpression()); } + + public void testLargeExpression() throws InvalidSPDXAnalysisException { + StringBuilder sb = new StringBuilder("GPL-2.0-only"); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append(" OR Apache-2.0"); + } + AnyLicenseInfo result = LicenseInfoFactory.parseSPDXLicenseStringCompatV2(sb.toString()); + assertTrue(result instanceof InvalidLicenseExpression); + } + + public void testLargeNesting() throws InvalidSPDXAnalysisException { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append("("); + } + sb.append("GPL-2.0-only AND Apache-2.0"); + for (int i = 0; i < LicenseExpressionParser.MAX_TOKENS + 100; i++) { + sb.append(")"); + } + AnyLicenseInfo result = LicenseInfoFactory.parseSPDXLicenseStringCompatV2(sb.toString()); + assertTrue(result instanceof InvalidLicenseExpression); + } }