From 9eadcfdfe6f7b26ba44360db9f828fdbe7a78fe7 Mon Sep 17 00:00:00 2001 From: Tino Calancha Date: Sun, 27 Jun 2021 17:53:30 +0200 Subject: [PATCH] lisp/auth-source-pass.el: Keep legitimate spaces inside data Users should be able to store a field as follows: message: remember: Destroy the image and you will break the enemy and later, recover the message untouched, i.e.: "remember: Destroy the image and you will break the enemy" * lisp/auth-source-pass.el (auth-source-pass--parse-data): Preserve inner spaces at data. * test/lisp/auth-source-pass-tests.el (auth-source-pass-parse-with-colons-in-data): Add test. --- lisp/auth-source-pass.el | 12 +++++------- test/lisp/auth-source-pass-tests.el | 6 ++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lisp/auth-source-pass.el b/lisp/auth-source-pass.el index c512c6fe4fa..914f8d2f1bf 100644 --- a/lisp/auth-source-pass.el +++ b/lisp/auth-source-pass.el @@ -167,15 +167,13 @@ The secret is the first line of CONTENTS." (defun auth-source-pass--parse-data (contents) "Parse the password-store data in the string CONTENTS and return an alist. CONTENTS is the contents of a password-store formatted file." - (let ((lines (split-string contents "\n" t "[ \t]+"))) + (let ((lines (cdr (split-string contents "\n" t "[ \t]+")))) (seq-remove #'null (mapcar (lambda (line) - (let ((pair (mapcar (lambda (s) (string-trim s)) - (split-string line ":")))) - (when (> (length pair) 1) - (cons (car pair) - (mapconcat #'identity (cdr pair) ":"))))) - (cdr lines))))) + (when-let ((pos (seq-position line ?:))) + (cons (string-trim (substring line 0 pos)) + (string-trim (substring line (1+ pos)))))) + lines)))) (defun auth-source-pass--do-debug (&rest msg) "Call `auth-source-do-debug` with MSG and a prefix." diff --git a/test/lisp/auth-source-pass-tests.el b/test/lisp/auth-source-pass-tests.el index a2f84f20e8e..d050ac5b695 100644 --- a/test/lisp/auth-source-pass-tests.el +++ b/test/lisp/auth-source-pass-tests.el @@ -49,6 +49,12 @@ '(("key1" . "val1") ("key2" . "val2")))))) +(ert-deftest auth-source-pass-parse-with-colons-in-data () + (let ((content "pass\n--\nkey1 :val1\nkey2: please: keep my space after colon\n\n")) + (should (equal (auth-source-pass--parse-data content) + '(("key1" . "val1") + ("key2" . "please: keep my space after colon")))))) + (defvar auth-source-pass--debug-log nil "Contains a list of all messages passed to `auth-source-do-debug`.") -- 2.39.2