Skip to content

Conversation

@bwoodsend
Copy link
Contributor

The following condensed list of expressions are all currently mislabelled as valid by canonicalize_license_expression().

( ) or MIT
MIT and ( )
( ) and MIT or MIT
( ) with ( MIT )
( ) with ( ) or MIT
MIT with ( ) with ( ) or MIT

The trick of converting license expressions to Python statements runs afoul of () being an empty tuple (i.e. valid syntax and falsy). Depending on how () is combined with real Falses, it may or may not get caught by the subsequent eval(python_expression) is False check:

MIT or ()  ->  False or ()  ->  ()     # Caught
() or MIT  ->  () or False  ->  False  # Not caught

Consider a license expression invalid if a ) comes immediately after a ( token. This also removes the one case where the eval() would return something other than False so that eval() can now be downgraded to a compile() (which I find very anxiety relieving even though I can't think of any way the old eval() could ever have been exposed to more than Falses or ()s).

@henryiii
Copy link
Contributor

CC @ofek for hatchling's version.

Copy link
Contributor

@ofek ofek left a comment

Choose a reason for hiding this comment

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

Seems like a great improvement!

The following condensed list of expressions are all currently
mislabelled as valid by `canonicalize_license_expression()`.

    ( ) or MIT
    MIT and ( )
    ( ) and MIT or MIT
    ( ) with ( MIT )
    ( ) with ( ) or MIT
    MIT with ( ) with ( ) or MIT

The trick of converting license expressions to Python statements runs
afoul of `()` being an empty tuple (i.e. valid syntax and falsy).
Depending on how `()` is combined with real `False`s, it may or may not
get caught by the subsequent `eval(python_expression) is False` check:

    MIT or ()  ->  False or ()  ->  ()     # Caught
    () or MIT  ->  () or False  ->  False  # Not caught

Consider a license expression invalid if a `)` comes immediately after a
`(` token. This also removes the one case where the `eval()` would
return something other than `False` so that `eval()` can now be
downgraded to a `compile()` (which I find very anxiety relieving even
though I can't think of any way the old `eval()` could ever have been
exposed to more than `False`s or `()`s).
@henryiii henryiii merged commit 2680259 into pypa:main May 22, 2025
70 of 71 checks passed
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.

3 participants