]> git.eshelyaron.com Git - emacs.git/commitdiff
Recognise hybrid IPv6/IPv4 addresses in textsec (bug#54624)
authorMattias Engdegård <mattiase@acm.org>
Mon, 11 Apr 2022 14:22:38 +0000 (16:22 +0200)
committerMattias Engdegård <mattiase@acm.org>
Mon, 11 Apr 2022 14:29:24 +0000 (16:29 +0200)
* lisp/international/textsec.el (textsec--ipvx-address-p):
  Recognise hybrid addresses like "::ffff:129.55.2.201".
  Combine to a single regexp and translate to rx.
  Remove some regexp ambiguity (relint complaint).
* test/lisp/international/textsec-tests.el (test-suspiction-domain):
  Add test cases.

lisp/international/textsec.el
test/lisp/international/textsec-tests.el

index cca49986fc43faa527badd70c21cc58170eecfb4..3e7879093843eb2386a67a04ae0f71bca0853ded 100644 (file)
@@ -233,12 +233,18 @@ The scripts are as defined by the Unicode Standard Annex 24 (UAX#24)."
 
 (defun textsec--ipvx-address-p (domain)
   "Return non-nil if DOMAIN is an ipv4 or ipv6 address."
-  (or (string-match-p "\\`\\([0-9]\\{1,3\\}\\.?\\)\\{1,4\\}\\'" domain)
-      (let ((ipv6 "\\([0-9a-f]\\{0,4\\}:?\\)\\{1,8\\}"))
-        ;; With brackets.
-        (or (string-match-p (format "\\`\\[%s\\]\\'" ipv6) domain)
-            ;; Without.
-            (string-match-p (format "\\`%s\\'" ipv6) domain)))))
+  ;; This is a very relaxed pattern for IPv4 or IPv6 addresses.  The
+  ;; assumption is that any malformed address accepted by this rule
+  ;; will be rejected by the actual address parser eventually.
+  (rx-let ((ipv4 (** 1 4
+                     (** 1 3 (in "0-9"))
+                     (? ".")))
+           (ipv6 (: (** 1 7
+                        (** 0 4 (in "0-9a-f"))
+                        ":")
+                    (** 0 4 (in "0-9a-f"))
+                    (? ":" ipv4))))
+    (string-match-p (rx bos (or ipv4 ipv6 (: "[" ipv6 "]")) eos) domain)))
 
 (defun textsec-domain-suspicious-p (domain)
   "Say whether DOMAIN's name looks suspicious.
index 9216d334f871eae8c724e551012ba3b7dc4111d8..6b0773dc407096a142a80ce3432cae2b43a17c5f 100644 (file)
   (should-not (textsec-domain-suspicious-p
                "[21a:34aa:c782:3ad2:1bf8:73f8:141:66e8]"))
   (should (textsec-domain-suspicious-p
-           "[21a:34aa:c782:3ad2:1bf8:73f8:141:66e8")))
+           "[21a:34aa:c782:3ad2:1bf8:73f8:141:66e8"))
+  (should-not (textsec-domain-suspicious-p "138.25.106.12"))
+  (should-not (textsec-domain-suspicious-p "2001:db8::ff00:42:8329"))
+  (should-not (textsec-domain-suspicious-p "::ffff:129.55.2.201")))
 
 (ert-deftest test-suspicious-local ()
   (should-not (textsec-local-address-suspicious-p "larsi"))