Bladeren bron

Fix chat login on Cloudron's form-only consent page

Johannes Zellner 1 week geleden
bovenliggende
commit
e03fe16f86
5 gewijzigde bestanden met toevoegingen van 118 en 10 verwijderingen
  1. 3 2
      AGENTS.md
  2. 62 0
      chat_login.c
  3. 26 0
      chat_login_test.c
  4. 1 1
      docs/network.html
  5. 26 7
      tools/mock_chat.py

+ 3 - 2
AGENTS.md

@@ -1037,8 +1037,9 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   Cloudron OIDC `/openid/interaction/<uid>` page), the page's
   `cloudron-config` JSON `submitUrl`, `POST {"username","password",
   "totpToken"}` (401 / `twoFactorRequired`+`totpRequired` / passkey-only /
-  `redirectTo`), then following `redirectTo` to `/auth/callback` (a consent
-  interaction is auto-confirmed) and validating with `/api/me`.  External
+  `redirectTo`), then following `redirectTo` to `/auth/callback` (the consent
+  interaction is a bare auto-submit form, so its `<form action=.../confirm>`
+  is parsed and posted) and validating with `/api/me`.  External
   SSO and passkey accounts use the pasted-cookie screen.  The password is
   wiped after each attempt and never stored; the Chat host's cookie header
   and the server go to `/share/chat.cfg` (or the given path) and are

+ 62 - 0
chat_login.c

@@ -67,6 +67,64 @@ static int c_find(const char *hay, unsigned int len, const char *needle,
 	return -1;
 }
 
+/* Cloudron's consent interaction page is a bare auto-submit form, not the
+ * login SPA:  <form id="submitForm" method="post"
+ *                   action="/openid/interaction/<uid>/confirm">
+ * Return the action of such an interaction form (relative to the page). */
+static int c_form_action(const char *html, unsigned int len, char *out,
+			 unsigned int cap)
+{
+	unsigned int at = 0;
+
+	while (at < len) {
+		int f = c_find(html, len, "<form", at);
+		int end, a;
+
+		if (f < 0)
+			return 0;
+		end = c_find(html, len, ">", (unsigned int)f);
+		if (end < 0)
+			return 0;
+		a = c_find(html, (unsigned int)end, "action", (unsigned int)f);
+		if (a >= 0) {
+			unsigned int i = (unsigned int)a + 6, k = 0;
+			char q;
+
+			while (i < (unsigned int)end &&
+			       (html[i] == ' ' || html[i] == '\t' ||
+				html[i] == '\r' || html[i] == '\n'))
+				i++;
+			if (i < (unsigned int)end && html[i] == '=') {
+				i++;
+				while (i < (unsigned int)end &&
+				       (html[i] == ' ' || html[i] == '\t' ||
+					html[i] == '\r' || html[i] == '\n'))
+					i++;
+				if (i < (unsigned int)end &&
+				    (html[i] == '"' || html[i] == '\'')) {
+					q = html[i++];
+					while (i < (unsigned int)end &&
+					       html[i] != q) {
+						if (k + 1 >= cap)
+							return 0;
+						out[k++] = html[i++];
+					}
+					out[k] = '\0';
+					if (i < (unsigned int)end &&
+					    html[i] == q &&
+					    c_find(out, k,
+						   "/openid/interaction/", 0) >= 0 &&
+					    (c_suffix(out, "/confirm") ||
+					     c_suffix(out, "/abort")))
+						return 1;
+				}
+			}
+		}
+		at = (unsigned int)end + 1;
+	}
+	return 0;
+}
+
 /* ---- server URL / config file ---- */
 
 int chat_server_normalize(const char *in, char *out, unsigned int cap)
@@ -299,6 +357,10 @@ int chat_login_parse_page(const char *html, unsigned int len,
 				raw[0] = '\0';
 		}
 	}
+	if (!raw[0]) {
+		if (!c_form_action(html, len, raw, sizeof(raw)))
+			raw[0] = '\0';
+	}
 	if (!raw[0])
 		return -1;
 	return resolve(base_url, raw, submit_url, cap);

+ 26 - 0
chat_login_test.c

@@ -70,6 +70,32 @@ static void test_page(void)
 		CHECK(chat_login_parse_page(p6, strlen(p6), "https://h.test/",
 					    url, sizeof(url), name, sizeof(name)) == -1);
 	}
+	/* The real Cloudron consent page: a bare auto-submit form with no
+	 * cloudron-config JSON (dashboard/oidc_interaction_confirm.html). */
+	{
+		const char *p7 =
+			"<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n"
+			"    <title>Authorize chat</title>\n  </head>\n<body>\n"
+			"<form id=\"submitForm\" method=\"post\" "
+			"action=\"/openid/interaction/UdA3nm_PJ/confirm\">\n"
+			"  <!-- <button type=\"submit\"></button> -->\n"
+			"</form>\n"
+			"<script type=\"module\" "
+			"src=\"/src/oidcinteractionconfirm.js\"></script>\n"
+			"</body>\n</html>\n";
+
+		CHECK(chat_login_parse_page(p7, strlen(p7),
+					    "https://my.example.com/openid/interaction/UdA3nm_PJ",
+					    url, sizeof(url), name, sizeof(name)) == 0);
+		CHECK(strcmp(url, "https://my.example.com/openid/interaction/UdA3nm_PJ/confirm") == 0);
+	}
+	/* Other forms must not be mistaken for an interaction form. */
+	{
+		const char *p8 = "<form action=\"/search\"><input name=\"q\"></form>";
+
+		CHECK(chat_login_parse_page(p8, strlen(p8), "https://h.test/",
+					    url, sizeof(url), name, sizeof(name)) == -1);
+	}
 }
 
 static void test_reply(void)

+ 1 - 1
docs/network.html

@@ -377,7 +377,7 @@
 			<li><code>GET &lt;server&gt;/auth/login</code> follows the redirect to the Cloudron OIDC provider's <code>/openid/interaction/&lt;uid&gt;</code> page, collecting the PKCE pre-session cookie on the Chat host and the interaction cookies on the provider.</li>
 			<li>The page's <code>&lt;script id="cloudron-config"&gt;</code> JSON names a relative <code>submitUrl</code> (a plain <code>"submitUrl":"…"</code> scan is the fallback).  No config means external SSO: the client offers the cookie screen.</li>
 			<li><code>POST submitUrl {"username","password","totpToken"}</code> answers 401 with a message, <code>{"twoFactorRequired":true,"totpRequired":true}</code> (the client asks for the code and reposts), <code>totpRequired:false</code> (passkey-only: cookie screen) or <code>{"redirectTo"}</code>.</li>
-			<li>Following <code>redirectTo</code> ends at <code>/auth/callback</code> on the Chat host, which sets <code>connect.sid</code>.  A consent interaction on the way is confirmed like the web UI does; an abort page means the account has no access.  <code>GET /api/me</code> validates the session.</li>
+			<li>Following <code>redirectTo</code> ends at <code>/auth/callback</code> on the Chat host, which sets <code>connect.sid</code>.  The consent interaction on the way is a bare auto-submit form (no <code>cloudron-config</code>): its <code>&lt;form action="/openid/interaction/&lt;uid&gt;/confirm"&gt;</code> is posted like the web UI does, and an <code>/abort</code> form or page means the account has no access.  <code>GET /api/me</code> validates the session.</li>
 		</ol>
 		<p>
 			The password lives only in the form field and the request buffer;

+ 26 - 7
tools/mock_chat.py

@@ -7,7 +7,8 @@ Two plain-HTTP origins on the host, reachable from a QEMU slirp guest as
   chat  (default port 18080): /auth/login, /auth/callback, /auth/return,
         /auth/logout and the /api/* routes the client uses
   login (default port 18081): /openid/auth, /openid/interaction/<uid>
-        (HTML with the cloudron-config JSON), POST .../login and .../confirm
+        (login page with cloudron-config JSON, consent page as a bare
+        auto-submit form), POST .../login and .../confirm
 
 Accounts (password "test"):
   test     plain login
@@ -216,6 +217,25 @@ LOGIN_PAGE = """<!DOCTYPE html>
 </head><body><div id="app"></div></body></html>
 """
 
+# The real Cloudron consent page (dashboard/oidc_interaction_confirm.html) is
+# a bare auto-submit form, not the login SPA: no cloudron-config JSON.
+CONSENT_PAGE = """<!DOCTYPE html>
+<html lang="en">
+  <head>
+    <title>Authorize chat</title>
+  </head>
+<body>
+
+<form id="submitForm" method="post" action="/openid/interaction/%s/confirm">
+  <!-- <button type="submit"></button> -->
+</form>
+
+<script type="module" src="/src/oidcinteractionconfirm.js"></script>
+
+</body>
+</html>
+"""
+
 
 class LoginHandler(Base):
     def do_GET(self):
@@ -241,12 +261,11 @@ class LoginHandler(Base):
                 if not it or self.cookies().get("_interaction") != it["icookie"]:
                     return self.send(400, {"message": "interaction cookie missing"})
                 if it["user"] and it["consent"]:
-                    cfg = {"name": "chat", "submitUrl": "/openid/interaction/%s/confirm" % uid}
-                else:
-                    cfg = {"iconUrl": "/icon", "name": "chat", "note": "",
-                           "submitUrl": "/openid/interaction/%s/login" % uid,
-                           "passkeyLoginUrl": "/openid/interaction/%s/passkey_login" % uid,
-                           "footer": "&copy; mock", "language": None}
+                    return self.send(200, CONSENT_PAGE % uid, "text/html")
+                cfg = {"iconUrl": "/icon", "name": "chat", "note": "",
+                       "submitUrl": "/openid/interaction/%s/login" % uid,
+                       "passkeyLoginUrl": "/openid/interaction/%s/passkey_login" % uid,
+                       "footer": "&copy; mock", "language": None}
                 return self.send(200, LOGIN_PAGE % json.dumps(cfg), "text/html")
             if len(parts) == 3 and parts[:2] == ["openid", "auth"]:
                 uid = parts[2]