Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 15 additions & 30 deletions src/main/java/org/spdx/utility/license/LicenseExpressionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<String, Operator> 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);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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<String> 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<String> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading