-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support evaluating spdx license expressions (#1329)
This implements a parser for SPDX license expressions in accordance with [annex D of the v2 spec](https://spdx.github.io/spdx-spec/v2-draft/SPDX-license-expressions/), to allow the scanner to properly determine if packages with such expressive licenses are permitted based on the licenses allowed by the user. To do this I've implemented a two-phase parser which starts by tokenizing the license string and then turns it into an AST of nodes that can be walked to determine if the full expression is satisfied; for no particularly good reason I have used a string for the base token type rather than a `struct`, meaning the tokens value is also it's type - the tradeoff with this is while it means we don't have to do as much referencing or work in the tokenizer, we do have to do some extra work when walking the tree to resolve the "simple expression" tokens. I'm proposing landing the current implementation as I don't think using a `struct` would be strictly better, though in hindsight it probably would have been a bit quicker to implement and so I plan to explore how much simpler (or complex) it might be as a follow up. Currently this is focused on `AND` and `OR` support, as I believe those are the two primary operators that are relevant to the scanner, though we still might want to have richer handling for the `WITH` and `+` operators; currently both of those just get treated as being part of the license expression (though it's not actually possible right now to allow a license with `WITH` as the CLI expects license values to not have any spaces - this too will be a follow-up for me). Finally, I've purposely not put any caching in place even though that should be easy, due to wanting to get this landed and as I don't expect it to actually have a significant impact on the scanner performance (ultimately most complex expressions in the real-world will be made up of a single operator, and chopping+looping over strings in memory is extremely fast) Resolves #1299
- Loading branch information
Showing
9 changed files
with
817 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
cmd/osv-scanner/fixtures/osv-scanner-expressive-licenses-config.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[[PackageOverrides]] | ||
name = "babel" | ||
license.override = ["MIT AND (LGPL-2.1-or-later OR BSD-3-Clause)"] | ||
|
||
[[PackageOverrides]] | ||
name = "human-signals" | ||
license.override = ["LGPL-2.1-only OR MIT OR BSD-3-Clause"] | ||
|
||
[[PackageOverrides]] | ||
name = "ms" | ||
license.override = ["MIT WITH Bison-exception-2.2"] |
11 changes: 11 additions & 0 deletions
11
cmd/osv-scanner/fixtures/osv-scanner-invalid-licenses-config.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[[PackageOverrides]] | ||
name = "babel" | ||
license.override = ["MIT AND (LGPL-2.1-or-later OR BSD-3-Clause))"] | ||
|
||
[[PackageOverrides]] | ||
name = "human-signals" | ||
license.override = ["LGPL-2.1-only OR OR BSD-3-Clause"] | ||
|
||
[[PackageOverrides]] | ||
name = "ms" | ||
license.override = ["MIT WITH (Bison-exception-2.2 AND somethingelse)"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.