Skip to content

fix(license): parse SPDX license expressions - #295

Merged
rng1995 merged 2 commits into
mainfrom
naren/port-spdx-compound
Nov 4, 2026
Merged

rng1995 merged 2 commits into
mainfrom
naren/port-spdx-compound

Conversation

@rng1995

@rng1995 rng1995 commented Oct 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

The Tier 1 license check now parses SPDX license expressions instead of splitting them on AND, OR and WITH. It also recognizes the SPDX version forms of blocked licenses.

  • A small in-tree SPDX expression grammar (AND, OR, WITH, parentheses) replaces _expression_symbols in validators/license.py. A value that uses an upper-case SPDX operator but does not parse (unbalanced parenthesis, dangling or repeated operator, WITH after a group) gets a new HIGH unsupported_license_expression finding and the unsupported license status, unless it names a blocked license. Any other value is treated as one free-text name, as before: MIT License still matches MIT, and MIT License (MIT) still goes to review.
  • A blocked license still blocks a value that does not parse. Every name between operators and parentheses is checked against the blocklist, so GPL-3.0 (or later), (gpl-3.0 or mit and (GPL-3.0 OR MIT stay blocked and the finding names the blocked license. For an upper-case expression, the syntax error is added as a message next to the blocked_license finding.
  • The SPDX + suffix is normalized to -or-later. The -only and -or-later forms of a blocked license are blocked, so GPL-3.0+, AGPL-3.0+, GPL-2.0+, LGPL-2.1-only, LGPL-2.1+ and MPL-2.0+ fail instead of passing as unknown licenses. So do the deprecated SPDX ids with a built-in exception, such as GPL-2.0-with-classpath-exception and GPL-3.0-with-GCC-exception.
  • WITH exceptions are checked separately from licenses. A new allowed_license_exceptions list in license_config.yaml (LLVM-exception, Swift-exception) keeps Apache-2.0 WITH LLVM-exception allowed. An unlisted exception, such as Apache-2.0 WITH Commons-Clause, goes to review. A blocked base license stays blocked whatever exception follows it.
  • Frontmatter/LICENSE reconciliation compares the LICENSE file ids with the licenses named in the frontmatter expression. MIT OR Apache-2.0 next to an Apache LICENSE file is consistent. An allowed expression that leaves out the allowed file license, such as MIT AND CC-BY-4.0 next to an Apache LICENSE file, is a frontmatter_license_mismatch conflict. This matches what a single identifier already got.
  • SPDX header extraction cuts the value at a closing backtick or | 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 bare MIT | GPL-3.0 header is left whole, so it is not shortened to MIT.
  • Tier 1 docs (license accordion) and a CHANGELOG entry under Unreleased / Fixed.

Why

Tier 1 runs the license check by default, and it is part of the publication evidence. Before this change:

Declared license Before After
(MIT OR Apache-2.0 allowed HIGH unsupported_license_expression
mit or (apache-2.0 (lower-case operator, unbalanced parenthesis) allowed unknown (review)
(MIT) unknown (review) allowed
MIT AND, OR MIT, MIT OR OR Apache-2.0, MIT WITH unknown (warning only) HIGH unsupported_license_expression
GPL-3.0+, AGPL-3.0+, GPL-2.0+, MIT OR GPL-3.0+ unknown (warning only) blocked
LGPL-2.0-only, LGPL-2.1-only, LGPL-3.0-only, LGPL-2.1+, LGPL-3.0-or-later unknown (warning only) blocked
GPL-2.0-with-classpath-exception, GPL-3.0-with-GCC-exception unknown (warning only) blocked
GPL-3.0 (or later), (gpl-3.0 or mit, mit or gpl-3.0), (GPL-3.0 OR MIT blocked blocked (unchanged)
Apache-2.0 WITH LLVM-exception unknown (review) allowed
Apache-2.0 WITH Commons-Clause unknown, reported as Commons-Clause unknown, reported as Apache-2.0 WITH Commons-Clause
GPL-3.0+ frontmatter next to an Apache LICENSE file unknown (warning only) blocked
`SPDX-License-Identifier: MIT OR GPL-3.0` in a Markdown table cell unknown (GPL-3.0 with the closing backtick and pipe attached) blocked
MIT License, The MIT License, CC-BY-4.0 AND Apache-2.0, mit or apache-2.0 allowed allowed (unchanged)

In the default non-strict mode, an unknown license is only a warning. So copyleft licenses written in their + or -only form 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/OR expressions.

Kept from public

  • No new base dependency. An alternative was the license-expression package. Its strict SPDX parser rejects free-text names that pass today: MIT License and The MIT License become 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, -license suffix, case, spaces, underscores) for anything that is not an SPDX expression. The +/-only/-or-later rule gives the blocking a symbol table would give for the bundled blocklist, and also blocks LGPL-2.1+.
  • Line-based SPDX header capture. SPDX_LICENSE_PATTERN still captures the whole line (([^\n]+)) and _strip_spdx_capture trims it. Headers followed by # comment or ; are not dropped. A stricter lookahead pattern would drop them.
  • Fail-closed frontmatter/LICENSE reconciliation ([BUG]: Frontmatter license: MIT wins even when LICENSE is GPL-3.0 #85, fix: do not let frontmatter MIT hide a GPL LICENSE file #97). Blocked LICENSE files still win. Unrecognized LICENSE/COPYING files still fail closed. Allowed-vs-allowed disagreements are still conflicts. Expressions now go through the same path instead of skipping the mismatch check.
  • Fail-closed compound handling ([BUG]: SPDX-License-Identifier "MIT OR GPL-3.0" is truncated to MIT and allowed #86). Any blocked license in an AND/OR expression blocks it, including one written after WITH and one in a value that does not parse, such as GPL-3.0 (or later) or mit 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 as mit or apache-2.0 are still accepted.
  • Bundled policy lists. The allow and block lists are unchanged. Only the exception allowlist is new.

Tests

  • tests/validators/test_license.py adds 56 cases:
    • Valid expressions stay allowed in strict mode: parentheses, mixed AND/OR, (MIT), Apache-2.0 WITH LLVM-exception.
    • Version forms of blocked licenses are blocked: deprecated bare ids, +, -only, -or-later, WITH on a blocked base, and deprecated -with-*-exception ids.
    • GPL-3.0+ next to an Apache LICENSE file is blocked.
    • Unlisted WITH exceptions go to review, and strict mode fails them.
    • Malformed expressions are rejected, with exact error messages.
    • A blocked license in a value that does not parse is blocked, and the finding names it: 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.
    • Values without an upper-case operator (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.
    • An expression that includes the LICENSE file's license is consistent with it; one that leaves it out is a conflict.
    • SPDX header extraction: Markdown table cell, inline code, # comment, trailing ;, WITH inside a block comment, and a bare | left whole.
    • A blocked license in a Markdown table header is blocked, and a malformed SPDX header is unsupported.
  • 32 of the new cases fail on unmodified main. The rest guard behavior that must not change, including the five lower-case malformed values with a blocked license, which main already blocks.
  • pytest tests/validators tests/test_publication_evidence.py tests/test_progress_output.py on current main: 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_*.py also passes.
  • Full suite before the rebase (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 unmodified main too: the 2 test_sidecar_retains_* tests in test_harbor_secure_docker_environment.py depend on host environment variables; 13 tests in tests/tier3/test_judge_failure_artifacts.py run a generated grader with the host python3, which is 3.9 here and rejects int | float; and test_streamed_timeout_callback_matches_result_and_contains_descendants is a timing test that failed 2 of 10 runs on main.
  • ruff check passes on the changed Python files. ruff format --check reports only formatting that main already has in license.py, outside the changed lines. CI runs ruff check only.
  • scripts/check_oss_boundary.py passes.

🤖 Generated with Claude Code

rng1995 and others added 2 commits October 30, 2026 12:00
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 rng1995 left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@chrisknvidia chrisknvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rng1995
rng1995 merged commit c61fa97 into main Nov 4, 2026
20 checks passed
@rng1995
rng1995 deleted the naren/port-spdx-compound branch November 4, 2026 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants