From 5a2562fd07e33c46cd4fb540e5bf57c0f35bd757 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Fri, 28 Jul 2023 06:59:25 -0500 Subject: [PATCH] Error-handling for invalid requests etc --- webtentacle.lisp | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/webtentacle.lisp b/webtentacle.lisp index d48be8e..ba34657 100644 --- a/webtentacle.lisp +++ b/webtentacle.lisp @@ -71,12 +71,21 @@ in the docstring of SERVER." type "application/activity+json"))))) +(define-condition invalid-acct-uri (error) () + (:documentation "Thrown when a user@host acct URI is expected, but is lacking a user or host potion.")) + + (defun resource-user-host (resource) "Given a queried RESOURCE, return a list of its contained user and host." (let* ((sans-acct (if (str:starts-with-p "acct:" resource) (subseq resource 5) - resource))) - (str:split #\@ sans-acct))) + resource)) + (user-host (str:split #\@ sans-acct))) + (if (or (not (eq (length user-host) 2)) + (str:emptyp (car user-host)) + (str:emptyp (cadr user-host))) + (error 'invalid-acct-uri :message "Invalid acct resource") + user-host))) (defun filter-link-rels (rels link-plists) @@ -109,14 +118,25 @@ the RESOURCE and RELS parameters from a Webfinger HTTP request, return the response JSON in Clack’s format. This can be used if you don’t want to wrap your server with SERVER, and would rather handle the Webfinger path yourself." - (list 200 - '(:content-type "text/plain") - (list (format nil "~A~%" - (apply #'user-json - (filter-user-info-rels - rels - (apply user-info-func - (resource-user-host resource)))))))) + (list + 200 + '(:content-type "text/plain") + (list + (format + nil "~A" + (handler-case + (if (not resource) + "\"No resource specified\"" + (or (apply #'user-json + (filter-user-info-rels + rels + (apply user-info-func + (resource-user-host resource)))) + "\"Couldn't find user\"")) + (invalid-acct-uri () + "\"Invalid acct URI\"") + (error (any-error) + (format nil "\"Server error: ~A\"" any-error))))))) (defun server (env user-info-func &optional (clack-app nil))