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