Repository navigation
fix(license): parse SPDX license expressions - #295
Conversation
The license check split values on AND, OR and WITH and dropped every parenthesis, so malformed expressions were not caught: "(MIT OR Apache-2.0" was allowed, and "MIT AND" or "MIT OR OR Apache-2.0" only produced a manual-review warning. SPDX version forms of copyleft licenses were not recognized either: "GPL-3.0+", "AGPL-3.0+", "LGPL-2.1-only" and "LGPL-2.1+" passed as unknown licenses (a warning in the default non-strict mode) although their base licenses are blocked. WITH exceptions were matched as licenses, so "Apache-2.0 WITH LLVM-exception" needed review, and an SPDX header inside Markdown inline code or a table cell kept the closing backtick or "|", hiding a blocked license as an unknown one. Parse the value with a small SPDX expression grammar (AND, OR, WITH, parentheses). A value that uses an upper-case SPDX operator but does not parse fails with a HIGH unsupported_license_expression finding; any other value is one free-text name, so "MIT License" still matches MIT and "MIT License (MIT)" still goes to review. Normalize the SPDX "+" suffix to "-or-later" and treat "-only" and "-or-later" forms of a blocked license as blocked. Evaluate WITH exceptions against a new allowed_license_exceptions config list (LLVM-exception, Swift-exception); other exceptions need review and a blocked base license stays blocked. Compare LICENSE file ids with the licenses named in a frontmatter expression, so an expression that includes the file's license is consistent with it and an allowed expression that leaves it out is a conflict, as a single id already was. In SPDX headers, cut the value at a closing backtick or "|" only when the same delimiter opens before the tag, and drop a trailing ";". No new dependency: the base install stays limited to Tier 1 needs, and a full SPDX-library parser would reject free-text names such as "MIT License" that pass today. Signed-off-by: Narendran Raghavan <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
A value that does not parse as an SPDX expression and has no upper-case operator, such as "GPL-3.0 (or later)" or "mit or gpl-3.0)", was reviewed as one unknown name. Unknown licenses are only a warning in the default mode, so a blocked license inside such a value passed Tier 1. Keep every name between operators and parentheses for the blocked check, whether or not the value parses. A malformed upper-case expression that names a blocked license, such as "(GPL-3.0 OR MIT", is now reported as blocked_license naming that license, with the syntax error as a message. Also block the deprecated SPDX ids that build an exception into a blocked license id, such as GPL-2.0-with-classpath-exception. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Narendran Raghavan <[email protected]>
rng1995
left a comment
There was a problem hiding this comment.
Review summary: no findings.
Checked the expression grammar (operator/paren state machine, free-text fallback when no upper-case operator is present), the fail-closed blocked check over every name including malformed values, the -only/-or-later/+/-with-*-exception variants against the blocklist, the allowed_license_exceptions handling, the frontmatter-vs-LICENSE-file reconciliation change (subset of declared names), the _strip_spdx_capture delimiter handling, and the CHANGELOG/docs/config entries. I also probed a set of expressions by hand (GPL-3.0+, nested groups with a blocked member, dangling WITH/OR, unbalanced parens, Apache-2.0 WITH LLVM-exception OR GPL-3.0) and each classified as described. tests/validators/test_license.py passes locally (96 passed) and ruff check is clean.
Reviewed with Claude Code.
cd2c951 to
ececd05
Compare
chrisknvidia
left a comment
There was a problem hiding this comment.
I read the expression grammar, the blocked check, the LICENSE reconciliation change and the header extraction line by line and found nothing blocking. I also ran the old splitting logic and the new parser side by side over a few hundred thousand generated values: nothing that was blocked before is unblocked now, and nothing containing a blocked token comes out allowed. The only values that flip to allowed are parenthesized single ids and an allowed license with a listed exception, which is what the PR says.
Summary
The Tier 1 license check now parses SPDX license expressions instead of splitting them on
AND,ORandWITH. It also recognizes the SPDX version forms of blocked licenses.AND,OR,WITH, parentheses) replaces_expression_symbolsinvalidators/license.py. A value that uses an upper-case SPDX operator but does not parse (unbalanced parenthesis, dangling or repeated operator,WITHafter a group) gets a new HIGHunsupported_license_expressionfinding and theunsupportedlicense status, unless it names a blocked license. Any other value is treated as one free-text name, as before:MIT Licensestill matches MIT, andMIT License (MIT)still goes to review.GPL-3.0 (or later),(gpl-3.0 or mitand(GPL-3.0 OR MITstay blocked and the finding names the blocked license. For an upper-case expression, the syntax error is added as a message next to theblocked_licensefinding.+suffix is normalized to-or-later. The-onlyand-or-laterforms of a blocked license are blocked, soGPL-3.0+,AGPL-3.0+,GPL-2.0+,LGPL-2.1-only,LGPL-2.1+andMPL-2.0+fail instead of passing as unknown licenses. So do the deprecated SPDX ids with a built-in exception, such asGPL-2.0-with-classpath-exceptionandGPL-3.0-with-GCC-exception.WITHexceptions are checked separately from licenses. A newallowed_license_exceptionslist inlicense_config.yaml(LLVM-exception,Swift-exception) keepsApache-2.0 WITH LLVM-exceptionallowed. An unlisted exception, such asApache-2.0 WITH Commons-Clause, goes to review. A blocked base license stays blocked whatever exception follows it.MIT OR Apache-2.0next to an Apache LICENSE file is consistent. An allowed expression that leaves out the allowed file license, such asMIT AND CC-BY-4.0next to an Apache LICENSE file, is afrontmatter_license_mismatchconflict. This matches what a single identifier already got.|only when the same delimiter opens before the tag on that line (Markdown inline code or a table cell). It also drops a trailing;. A bareMIT | GPL-3.0header is left whole, so it is not shortened toMIT.Why
Tier 1 runs the license check by default, and it is part of the publication evidence. Before this change:
(MIT OR Apache-2.0unsupported_license_expressionmit or (apache-2.0(lower-case operator, unbalanced parenthesis)(MIT)MIT AND,OR MIT,MIT OR OR Apache-2.0,MIT WITHunsupported_license_expressionGPL-3.0+,AGPL-3.0+,GPL-2.0+,MIT OR GPL-3.0+LGPL-2.0-only,LGPL-2.1-only,LGPL-3.0-only,LGPL-2.1+,LGPL-3.0-or-laterGPL-2.0-with-classpath-exception,GPL-3.0-with-GCC-exceptionGPL-3.0 (or later),(gpl-3.0 or mit,mit or gpl-3.0),(GPL-3.0 OR MITApache-2.0 WITH LLVM-exceptionApache-2.0 WITH Commons-ClauseCommons-ClauseApache-2.0 WITH Commons-ClauseGPL-3.0+frontmatter next to an Apache LICENSE file`SPDX-License-Identifier: MIT OR GPL-3.0`in a Markdown table cellGPL-3.0with the closing backtick and pipe attached)MIT License,The MIT License,CC-BY-4.0 AND Apache-2.0,mit or apache-2.0In the default non-strict mode, an unknown license is only a warning. So copyleft licenses written in their
+or-onlyform passed Tier 1, and a malformed expression with a dropped parenthesis could be marked allowed.This follows up on the compound-expression fix for #86. That fix already covers blocked licenses inside
AND/ORexpressions.Kept from public
license-expressionpackage. Its strict SPDX parser rejects free-text names that pass today:MIT LicenseandThe MIT Licensebecome syntax errors and therefore HIGH failures. It would also add a base dependency to an install that is meant to cover Tier 1 only, plus lockfile churn. The in-tree grammar keeps public's name normalization (the-prefix,-licensesuffix, case, spaces, underscores) for anything that is not an SPDX expression. The+/-only/-or-laterrule gives the blocking a symbol table would give for the bundled blocklist, and also blocksLGPL-2.1+.SPDX_LICENSE_PATTERNstill captures the whole line (([^\n]+)) and_strip_spdx_capturetrims it. Headers followed by# commentor;are not dropped. A stricter lookahead pattern would drop them.AND/ORexpression blocks it, including one written afterWITHand one in a value that does not parse, such asGPL-3.0 (or later)ormit or gpl-3.0). A stricter parser that reports every malformed value only as a syntax error would hide which license is blocked, and a lower-case malformed value would drop to a review warning. Lower-case operators such asmit or apache-2.0are still accepted.Tests
tests/validators/test_license.pyadds 56 cases:AND/OR,(MIT),Apache-2.0 WITH LLVM-exception.+,-only,-or-later,WITHon a blocked base, and deprecated-with-*-exceptionids.GPL-3.0+next to an Apache LICENSE file is blocked.WITHexceptions go to review, and strict mode fails them.GPL-3.0 (or later),LGPL-2.1 (or any later version),(gpl-3.0 or mit,mit or (gpl-3.0,mit or gpl-3.0),MIT AND (GPL-3.0,(GPL-3.0 OR MIT. The upper-case cases also report the syntax error as a message.MIT License,MIT XOR Apache-2.0,MIT License (MIT),(MIT,mit or (apache-2.0) never become syntax errors, and malformed ones are not allowed.# comment, trailing;,WITHinside a block comment, and a bare|left whole.main. The rest guard behavior that must not change, including the five lower-case malformed values with a blocked license, whichmainalready blocks.pytest tests/validators tests/test_publication_evidence.py tests/test_progress_output.pyon currentmain: 2619 passed, 1 skipped. This includes the private plugin composition tests, which run the license check through the plugin checks.pytest tests/reporting/test_reporters.py tests/cli tests/test_commands.py tests/test_cli.py tests/test_plugin_*.py tests/test_oss_*.pyalso passes.pytest tests -n 8, on a loaded machine): 15464 passed, 23 skipped, 35 failed. None of the failures are in license code. Rerun one file at a time, 19 of them pass (timing-sensitive Harbor and bridge tests, and AWS preflight tests). The other 16 fail on unmodifiedmaintoo: the 2test_sidecar_retains_*tests intest_harbor_secure_docker_environment.pydepend on host environment variables; 13 tests intests/tier3/test_judge_failure_artifacts.pyrun a generated grader with the hostpython3, which is 3.9 here and rejectsint | float; andtest_streamed_timeout_callback_matches_result_and_contains_descendantsis a timing test that failed 2 of 10 runs onmain.ruff checkpasses on the changed Python files.ruff format --checkreports only formatting thatmainalready has inlicense.py, outside the changed lines. CI runsruff checkonly.scripts/check_oss_boundary.pypasses.🤖 Generated with Claude Code