spdx / Spdx-Java-Library

Java library which implements the Java object model for SPDX and provides useful helper functions
Apache License 2.0
32 stars 33 forks source link

Official MIT license text is not recognized as MIT by LicenseCompareHelper.isStandardLicenseWithinText #241

Open sdheh opened 3 weeks ago

sdheh commented 3 weeks ago

TLDR: LicenseCompareHelper.matchingStandardLicenseIdsWithinText does not match the MIT license text from https://spdx.org/licenses/MIT.html but LicenseCompareHelper.isTextStandardLicense does. Version: 1.1.11 and allowing the fetching of recent license templates from the internet.

Details (steps to reproduce): I copied the following text from https://spdx.org/licenses/MIT.html and used assigned it to the String licenseText:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

I ran the following code:

SpdxListedLicense mitLicense = ListedLicenses.getListedLicenses().getListedLicenseById("MIT");
System.out.println(LicenseCompareHelper.isTextStandardLicense(mitLicense, licenseText).isDifferenceFound());
System.out.println(LicenseCompareHelper.isStandardLicenseWithinText(licenseText, mitLicense));

This produces the following output:

false
false

but the output should be

false
true

The problem is in TemplateRegexMatcher.findTemplateWithinText, where the startPattern is

(?im)(\Qmit\E\s*\Qlicense\E\s*)?(.{0,5000})\Qpermission\E\s*\Qis\E\s*\Qhereby\E\s*\Qgranted\E\s*\Q,\E\s*\Qfree\E\s*\Qof\E\s*\Qcharge\E\s*\Q,\E\s*\Qto\E\s*\Qany\E\s*\Qperson\E\s*\Qobtaining\E\s*\Qa\E\s*\Qcopy\E\s*\Qof\E\s*(this\s+software\s+and\s+associated\s+documentation\s+files|this\s+source\s+file)\Q(\E\s*\Qthe\E\s*\Q'\E\s*\Qsoftware\E\s*\Q'\E\s*\Q)\E\s*\Q,\E\s*\Qto\E\s*\Qdeal\E\s*\Qin\E\s*

The problem is the following part:

file)\Q(\E

There is no \s* after file) so that is why this regex does not match the text.

goneall commented 3 weeks ago

Thanks @sdheh for the detailed analysis! This could explain some of the other issues.

It may be a little while before I can work on a fix myself since I'm heads-down working on the SPDX 3 upgrade. In the mean time pull requests would be welcome.

pmonks commented 3 weeks ago

I'm pretty sure this is a duplicate of #234.

sdheh commented 3 weeks ago

By the way, the regex match will always include the first 5000 characters before the word "permission" because the beginning is optional and .{0,5000} is greedy. Is this intended?

goneall commented 1 week ago

By the way, the regex match will always include the first 5000 characters before the word "permission" because the beginning is optional and .{0,5000} is greedy. Is this intended?

It may be more efficient to make this match less greedy, but it shouldn't cause a problem since the follow-on calls to the token license matching algorithm will skip the "optional" text.