From 1fbed8e3178c2c2bc2f093374e0035a1fe567377 Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sat, 28 May 2022 14:17:36 -0400 Subject: [PATCH] fix(web/panettone): Don't add extra padding when already padded Because of math being upsetting, we were adding 4 padding characters to an already-properly-padded base64 string, which broke tazjin. This also breaks this function out into panettone.util, and adds a test for it. Change-Id: I7bc8a440ad9d0917272dd9f2e341081ea14693da Reviewed-on: https://cl.tvl.fyi/c/depot/+/5782 Autosubmit: grfn Reviewed-by: tazjin Tested-by: BuildkiteCI --- web/panettone/default.nix | 1 + web/panettone/src/authentication.lisp | 8 +------- web/panettone/src/packages.lisp | 2 +- web/panettone/src/util.lisp | 8 ++++++++ web/panettone/test/util_test.lisp | 9 +++++++++ 5 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 web/panettone/test/util_test.lisp diff --git a/web/panettone/default.nix b/web/panettone/default.nix index 862dac95a..283f83499 100644 --- a/web/panettone/default.nix +++ b/web/panettone/default.nix @@ -42,6 +42,7 @@ depot.nix.buildLisp.program { ./test/package.lisp ./test/model_test.lisp ./test/inline-markdown_test.lisp + ./test/util_test.lisp ]; expression = "(fiveam:run!)"; diff --git a/web/panettone/src/authentication.lisp b/web/panettone/src/authentication.lisp index 5c6d82e02..3ce07aa8d 100644 --- a/web/panettone/src/authentication.lisp +++ b/web/panettone/src/authentication.lisp @@ -78,12 +78,6 @@ the user, however email addresses are temporarily not available." ;; TODO(tazjin): Figure out actual displayName mapping in tokens. :displayname username))) -(defun add-missing-base64-padding (s) - "Add any missing padding characters to the (un-padded) base64 string `S', such -that it can be successfully decoded by the `BASE64' package" - ;; I apologize - (format nil "~A~v@{~A~:*~}" s (- 4 (mod (length s) 4)) "=")) - (defun fetch-token (code) "Fetches the access token on completion of user authentication through the OAuth2 endpoint and returns the resulting user object." @@ -117,5 +111,5 @@ the OAuth2 endpoint and returns the resulting user object." ;; lisp base64 library doesn't know how to deal with ;; that - we need to add those extra padding ;; characters here. - (add-missing-base64-padding payload))))) + (panettone.util:add-missing-base64-padding payload))))) (claims-to-user claims)))))) diff --git a/web/panettone/src/packages.lisp b/web/panettone/src/packages.lisp index a63f4c766..4ff4c070f 100644 --- a/web/panettone/src/packages.lisp +++ b/web/panettone/src/packages.lisp @@ -1,7 +1,7 @@ (defpackage panettone.util (:use :cl :klatre) (:import-from :alexandria :when-let) - (:export :integer-env)) + (:export :integer-env :add-missing-base64-padding)) (defpackage panettone.css (:use :cl :lass) diff --git a/web/panettone/src/util.lisp b/web/panettone/src/util.lisp index 9fd9ceaa7..2abedf7b8 100644 --- a/web/panettone/src/util.lisp +++ b/web/panettone/src/util.lisp @@ -5,3 +5,11 @@ (when-let ((str (uiop:getenvp var))) (try-parse-integer str)) default)) + +(defun add-missing-base64-padding (s) + "Add any missing padding characters to the (un-padded) base64 string `S', such +that it can be successfully decoded by the `BASE64' package" + ;; I apologize + (let* ((needed-padding (mod (length s) 4)) + (pad-chars (if (zerop needed-padding) 0 (- 4 needed-padding)))) + (format nil "~A~v@{~A~:*~}" s pad-chars "="))) diff --git a/web/panettone/test/util_test.lisp b/web/panettone/test/util_test.lisp new file mode 100644 index 000000000..ff52d916c --- /dev/null +++ b/web/panettone/test/util_test.lisp @@ -0,0 +1,9 @@ +(in-package :panettone.tests) +(declaim (optimize (safety 3))) + +(test add-missing-base64-padding-test + (is (string= + "abcdef" + (base64:base64-string-to-string + (panettone.util:add-missing-base64-padding + "YWJjZGVm")))))