license_detection.rs

  1use regex::Regex;
  2
  3/// The most common license locations, with US and UK English spelling.
  4pub const LICENSE_FILES_TO_CHECK: &[&str] = &["LICENSE", "LICENCE", "LICENSE.txt", "LICENCE.txt"];
  5
  6pub fn is_license_eligible_for_data_collection(license: &str) -> bool {
  7    // TODO: Include more licenses later (namely, Apache)
  8    for pattern in [MIT_LICENSE_REGEX, ISC_LICENSE_REGEX, UPL_LICENSE_REGEX] {
  9        let regex = Regex::new(pattern.trim()).unwrap();
 10        if regex.is_match(license.trim()) {
 11            return true;
 12        }
 13    }
 14    false
 15}
 16
 17const MIT_LICENSE_REGEX: &str = r#"
 18^.*MIT License.*
 19
 20Copyright.*?
 21
 22Permission is hereby granted, free of charge, to any person obtaining a copy
 23of this software and associated documentation files \(the "Software"\), to deal
 24in the Software without restriction, including without limitation the rights
 25to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 26copies of the Software, and to permit persons to whom the Software is
 27furnished to do so, subject to the following conditions:
 28
 29The above copyright notice and this permission notice shall be included in all
 30copies or substantial portions of the Software\.
 31
 32THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 33IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 34FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT\. IN NO EVENT SHALL THE
 35AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 36LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 37OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 38SOFTWARE\.$
 39"#;
 40
 41const ISC_LICENSE_REGEX: &str = r#"
 42^ISC License
 43
 44Copyright.*?
 45
 46Permission to use, copy, modify, and/or distribute this software for any
 47purpose with or without fee is hereby granted, provided that the above
 48copyright notice and this permission notice appear in all copies\.
 49
 50THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 51WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 52MERCHANTABILITY AND FITNESS\. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 53ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 54WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 55ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 56OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE\.$
 57"#;
 58
 59const UPL_LICENSE_REGEX: &str = r#"
 60Copyright.*?
 61
 62The Universal Permissive License.*?
 63
 64Subject to the condition set forth below, permission is hereby granted to any person
 65obtaining a copy of this software, associated documentation and/or data \(collectively
 66the "Software"\), free of charge and under any and all copyright rights in the
 67Software, and any and all patent rights owned or freely licensable by each licensor
 68hereunder covering either \(i\) the unmodified Software as contributed to or provided
 69by such licensor, or \(ii\) the Larger Works \(as defined below\), to deal in both
 70
 71\(a\) the Software, and
 72
 73\(b\) any piece of software and/or hardware listed in the lrgrwrks\.txt file if one is
 74    included with the Software \(each a "Larger Work" to which the Software is
 75    contributed by such licensors\),
 76
 77without restriction, including without limitation the rights to copy, create
 78derivative works of, display, perform, and distribute the Software and make, use,
 79sell, offer for sale, import, export, have made, and have sold the Software and the
 80Larger Work\(s\), and to sublicense the foregoing rights on either these or other
 81terms\.
 82
 83This license is subject to the following condition:
 84
 85The above copyright notice and either this complete permission notice or at a minimum
 86a reference to the UPL must be included in all copies or substantial portions of the
 87Software\.
 88
 89THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 90INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 91PARTICULAR PURPOSE AND NONINFRINGEMENT\. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 92HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 93CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 94OR THE USE OR OTHER DEALINGS IN THE SOFTWARE\.$
 95"#;
 96
 97#[cfg(test)]
 98mod tests {
 99    use unindent::unindent;
100
101    use crate::is_license_eligible_for_data_collection;
102
103    #[test]
104    fn test_mit_positive_detection() {
105        let example_license = unindent(
106            r#"
107                MIT License
108
109                Copyright (c) 2024 John Doe
110
111                Permission is hereby granted, free of charge, to any person obtaining a copy
112                of this software and associated documentation files (the "Software"), to deal
113                in the Software without restriction, including without limitation the rights
114                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
115                copies of the Software, and to permit persons to whom the Software is
116                furnished to do so, subject to the following conditions:
117
118                The above copyright notice and this permission notice shall be included in all
119                copies or substantial portions of the Software.
120
121                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
122                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
123                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
124                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
125                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
126                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
127                SOFTWARE.
128            "#
129            .trim(),
130        );
131
132        assert!(is_license_eligible_for_data_collection(&example_license));
133
134        let example_license = unindent(
135            r#"
136                The MIT License (MIT)
137
138                Copyright (c) 2019 John Doe
139
140                Permission is hereby granted, free of charge, to any person obtaining a copy
141                of this software and associated documentation files (the "Software"), to deal
142                in the Software without restriction, including without limitation the rights
143                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
144                copies of the Software, and to permit persons to whom the Software is
145                furnished to do so, subject to the following conditions:
146
147                The above copyright notice and this permission notice shall be included in all
148                copies or substantial portions of the Software.
149
150                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
152                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
153                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
154                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
155                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
156                SOFTWARE.
157            "#
158            .trim(),
159        );
160
161        assert!(is_license_eligible_for_data_collection(&example_license));
162    }
163
164    #[test]
165    fn test_mit_negative_detection() {
166        let example_license = unindent(
167            r#"
168                MIT License
169
170                Copyright (c) 2024 John Doe
171
172                Permission is hereby granted, free of charge, to any person obtaining a copy
173                of this software and associated documentation files (the "Software"), to deal
174                in the Software without restriction, including without limitation the rights
175                to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
176                copies of the Software, and to permit persons to whom the Software is
177                furnished to do so, subject to the following conditions:
178
179                The above copyright notice and this permission notice shall be included in all
180                copies or substantial portions of the Software.
181
182                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
183                IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
184                FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
185                AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
186                LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
187                OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
188                SOFTWARE.
189
190                This project is dual licensed under the MIT License and the Apache License, Version 2.0.
191            "#
192            .trim(),
193        );
194
195        assert!(!is_license_eligible_for_data_collection(&example_license));
196    }
197
198    #[test]
199    fn test_isc_positive_detection() {
200        let example_license = unindent(
201            r#"
202                ISC License
203
204                Copyright (c) 2024, John Doe
205
206                Permission to use, copy, modify, and/or distribute this software for any
207                purpose with or without fee is hereby granted, provided that the above
208                copyright notice and this permission notice appear in all copies.
209
210                THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
211                WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
212                MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
213                ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
214                WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
215                ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
216                OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
217            "#
218            .trim(),
219        );
220
221        assert!(is_license_eligible_for_data_collection(&example_license));
222    }
223
224    #[test]
225    fn test_isc_negative_detection() {
226        let example_license = unindent(
227            r#"
228                ISC License
229
230                Copyright (c) 2024, John Doe
231
232                Permission to use, copy, modify, and/or distribute this software for any
233                purpose with or without fee is hereby granted, provided that the above
234                copyright notice and this permission notice appear in all copies.
235
236                THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
237                WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
238                MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
239                ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
240                WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
241                ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
242                OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
243
244                This project is dual licensed under the ISC License and the MIT License.
245            "#
246            .trim(),
247        );
248
249        assert!(!is_license_eligible_for_data_collection(&example_license));
250    }
251
252    #[test]
253    fn test_upl_positive_detection() {
254        let example_license = unindent(
255            r#"
256                Copyright (c) 2025, John Doe
257
258                The Universal Permissive License (UPL), Version 1.0
259
260                Subject to the condition set forth below, permission is hereby granted to any person
261                obtaining a copy of this software, associated documentation and/or data (collectively
262                the "Software"), free of charge and under any and all copyright rights in the
263                Software, and any and all patent rights owned or freely licensable by each licensor
264                hereunder covering either (i) the unmodified Software as contributed to or provided
265                by such licensor, or (ii) the Larger Works (as defined below), to deal in both
266
267                (a) the Software, and
268
269                (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if one is
270                    included with the Software (each a "Larger Work" to which the Software is
271                    contributed by such licensors),
272
273                without restriction, including without limitation the rights to copy, create
274                derivative works of, display, perform, and distribute the Software and make, use,
275                sell, offer for sale, import, export, have made, and have sold the Software and the
276                Larger Work(s), and to sublicense the foregoing rights on either these or other
277                terms.
278
279                This license is subject to the following condition:
280
281                The above copyright notice and either this complete permission notice or at a minimum
282                a reference to the UPL must be included in all copies or substantial portions of the
283                Software.
284
285                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
286                INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
287                PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
288                HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
289                CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
290                OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
291            "#
292            .trim(),
293        );
294
295        assert!(is_license_eligible_for_data_collection(&example_license));
296    }
297
298    #[test]
299    fn test_upl_negative_detection() {
300        let example_license = unindent(
301            r#"
302                UPL License
303
304                Copyright (c) 2024, John Doe
305
306                The Universal Permissive License (UPL), Version 1.0
307
308                Subject to the condition set forth below, permission is hereby granted to any person
309                obtaining a copy of this software, associated documentation and/or data (collectively
310                the "Software"), free of charge and under any and all copyright rights in the
311                Software, and any and all patent rights owned or freely licensable by each licensor
312                hereunder covering either (i) the unmodified Software as contributed to or provided
313                by such licensor, or (ii) the Larger Works (as defined below), to deal in both
314
315                (a) the Software, and
316
317                (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if one is
318                    included with the Software (each a "Larger Work" to which the Software is
319                    contributed by such licensors),
320
321                without restriction, including without limitation the rights to copy, create
322                derivative works of, display, perform, and distribute the Software and make, use,
323                sell, offer for sale, import, export, have made, and have sold the Software and the
324                Larger Work(s), and to sublicense the foregoing rights on either these or other
325                terms.
326
327                This license is subject to the following condition:
328
329                The above copyright notice and either this complete permission notice or at a minimum
330                a reference to the UPL must be included in all copies or substantial portions of the
331                Software.
332
333                THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
334                INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
335                PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
336                HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
337                CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
338                OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
339
340                This project is dual licensed under the ISC License and the MIT License.
341            "#
342            .trim(),
343        );
344
345        assert!(!is_license_eligible_for_data_collection(&example_license));
346    }
347}