1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 Cyril Roelandt <tipecaml@gmail.com>
3 ;;; Copyright © 2014, 2015 Eric Bavier <bavier@member.fsf.org>
4 ;;; Copyright © 2013, 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
5 ;;; Copyright © 2015, 2016 Mathieu Lirzin <mthl@gnu.org>
6 ;;; Copyright © 2016 Danny Milosavljevic <dannym+a@scratchpost.org>
7 ;;; Copyright © 2016 Hartmut Goebel <h.goebel@crazy-compilers.com>
8 ;;; Copyright © 2017 Alex Kost <alezost@gmail.com>
9 ;;; Copyright © 2017 Tobias Geerinckx-Rice <me@tobias.gr>
10 ;;; Copyright © 2017, 2018 Efraim Flashner <efraim@flashner.co.il>
11 ;;; Copyright © 2018, 2019 Arun Isaac <arunisaac@systemreboot.net>
13 ;;; This file is part of GNU Guix.
15 ;;; GNU Guix is free software; you can redistribute it and/or modify it
16 ;;; under the terms of the GNU General Public License as published by
17 ;;; the Free Software Foundation; either version 3 of the License, or (at
18 ;;; your option) any later version.
20 ;;; GNU Guix is distributed in the hope that it will be useful, but
21 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;;; GNU General Public License for more details.
25 ;;; You should have received a copy of the GNU General Public License
26 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
28 (define-module (guix lint)
29 #:use-module ((guix store) #:hide (close-connection))
30 #:use-module (guix base32)
31 #:use-module (guix diagnostics)
32 #:use-module (guix download)
33 #:use-module (guix ftp-client)
34 #:use-module (guix http-client)
35 #:use-module (guix packages)
36 #:use-module (guix i18n)
37 #:use-module (guix licenses)
38 #:use-module (guix records)
39 #:use-module (guix grafts)
40 #:use-module (guix upstream)
41 #:use-module (guix utils)
42 #:use-module (guix memoization)
43 #:use-module (guix scripts)
44 #:use-module ((guix ui) #:select (texi->plain-text fill-paragraph))
45 #:use-module (guix gnu-maintenance)
46 #:use-module (guix cve)
47 #:use-module ((guix swh) #:hide (origin?))
48 #:autoload (guix git-download) (git-reference?)
49 #:use-module (ice-9 match)
50 #:use-module (ice-9 regex)
51 #:use-module (ice-9 format)
52 #:use-module (web client)
53 #:use-module (web uri)
54 #:use-module ((guix build download)
55 #:select (maybe-expand-mirrors
56 (open-connection-for-uri
57 . guix:open-connection-for-uri)
59 #:use-module (web request)
60 #:use-module (web response)
61 #:use-module (srfi srfi-1)
62 #:use-module (srfi srfi-6) ;Unicode string ports
63 #:use-module (srfi srfi-9)
64 #:use-module (srfi srfi-11)
65 #:use-module (srfi srfi-26)
66 #:use-module (srfi srfi-34)
67 #:use-module (srfi srfi-35)
68 #:use-module (ice-9 rdelim)
69 #:export (check-description-style
70 check-inputs-should-be-native
71 check-inputs-should-not-be-an-input-at-all
72 check-patch-file-names
77 check-source-file-name
78 check-source-unstable-tarball
91 lint-warning-message-text
92 lint-warning-message-data
96 %network-dependent-checkers
102 lint-checker-description
110 (define-record-type* <lint-warning>
111 lint-warning make-lint-warning
113 (package lint-warning-package)
114 (message-text lint-warning-message-text)
115 (message-data lint-warning-message-data
117 (location lint-warning-location
120 (define (lint-warning-message warning)
122 (G_ (lint-warning-message-text warning))
123 (lint-warning-message-data warning)))
125 (define (package-file package)
127 (package-location package)))
129 (define* (%make-warning package message-text
130 #:optional (message-data '())
131 #:key field location)
137 (package-field-location package field)
138 (package-location package))))
140 (define-syntax make-warning
142 ((_ package (G_ message) rest ...)
143 (%make-warning package message rest ...))))
150 (define-record-type* <lint-checker>
151 lint-checker make-lint-checker
153 ;; TODO: add a 'certainty' field that shows how confident we are in the
154 ;; checker. Then allow users to only run checkers that have a certain
155 ;; 'certainty' level.
156 (name lint-checker-name)
157 (description lint-checker-description)
158 (check lint-checker-check))
160 (define (properly-starts-sentence? s)
161 (string-match "^[(\"'`[:upper:][:digit:]]" s))
163 (define (starts-with-abbreviation? s)
164 "Return #t if S starts with what looks like an abbreviation or acronym."
165 (string-match "^[A-Z][A-Z0-9]+\\>" s))
167 (define %quoted-identifier-rx
168 ;; A quoted identifier, like 'this'.
169 (make-regexp "['`][[:graph:]]+'"))
171 (define (check-description-style package)
172 ;; Emit a warning if stylistic issues are found in the description of PACKAGE.
173 (define (check-not-empty description)
174 (if (string-null? description)
176 (make-warning package
177 (G_ "description should not be empty")
178 #:field 'description))
181 (define (check-texinfo-markup description)
182 "Check that DESCRIPTION can be parsed as a Texinfo fragment. If the
183 markup is valid return a plain-text version of DESCRIPTION, otherwise #f."
185 (lambda () (texi->plain-text description))
186 (lambda (keys . args)
187 (make-warning package
188 (G_ "Texinfo markup in description is invalid")
189 #:field 'description))))
191 (define (check-trademarks description)
192 "Check that DESCRIPTION does not contain '™' or '®' characters. See
193 http://www.gnu.org/prep/standards/html_node/Trademarks.html."
194 (match (string-index description (char-set #\™ #\®))
195 ((and (? number?) index)
197 (make-warning package
198 (G_ "description should not contain ~
199 trademark sign '~a' at ~d")
200 (list (string-ref description index) index)
201 #:field 'description)))
204 (define (check-quotes description)
205 "Check whether DESCRIPTION contains single quotes and suggest @code."
206 (if (regexp-exec %quoted-identifier-rx description)
208 (make-warning package
209 ;; TRANSLATORS: '@code' is Texinfo markup and must be kept
211 (G_ "use @code or similar ornament instead of quotes")
212 #:field 'description))
215 (define (check-proper-start description)
216 (if (or (string-null? description)
217 (properly-starts-sentence? description)
218 (string-prefix-ci? (package-name package) description))
223 (G_ "description should start with an upper-case letter or digit")
224 #:field 'description))))
226 (define (check-end-of-sentence-space description)
227 "Check that an end-of-sentence period is followed by two spaces."
229 (reverse (fold-matches
230 "\\. [A-Z]" description '()
232 ;; Filter out matches of common abbreviations.
233 (if (find (lambda (s)
234 (string-suffix-ci? s (match:prefix m)))
235 '("i.e" "e.g" "a.k.a" "resp"))
236 r (cons (match:start m) r)))))))
237 (if (null? infractions)
240 (make-warning package
241 (G_ "sentences in description should be followed ~
242 by two spaces; possible infraction~p at ~{~a~^, ~}")
243 (list (length infractions)
245 #:field 'description)))))
247 (let ((description (package-description package)))
248 (if (string? description)
250 (check-not-empty description)
251 (check-quotes description)
252 (check-trademarks description)
253 ;; Use raw description for this because Texinfo rendering
254 ;; automatically fixes end of sentence space.
255 (check-end-of-sentence-space description)
256 (match (check-texinfo-markup description)
257 ((and warning (? lint-warning?)) (list warning))
259 (check-proper-start plain-description))))
261 (make-warning package
262 (G_ "invalid description: ~s")
264 #:field 'description)))))
266 (define (package-input-intersection inputs-to-check input-names)
267 "Return the intersection between INPUTS-TO-CHECK, the list of input tuples
268 of a package, and INPUT-NAMES, a list of package specifications such as
270 (match inputs-to-check
271 (((labels packages . outputs) ...)
272 (filter-map (lambda (package output)
273 (and (package? package)
274 (let ((input (string-append
275 (package-name package)
276 (if (> (length output) 0)
277 (string-append ":" (car output))
279 (and (member input input-names)
283 (define (check-inputs-should-be-native package)
284 ;; Emit a warning if some inputs of PACKAGE are likely to belong to its
286 (let ((inputs (package-inputs package))
290 "extra-cmake-modules"
296 "python-coverage" "python2-coverage"
297 "python-cython" "python2-cython"
298 "python-docutils" "python2-docutils"
299 "python-mock" "python2-mock"
300 "python-nose" "python2-nose"
301 "python-pbr" "python2-pbr"
302 "python-pytest" "python2-pytest"
303 "python-pytest-cov" "python2-pytest-cov"
304 "python-setuptools-scm" "python2-setuptools-scm"
305 "python-sphinx" "python2-sphinx")))
309 (G_ "'~a' should probably be a native input")
312 (package-input-intersection inputs input-names))))
314 (define (check-inputs-should-not-be-an-input-at-all package)
315 ;; Emit a warning if some inputs of PACKAGE are likely to should not be
317 (let ((input-names '("python-setuptools"
324 (G_ "'~a' should probably not be an input at all")
327 (package-input-intersection (package-direct-inputs package)
330 (define (package-name-regexp package)
331 "Return a regexp that matches PACKAGE's name as a word at the beginning of a
333 (make-regexp (string-append "^" (regexp-quote (package-name package))
337 (define (check-synopsis-style package)
338 ;; Emit a warning if stylistic issues are found in the synopsis of PACKAGE.
339 (define (check-final-period synopsis)
340 ;; Synopsis should not end with a period, except for some special cases.
341 (if (and (string-suffix? "." synopsis)
342 (not (string-suffix? "etc." synopsis)))
344 (make-warning package
345 (G_ "no period allowed at the end of the synopsis")
349 (define check-start-article
350 ;; Skip this check for GNU packages, as suggested by Karl Berry's reply to
351 ;; <http://lists.gnu.org/archive/html/bug-womb/2014-11/msg00000.html>.
352 (if (false-if-exception (gnu-package? package))
355 (if (or (string-prefix-ci? "A " synopsis)
356 (string-prefix-ci? "An " synopsis))
358 (make-warning package
359 (G_ "no article allowed at the beginning of \
364 (define (check-synopsis-length synopsis)
365 (if (>= (string-length synopsis) 80)
367 (make-warning package
368 (G_ "synopsis should be less than 80 characters long")
372 (define (check-proper-start synopsis)
373 (if (properly-starts-sentence? synopsis)
376 (make-warning package
377 (G_ "synopsis should start with an upper-case letter or digit")
378 #:field 'synopsis))))
380 (define (check-start-with-package-name synopsis)
381 (if (and (regexp-exec (package-name-regexp package) synopsis)
382 (not (starts-with-abbreviation? synopsis)))
384 (make-warning package
385 (G_ "synopsis should not start with the package name")
389 (define (check-texinfo-markup synopsis)
390 "Check that SYNOPSIS can be parsed as a Texinfo fragment. If the
391 markup is valid return a plain-text version of SYNOPSIS, otherwise #f."
394 (texi->plain-text synopsis)
396 (lambda (keys . args)
398 (make-warning package
399 (G_ "Texinfo markup in synopsis is invalid")
400 #:field 'synopsis)))))
403 (list check-proper-start
406 check-start-with-package-name
407 check-synopsis-length
408 check-texinfo-markup))
410 (match (package-synopsis package)
413 (make-warning package
414 (G_ "synopsis should not be empty")
416 ((? string? synopsis)
423 (make-warning package
424 (G_ "invalid synopsis: ~s")
426 #:field 'synopsis)))))
428 (define* (probe-uri uri #:key timeout)
429 "Probe URI, a URI object, and return two values: a symbol denoting the
430 probing status, such as 'http-response' when we managed to get an HTTP
431 response from URI, and additional details, such as the actual HTTP response.
433 TIMEOUT is the maximum number of seconds (possibly an inexact number) to wait
434 for connections to complete; when TIMEOUT is #f, wait as long as needed."
436 '((User-Agent . "GNU Guile")
441 (match (uri-scheme uri)
445 (let ((port (guix:open-connection-for-uri
446 uri #:timeout timeout))
447 (request (build-request uri #:headers headers)))
452 (write-request request port)
454 (read-response port))
456 (close-connection port))))
458 (case (response-code response)
459 ((302 ; found (redirection)
461 307 ; temporary redirection
462 308) ; permanent redirection
463 (let ((location (response-location response)))
464 (if (or (not location) (member location visited))
465 (values 'http-response response)
466 (loop location (cons location visited))))) ;follow the redirect
467 ((301) ; moved permanently
468 (let ((location (response-location response)))
469 ;; Return RESPONSE, unless the final response as we follow
470 ;; redirects is not 200.
472 (let-values (((status response2)
473 (loop location (cons location visited))))
476 (values 'http-response
477 (if (= 200 (response-code response2))
481 (values status response2))))
482 (values 'http-response response)))) ;invalid redirect
484 (values 'http-response response)))))
487 ((bad-header bad-header-component)
488 ;; This can happen if the server returns an invalid HTTP header,
489 ;; as is the case with the 'Date' header at sqlite.org.
490 (values 'invalid-http-response #f))
491 ((getaddrinfo-error system-error
492 gnutls-error tls-certificate-error)
495 (apply throw key args))))))
499 (let ((conn (ftp-open (uri-host uri) #:timeout timeout)))
504 (ftp-chdir conn (dirname (uri-path uri)))
505 (ftp-size conn (basename (uri-path uri))))
508 (values 'ftp-response '(ok))))
512 (values 'ftp-response `(error ,@args)))
513 ((getaddrinfo-error system-error gnutls-error)
516 (apply throw key args))))))
518 (values 'unknown-protocol #f)))))
520 (define (tls-certificate-error-string args)
521 "Return a string explaining the 'tls-certificate-error' arguments ARGS."
522 (call-with-output-string
524 (print-exception port #f
525 'tls-certificate-error args))))
527 (define (validate-uri uri package field)
528 "Return #t if the given URI can be reached, otherwise return a warning for
529 PACKAGE mentioning the FIELD."
530 (let-values (((status argument)
531 (probe-uri uri #:timeout 3))) ;wait at most 3 seconds
534 (cond ((= 200 (response-code argument))
535 (match (response-content-length argument)
537 ;; As of July 2016, SourceForge returns 200 (instead of 404)
538 ;; with a small HTML page upon failure. Attempt to detect
539 ;; such malicious behavior.
541 (make-warning package
542 (G_ "URI ~a returned \
543 suspiciously small file (~a bytes)")
544 (list (uri->string uri)
548 ((= 301 (response-code argument))
549 (if (response-location argument)
550 (make-warning package
551 (G_ "permanent redirect from ~a to ~a")
552 (list (uri->string uri)
554 (response-location argument)))
556 (make-warning package
557 (G_ "invalid permanent redirect \
559 (list (uri->string uri))
562 (make-warning package
563 (G_ "URI ~a not reachable: ~a (~s)")
564 (list (uri->string uri)
565 (response-code argument)
566 (response-reason-phrase argument))
571 (('error port command code message)
572 (make-warning package
573 (G_ "URI ~a not reachable: ~a (~s)")
574 (list (uri->string uri)
575 code (string-trim-both message))
578 (make-warning package
579 (G_ "URI ~a domain not found: ~a")
580 (list (uri->string uri)
581 (gai-strerror (car argument)))
584 (make-warning package
585 (G_ "URI ~a unreachable: ~a")
586 (list (uri->string uri)
589 (cons status argument))))
591 ((tls-certificate-error)
592 (make-warning package
593 (G_ "TLS certificate error: ~a")
594 (list (tls-certificate-error-string argument))
596 ((invalid-http-response gnutls-error)
597 ;; Probably a misbehaving server; ignore.
599 ((unknown-protocol) ;nothing we can do
602 (error "internal linter error" status)))))
604 (define (check-home-page package)
605 "Emit a warning if PACKAGE has an invalid 'home-page' field, or if that
606 'home-page' is not reachable."
607 (let ((uri (and=> (package-home-page package) string->uri)))
610 (match (validate-uri uri package 'home-page)
611 ((and (? lint-warning? warning) warning)
614 ((not (package-home-page package))
615 (if (or (string-contains (package-name package) "bootstrap")
616 (string=? (package-name package) "ld-wrapper"))
619 (make-warning package
620 (G_ "invalid value for home page")
621 #:field 'home-page))))
624 (make-warning package
625 (G_ "invalid home page URL: ~s")
626 (list (package-home-page package))
627 #:field 'home-page))))))
629 (define %distro-directory
631 (dirname (search-path %load-path "gnu.scm"))))
633 (define (check-patch-file-names package)
634 "Emit a warning if the patches requires by PACKAGE are badly named or if the
635 patch could not be found."
636 (guard (c ((message-condition? c) ;raised by 'search-patch'
638 ;; Use %make-warning, as condition-mesasge is already
640 (%make-warning package (condition-message c)
641 #:field 'patch-file-names))))
643 (or (and=> (package-source package) origin-patches)
647 (if (every (match-lambda ;patch starts with package name?
649 (and=> (string-contains (basename patch)
650 (package-name package))
652 (_ #f)) ;must be an <origin> or something like that.
658 (G_ "file names of patches should start with the package name")
659 #:field 'patch-file-names)))
661 ;; Check whether we're reaching tar's maximum file name length.
662 (let ((prefix (string-length (%distro-directory)))
663 (margin (string-length "guix-0.13.0-10-123456789/"))
665 (filter-map (match-lambda
667 (if (> (+ margin (if (string-prefix? (%distro-directory)
669 (- (string-length patch) prefix)
670 (string-length patch)))
674 (G_ "~a: file name is too long")
675 (list (basename patch))
676 #:field 'patch-file-names)
681 (define (escape-quotes str)
682 "Replace any quote character in STR by an escaped quote character."
684 (string-fold-right (lambda (chr result)
686 (#\" (cons* #\\ #\"result))
687 (_ (cons chr result))))
691 (define official-gnu-packages*
693 "A memoizing version of 'official-gnu-packages' that returns the empty
694 list when something goes wrong, such as a networking issue."
695 (let ((gnus (false-if-exception (official-gnu-packages))))
698 (define (check-gnu-synopsis+description package)
699 "Make sure that, if PACKAGE is a GNU package, it uses the synopsis and
700 descriptions maintained upstream."
701 (match (find (lambda (descriptor)
702 (string=? (gnu-package-name descriptor)
703 (package-name package)))
704 (official-gnu-packages*))
705 (#f ;not a GNU package, so nothing to do
707 (descriptor ;a genuine GNU package
709 (let ((upstream (gnu-package-doc-summary descriptor))
710 (downstream (package-synopsis package)))
712 (or (not (string? downstream))
713 (not (string=? upstream downstream))))
715 (make-warning package
716 (G_ "proposed synopsis: ~s~%")
721 (let ((upstream (gnu-package-doc-description descriptor))
722 (downstream (package-description package)))
724 (or (not (string? downstream))
725 (not (string=? (fill-paragraph upstream 100)
726 (fill-paragraph downstream 100)))))
730 (G_ "proposed description:~% \"~a\"~%")
731 (list (fill-paragraph (escape-quotes upstream) 77 7))
732 #:field 'description))
735 (define (origin-uris origin)
736 "Return the list of URIs (strings) for ORIGIN."
737 (match (origin-uri origin)
743 (define (check-source package)
744 "Emit a warning if PACKAGE has an invalid 'source' field, or if that
745 'source' is not reachable."
746 (define (warnings-for-uris uris)
747 (let loop ((uris uris)
753 (match (validate-uri uri package 'source)
755 ;; We found a working URL, so stop right away.
757 ((? lint-warning? warning)
758 (loop rest (cons warning warnings))))))))
760 (let ((origin (package-source package)))
762 (eqv? (origin-method origin) url-fetch))
763 (let* ((uris (append-map (cut maybe-expand-mirrors <> %mirrors)
764 (map string->uri (origin-uris origin))))
765 (warnings (warnings-for-uris uris)))
767 ;; Just make sure that at least one of the URIs is valid.
768 (if (= (length uris) (length warnings))
769 ;; When everything fails, report all of WARNINGS, otherwise don't
772 ;; XXX: Ideally we'd still allow warnings to be raised if *some*
773 ;; URIs are unreachable, but distinguish that from the error case
774 ;; where *all* the URIs are unreachable.
776 (make-warning package
777 (G_ "all the source URIs are unreachable:")
783 (define (check-source-file-name package)
784 "Emit a warning if PACKAGE's origin has no meaningful file name."
785 (define (origin-file-name-valid? origin)
786 ;; Return #f if the source file name contains only a version or is #f;
787 ;; indicates that the origin needs a 'file-name' field.
788 (let ((file-name (origin-actual-file-name origin))
789 (version (package-version package)))
791 ;; Common in many projects is for the filename to start
792 ;; with a "v" followed by the version,
793 ;; e.g. "v3.2.0.tar.gz".
794 (not (string-match (string-append "^v?" version) file-name)))))
796 (let ((origin (package-source package)))
797 (if (or (not origin) (origin-file-name-valid? origin))
800 (make-warning package
801 (G_ "the source file name should contain the package name")
804 (define (check-source-unstable-tarball package)
805 "Emit a warning if PACKAGE's source is an autogenerated tarball."
806 (define (check-source-uri uri)
807 (if (and (string=? (uri-host (string->uri uri)) "github.com")
808 (match (split-and-decode-uri-path
809 (uri-path (string->uri uri)))
810 ((_ _ "archive" _ ...) #t)
812 (make-warning package
813 (G_ "the source URI should not be an autogenerated tarball")
817 (let ((origin (package-source package)))
818 (if (and (origin? origin)
819 (eqv? (origin-method origin) url-fetch))
820 (filter-map check-source-uri
821 (origin-uris origin))
824 (define (check-mirror-url package)
825 "Check whether PACKAGE uses source URLs that should be 'mirror://'."
826 (define (check-mirror-uri uri) ;XXX: could be optimized
827 (let loop ((mirrors %mirrors))
831 (((mirror-id mirror-urls ...) rest ...)
832 (match (find (cut string-prefix? <> uri) mirror-urls)
836 (make-warning package
840 (string-drop uri (string-length prefix)))
841 #:field 'source)))))))
843 (let ((origin (package-source package)))
844 (if (and (origin? origin)
845 (eqv? (origin-method origin) url-fetch))
846 (let ((uris (origin-uris origin)))
847 (filter-map check-mirror-uri uris))
850 (define* (check-github-url package #:key (timeout 3))
851 "Check whether PACKAGE uses source URLs that redirect to GitHub."
852 (define (follow-redirect url)
853 (let* ((uri (string->uri url))
854 (port (guix:open-connection-for-uri uri #:timeout timeout))
855 (response (http-head uri #:port port)))
857 (case (response-code response)
859 (uri->string (assoc-ref (response-headers response) 'location)))
862 (define (follow-redirects-to-github uri)
864 ((string-prefix? "https://github.com/" uri) uri)
865 ((string-prefix? "http" uri)
866 (and=> (follow-redirect uri) follow-redirects-to-github))
867 ;; Do not attempt to follow redirects on URIs other than http and https
868 ;; (such as mirror, file)
871 (let ((origin (package-source package)))
872 (if (and (origin? origin)
873 (eqv? (origin-method origin) url-fetch))
876 (and=> (follow-redirects-to-github uri)
878 (if (string=? github-uri uri)
882 (G_ "URL should be '~a'")
885 (origin-uris origin))
888 (define (check-derivation package)
889 "Emit a warning if we fail to compile PACKAGE to a derivation."
893 (guard (c ((store-protocol-error? c)
894 (make-warning package
895 (G_ "failed to create ~a derivation: ~a")
897 (store-protocol-error-message c))))
898 ((message-condition? c)
899 (make-warning package
900 (G_ "failed to create ~a derivation: ~a")
902 (condition-message c)))))
904 ;; Disable grafts since it can entail rebuilds.
905 (parameterize ((%graft? #f))
906 (package-derivation store package system #:graft? #f)
908 ;; If there's a replacement, make sure we can compute its
910 (match (package-replacement package)
913 (package-derivation store replacement system
916 (make-warning package
917 (G_ "failed to create ~a derivation: ~s")
918 (list system args)))))
920 (filter lint-warning?
921 (map try (package-supported-systems package))))
923 (define (check-license package)
924 "Warn about type errors of the 'license' field of PACKAGE."
925 (match (package-license package)
931 (make-warning package (G_ "invalid license field")
932 #:field 'license)))))
934 (define (call-with-networking-fail-safe message error-value proc)
935 "Call PROC catching any network-related errors. Upon a networking error,
936 display a message including MESSAGE and return ERROR-VALUE."
937 (guard (c ((http-get-error? c)
938 (warning (G_ "~a: HTTP GET error for ~a: ~a (~s)~%")
940 (uri->string (http-get-error-uri c))
941 (http-get-error-code c)
942 (http-get-error-reason c))
947 (('getaddrinfo-error errcode)
948 (warning (G_ "~a: host lookup failure: ~a~%")
950 (gai-strerror errcode))
952 (('tls-certificate-error args ...)
953 (warning (G_ "~a: TLS certificate error: ~a")
955 (tls-certificate-error-string args))
957 ((and ('system-error _ ...) args)
958 (let ((errno (system-error-errno args)))
959 (if (member errno (list ECONNRESET ECONNABORTED ECONNREFUSED))
960 (let ((details (call-with-output-string
962 (print-exception port #f (car args)
964 (warning (G_ "~a: ~a~%") message details)
966 (apply throw args))))
968 (apply throw args))))))
970 (define-syntax-rule (with-networking-fail-safe message error-value exp ...)
971 (call-with-networking-fail-safe message error-value
972 (lambda () exp ...)))
974 (define (current-vulnerabilities*)
975 "Like 'current-vulnerabilities', but return the empty list upon networking
976 or HTTP errors. This allows network-less operation and makes problems with
977 the NIST server non-fatal."
978 (with-networking-fail-safe (G_ "while retrieving CVE vulnerabilities")
980 (current-vulnerabilities)))
982 (define package-vulnerabilities
983 (let ((lookup (delay (vulnerabilities->lookup-proc
984 (current-vulnerabilities*)))))
986 "Return a list of vulnerabilities affecting PACKAGE."
987 ;; First we retrieve the Common Platform Enumeration (CPE) name and
988 ;; version for PACKAGE, then we can pass them to LOOKUP.
989 (let ((name (or (assoc-ref (package-properties package)
991 (package-name package)))
992 (version (or (assoc-ref (package-properties package)
994 (package-version package))))
995 ((force lookup) name version)))))
997 (define (check-vulnerabilities package)
998 "Check for known vulnerabilities for PACKAGE."
999 (let ((package (or (package-replacement package) package)))
1000 (match (package-vulnerabilities package)
1003 ((vulnerabilities ...)
1004 (let* ((patched (package-patched-vulnerabilities package))
1005 (known-safe (or (assq-ref (package-properties package)
1008 (unpatched (remove (lambda (vuln)
1009 (let ((id (vulnerability-id vuln)))
1010 (or (member id patched)
1011 (member id known-safe))))
1013 (if (null? unpatched)
1018 (G_ "probably vulnerable to ~a")
1019 (list (string-join (map vulnerability-id unpatched)
1022 (define (check-for-updates package)
1023 "Check if there is an update available for PACKAGE."
1024 (match (with-networking-fail-safe
1025 (format #f (G_ "while retrieving upstream info for '~a'")
1026 (package-name package))
1028 (package-latest-release* package (force %updaters)))
1029 ((? upstream-source? source)
1030 (if (version>? (upstream-source-version source)
1031 (package-version package))
1033 (make-warning package
1034 (G_ "can be upgraded to ~a")
1035 (list (upstream-source-version source))
1038 (#f '()))) ; cannot find newer upstream release
1041 (define (check-archival package)
1042 "Check whether PACKAGE's source code is archived on Software Heritage. If
1043 it's not, and if its source code is a VCS snapshot, then send a \"save\"
1044 request to Software Heritage.
1046 Software Heritage imposes limits on the request rate per client IP address.
1047 This checker prints a notice and stops doing anything once that limit has been
1049 (define (response->warning url method response)
1050 (if (request-rate-limit-reached? url method)
1051 (list (make-warning package
1052 (G_ "Software Heritage rate limit reached; \
1055 (list (make-warning package
1056 (G_ "'~a' returned ~a")
1057 (list url (response-code response))
1060 (define skip-key (gensym "skip-archival-check"))
1062 (define (skip-when-limit-reached url method)
1063 (or (not (request-rate-limit-reached? url method))
1064 (throw skip-key #t)))
1066 (parameterize ((%allow-request? skip-when-limit-reached))
1069 (match (and (origin? (package-source package))
1070 (package-source package))
1073 ((= origin-uri (? git-reference? reference))
1075 (git-reference-url reference))
1077 (git-reference-commit reference))
1079 (match (if (commit-id? commit)
1080 (or (lookup-revision commit)
1081 (lookup-origin-revision url commit))
1082 (lookup-origin-revision url commit))
1083 ((? revision? revision)
1086 ;; Revision is missing from the archive, attempt to save it.
1089 (save-origin (git-reference-url reference) "git")
1092 ;; TRANSLATORS: "Software Heritage" is a proper noun
1093 ;; that must remain untranslated. See
1094 ;; <https://www.softwareheritage.org>.
1095 (G_ "scheduled Software Heritage archival")
1097 (lambda (key url method response . _)
1098 (cond ((= 429 (response-code response))
1101 (G_ "archival rate limit exceeded; \
1105 (response->warning url method response))))))))
1107 ;; Since "save" origins are not supported for non-VCS source, all
1108 ;; we can do is tell whether a given tarball is available or not.
1109 (if (origin-sha256 origin) ;XXX: for ungoogled-chromium
1110 (match (lookup-content (origin-sha256 origin) "sha256")
1112 (list (make-warning package
1113 (G_ "source not archived on Software \
1120 ((key url method response)
1121 (response->warning url method response))
1123 (if (eq? key skip-key)
1125 (apply throw key args)))))))
1129 ;;; Source code formatting.
1132 (define (report-tabulations package line line-number)
1133 "Warn about tabulations found in LINE."
1134 (match (string-index line #\tab)
1137 (make-warning package
1138 (G_ "tabulation on line ~a, column ~a")
1139 (list line-number index)
1141 (location (package-file package)
1145 (define (report-trailing-white-space package line line-number)
1146 "Warn about trailing white space in LINE."
1147 (and (not (or (string=? line (string-trim-right line))
1148 (string=? line (string #\page))))
1149 (make-warning package
1150 (G_ "trailing white space on line ~a")
1153 (location (package-file package)
1157 (define (report-long-line package line line-number)
1158 "Emit a warning if LINE is too long."
1159 ;; Note: We don't warn at 80 characters because sometimes hashes and URLs
1160 ;; make it hard to fit within that limit and we want to avoid making too
1162 (and (> (string-length line) 90)
1163 (make-warning package
1164 (G_ "line ~a is way too long (~a characters)")
1165 (list line-number (string-length line))
1167 (location (package-file package)
1171 (define %hanging-paren-rx
1172 (make-regexp "^[[:blank:]]*[()]+[[:blank:]]*$"))
1174 (define (report-lone-parentheses package line line-number)
1175 "Emit a warning if LINE contains hanging parentheses."
1176 (and (regexp-exec %hanging-paren-rx line)
1177 (make-warning package
1178 (G_ "parentheses feel lonely, \
1179 move to the previous or next line")
1182 (location (package-file package)
1186 (define %formatting-reporters
1187 ;; List of procedures that report formatting issues. These are not separate
1188 ;; checkers because they would need to re-read the file.
1189 (list report-tabulations
1190 report-trailing-white-space
1192 report-lone-parentheses))
1194 (define* (report-formatting-issues package file starting-line
1195 #:key (reporters %formatting-reporters))
1196 "Report white-space issues in FILE starting from STARTING-LINE, and report
1198 (define (sexp-last-line port)
1199 ;; Return the last line of the sexp read from PORT or an estimate thereof.
1200 (define &failure (list 'failure))
1202 (let ((start (ftell port))
1203 (start-line (port-line port))
1204 (sexp (catch 'read-error
1205 (lambda () (read port))
1207 (let ((line (port-line port)))
1208 (seek port start SEEK_SET)
1209 (set-port-line! port start-line)
1210 (if (eq? sexp &failure)
1211 (+ start-line 60) ;conservative estimate
1214 (call-with-input-file file
1216 (let loop ((line-number 1)
1219 (let ((line (read-line port)))
1220 (if (or (eof-object? line)
1221 (and last-line (> line-number last-line)))
1223 (if (and (= line-number starting-line)
1225 (loop (+ 1 line-number)
1226 (+ 1 (sexp-last-line port))
1228 (loop (+ 1 line-number)
1232 (if (< line-number starting-line)
1234 (filter-map (lambda (report)
1235 (report package line line-number))
1236 reporters)))))))))))
1238 (define (check-formatting package)
1239 "Check the formatting of the source code of PACKAGE."
1240 (let ((location (package-location package)))
1242 (and=> (search-path %load-path (location-file location))
1244 ;; Report issues starting from the line before the 'package'
1245 ;; form, which usually contains the 'define' form.
1246 (report-formatting-issues package file
1247 (- (location-line location) 1))))
1252 ;;; List of checkers.
1255 (define %local-checkers
1259 (description "Validate package descriptions")
1260 (check check-description-style))
1262 (name 'inputs-should-be-native)
1263 (description "Identify inputs that should be native inputs")
1264 (check check-inputs-should-be-native))
1266 (name 'inputs-should-not-be-input)
1267 (description "Identify inputs that shouldn't be inputs at all")
1268 (check check-inputs-should-not-be-an-input-at-all))
1271 ;; TRANSLATORS: <license> is the name of a data type and must not be
1273 (description "Make sure the 'license' field is a <license> \
1275 (check check-license))
1278 (description "Suggest 'mirror://' URLs")
1279 (check check-mirror-url))
1281 (name 'source-file-name)
1282 (description "Validate file names of sources")
1283 (check check-source-file-name))
1285 (name 'source-unstable-tarball)
1286 (description "Check for autogenerated tarballs")
1287 (check check-source-unstable-tarball))
1290 (description "Report failure to compile a package to a derivation")
1291 (check check-derivation))
1293 (name 'patch-file-names)
1294 (description "Validate file names and availability of patches")
1295 (check check-patch-file-names))
1298 (description "Look for formatting issues in the source")
1299 (check check-formatting))))
1301 (define %network-dependent-checkers
1305 (description "Validate package synopses")
1306 (check check-synopsis-style))
1308 (name 'gnu-description)
1309 (description "Validate synopsis & description of GNU packages")
1310 (check check-gnu-synopsis+description))
1313 (description "Validate home-page URLs")
1314 (check check-home-page))
1317 (description "Validate source URLs")
1318 (check check-source))
1321 (description "Suggest GitHub URLs")
1322 (check check-github-url))
1325 (description "Check the Common Vulnerabilities and Exposures\
1327 (check check-vulnerabilities))
1330 (description "Check the package for new upstream releases")
1331 (check check-for-updates))
1334 (description "Ensure source code archival on Software Heritage")
1335 (check check-archival))))
1337 (define %all-checkers
1338 (append %local-checkers
1339 %network-dependent-checkers))