Просмотр исходного кода

Keep browser sessions across login POSTs and route #fragments in place

- Cookies: the curl helpers re-attach the shared jar before every request
  (uhttp_init() after a POST cleared h.jar, so the request after the OIDC
  login POST went out cookie-less: "authorization request has expired"),
  and save by merging their own changes into /share/bcookies instead of
  overwriting it.  The jar holds 48 cookies and evicts the one set or
  updated longest ago, so a refreshed session cookie outlives the one-shot
  OIDC interaction cookies.
- Fragment navigation: location.hash is live; a change of only the
  #fragment (location.hash/href/assign/replace, #... links, back/forward)
  updates the URL, address bar and history and fires hashchange instead of
  reloading, which the hash-routed Cloudron dashboard depends on.  window
  events no longer run their handlers twice.
- <select> exposes options/multiple, and an <option> without a value
  reports its text (Vue's v-model on selects).
Johannes Zellner 1 неделя назад
Родитель
Сommit
0e15afe99c
10 измененных файлов с 305 добавлено и 18 удалено
  1. 18 3
      AGENTS.md
  2. 86 2
      browser.c
  3. 1 2
      docs/network.html
  4. 0 0
      docs/userland.html
  5. 98 6
      js_browser.c
  6. 7 0
      js_browser.h
  7. 71 2
      ucmd_curl.c
  8. 6 2
      uhttp_cookie.c
  9. 4 1
      uhttp_cookie.h
  10. 14 0
      uhttp_cookie_test.c

+ 18 - 3
AGENTS.md

@@ -1369,7 +1369,13 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   returns the status and body whatever the HTTP status (2 MB response, 16 KB
   request body).  Web Storage is stored in `/share/bls<origin-hash>`
   (local) and `/share/bss<pid>-<origin-hash>` (session, per window),
-  outside the page heap that navigation resets.  When `/data` is mounted
+  outside the page heap that navigation resets.  The helpers share one
+  jar, `/share/bcookies`: `h.jar` is attached before every request
+  (`uhttp_init()` after a POST clears it, which made the request after the
+  OIDC login POST go out cookie-less: "authorization request has
+  expired"), and a helper saves by re-reading the file and replaying only
+  its own request's cookie additions/changes/deletions (`jar_save()` in
+  `ucmd_curl.c`), so parallel helpers cannot clobber each other.  When `/data` is mounted
   the browser also persists the helpers' cookie jar (`/share/bcookies` ->
   `/data/BCOOKIES.BIN`) and localStorage (every origin in one
   `/data/BSTORE.DAT`: `NBLS1\n` then `<origin>\n<len>\n<json>\n` records,
@@ -1380,7 +1386,16 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   3 s (`PERSIST_MS`).  The helpers only touch `/share`.  Both files are
   plaintext bearer credentials, like `CHAT.CFG`; sessionStorage stays in
   RAM.  Without `/data` the disk accesses just fail.  `console.*` print
-  Error stacks.  `location.assign`/`replace`/`reload` navigate.  The
+  Error stacks.  `location.assign`/`replace`/`reload` navigate, except that a
+  change of only the `#fragment` stays on the page: `location.hash` is a
+  live accessor, `href`/`hash`/`assign`/`replace` and clicks on `#...` links
+  update `page_url`, the address bar and history (`host.set_hash`), and a
+  `hashchange` event fires on `window` from the next tick; back/forward
+  between fragments of the same page do the same (hash-routed apps such as
+  the Cloudron dashboard would otherwise reload on every menu click).
+  Clicks on plain elements with listeners (`div role="button"`) reach the
+  innermost laid-out box under the pointer.  `<select>` exposes `options`
+  and `multiple`, and an `<option>` without `value` reports its text.  The
   Cloudron dashboard (`my.<domain>`: a Vite/Vue 3 app, 12 modules / 3.5 MB)
   boots, builds its PKCE request, and its OIDC login form renders, submits
   through `fetch()` with the interaction cookie and shows the server's
@@ -1441,7 +1456,7 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   exists, otherwise `/share/chat.cfg` (or the given path), and are
   re-saved when the server rolls the cookie.  A session left in the old
   `/share/chat.cfg` is copied to `/data` once.  `uhttp_cookie.c/h` is the jar
-  (24 host-only cookies, Path prefix match, Secure, Max-Age/1970-Expires
+  (48 host-only cookies, least-recently-set eviction, Path prefix match, Secure, Max-Age/1970-Expires
   deletion); uhttp stores every `Set-Cookie` (redirects too), sends matching
   cookies on each hop, splits requests/bodies into 1024-byte TLS records or
   MSS-sized TCP sends, takes `accept`/`extra_headers`, follows up to 8

+ 86 - 2
browser.c

@@ -251,6 +251,8 @@ static unsigned int form_dirty;
 
 static int form_descendant(unsigned int node, unsigned int form);
 static int form_submit_form(unsigned int node, int fire_event);
+static int link_fragment_nav(unsigned int node);
+static void history_push(const char *url);
 static void form_reset_event(unsigned int form);
 
 static unsigned int s_len(const char *s);
@@ -614,6 +616,45 @@ static void js_host_navigate(const char *url)
 	cancel_fetch = 1;
 }
 
+/* Same-document fragment change (location.hash, an <a href="#/x"> click,
+ * back/forward between fragments): the page URL, address bar and history
+ * follow, nothing is reloaded.  `push` adds a history entry. */
+static int page_set_fragment(const char *url, int push)
+{
+	if (!url || !url[0] || s_len(url) >= sizeof(page_url))
+		return -1;
+	s_copy(page_url, url, sizeof(page_url));
+	s_copy(url_in.text, page_url, NGUI_INPUT_MAX);
+	url_in.len = s_len(url_in.text);
+	url_in.cursor = url_in.len;
+	url_in.scroll = 0;
+	if (push)
+		history_push(page_url);
+	ngui_invalidate(&ui);
+	return 0;
+}
+
+static int js_host_set_hash(const char *url)
+{
+	return page_set_fragment(url, 1);
+}
+
+/* 1 when `url` is this page with a different #fragment (and JavaScript runs
+ * the page, so a hash router can react). */
+static int same_doc_fragment(const char *url)
+{
+	unsigned int a = 0, b = 0;
+
+	if (!js_browser_active() || !url)
+		return 0;
+	while (url[a] && url[a] != '#')
+		a++;
+	while (page_url[b] && page_url[b] != '#')
+		b++;
+	return url[a] == '#' && a == b && !strncmp(url, page_url, a) &&
+	       !s_eq(url, page_url);
+}
+
 static void js_host_changed(void)
 {
 	/* Class/style/attribute mutations change computed styles, not just the
@@ -1138,6 +1179,8 @@ static int js_host_click(unsigned int node)
 	 * default action, like a real click (delegated list handlers). */
 	if (!js_browser_event(node, "click", 1, 1))
 		return 1;
+	if (link_fragment_nav(node))
+		return 1;
 	href = br_href(&doc, node);
 	if (resolve_link(page_url, href, url, sizeof(url)) != 0)
 		return 0;
@@ -1753,6 +1796,7 @@ static void run_page_scripts(void)
 	host.http = js_host_http;
 	host.load_script = js_host_load_script;
 	host.storage = js_host_storage;
+	host.set_hash = js_host_set_hash;
 	host.page_url = page_url;
 	host.width = ui.w;
 	host.height = ui.h;
@@ -10249,6 +10293,34 @@ static void form_key(int k)
 	ngui_invalidate(&ui);
 }
 
+/* A link to "#..." (or to this page plus a fragment) on a scripted page is
+ * a same-document navigation: update the URL and fire hashchange.  Returns
+ * 1 when handled. */
+static int link_fragment_nav(unsigned int node)
+{
+	const char *href = br_href(&doc, node);
+	char url[BR_URL_MAX];
+	unsigned int n = 0;
+
+	if (!href || !href[0] || !js_browser_active())
+		return 0;
+	if (href[0] == '#') {
+		while (page_url[n] && page_url[n] != '#' && n + 1 < sizeof(url)) {
+			url[n] = page_url[n];
+			n++;
+		}
+		url[n] = '\0';
+		s_append(url, &n, href, sizeof(url));
+	} else if (resolve_link(page_url, href, url, sizeof(url)) != 0) {
+		return 0;
+	}
+	if (!same_doc_fragment(url))
+		return href[0] == '#';
+	page_set_fragment(url, 1);
+	js_browser_hashchange();
+	return 1;
+}
+
 /* The innermost laid-out box under a screen point: the smallest valid box
  * that contains it (the last one wins a tie, i.e. the later/deeper node).
  * Used to deliver clicks to elements that are neither links nor controls
@@ -10306,6 +10378,8 @@ static void handle_page_click(unsigned int *action)
 	form_blur();
 	if (!js_browser_event((unsigned int)node, "click", 1, 1))
 		return;
+	if (link_fragment_nav((unsigned int)node))
+		return;
 	if (resolve_link(page_url, br_href(&doc, (unsigned int)node), url,
 			 sizeof(url)) != 0)
 		return;
@@ -10411,13 +10485,23 @@ void _start(const char *arg)
 		if (action & ACT_BACK) {
 			if (hist_pos > 0) {
 				hist_pos--;
-				navigate(history[hist_pos], 0);
+				if (same_doc_fragment(history[hist_pos])) {
+					page_set_fragment(history[hist_pos], 0);
+					js_browser_hashchange();
+				} else {
+					navigate(history[hist_pos], 0);
+				}
 			}
 		}
 		if (action & ACT_FORWARD) {
 			if (hist_pos + 1 < hist_len) {
 				hist_pos++;
-				navigate(history[hist_pos], 0);
+				if (same_doc_fragment(history[hist_pos])) {
+					page_set_fragment(history[hist_pos], 0);
+					js_browser_hashchange();
+				} else {
+					navigate(history[hist_pos], 0);
+				}
 			}
 		}
 		if (action & ACT_RELOAD) {

Разница между файлами не показана из-за своего большого размера
+ 1 - 2
docs/network.html


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
docs/userland.html


+ 98 - 6
js_browser.c

@@ -72,6 +72,12 @@ static int node_delete(js_State *state, void *p, const char *name);
 static void push_element_url(js_State *state, unsigned int node,
 			     const char *name);
 static void rellist_supports(js_State *state);
+static void push_query_array(js_State *state, unsigned int root,
+			     const char *selector);
+static void node_push_text(js_State *state, unsigned int node);
+static void url_resolve(const char *base, const char *rel,
+			char *out, unsigned int cap);
+static int hash_pending;
 
 /* One wrapper per DOM node: the same element must have the same object
  * identity on every access, or library caches (jQuery expando data, Sizzle)
@@ -571,7 +577,8 @@ static int dispatch_object(js_State *state, unsigned int target,
 	}
 	if (target != JS_TARGET_WINDOW)
 		path[np++] = JS_TARGET_DOCUMENT;
-	if (np < (int)(sizeof(path) / sizeof(path[0])))
+	if (target != JS_TARGET_WINDOW &&
+	    np < (int)(sizeof(path) / sizeof(path[0])))
 		path[np++] = JS_TARGET_WINDOW;
 
 	/* Capture from the outermost ancestor down to the parent. */
@@ -1926,6 +1933,22 @@ static int node_has(js_State *state, void *p, const char *name)
 	}
 	if (!strcmp(name, "href")) { push_attr(state, node, "href", 0); return 1; }
 	if (!strcmp(name, "src")) { push_attr(state, node, "src", 0); return 1; }
+	if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_SELECT) {
+		if (!strcmp(name, "options")) {
+			push_query_array(state, node, "option");
+			return 1;
+		}
+		if (!strcmp(name, "multiple")) {
+			js_pushboolean(state, node_attr(node, "multiple") != 0);
+			return 1;
+		}
+	}
+	/* An <option> without a value attribute has its text as value. */
+	if (n->type == BR_NODE_ELEM && n->tag == BR_TAG_OPTION &&
+	    !strcmp(name, "value") && !node_attr(node, "value")) {
+		node_push_text(state, node);
+		return 1;
+	}
 	if (!strcmp(name, "value")) {
 		char buf[128];
 
@@ -2369,22 +2392,78 @@ static void location_get(js_State *state)
 	js_pushstring(state, current_url ? current_url : "");
 }
 
-static void location_set(js_State *state)
+/* Length of a URL without its #fragment. */
+static unsigned int url_doc_len(const char *u)
+{
+	unsigned int n = 0;
+
+	while (u[n] && u[n] != '#')
+		n++;
+	return n;
+}
+
+/* Navigate, or, when only the #fragment changes, stay on the page: the
+ * host updates the URL (location.href follows it) and a hashchange event is
+ * queued.  Hash-routed apps (the Cloudron dashboard) navigate this way; a
+ * reload would restart the whole app. */
+static void location_go(const char *url)
 {
-	const char *url = js_tostring(state, 1);
+	char abs[1024];
+	unsigned int n;
 
+	if (!current_url || !url) {
+		if (host.navigate && url)
+			host.navigate(url);
+		return;
+	}
+	url_resolve(current_url, url, abs, sizeof(abs));
+	n = url_doc_len(abs);
+	if (host.set_hash && abs[n] == '#' && n == url_doc_len(current_url) &&
+	    !strncmp(abs, current_url, n)) {
+		if (strcmp(abs, current_url) && host.set_hash(abs) == 0)
+			hash_pending = 1;
+		return;
+	}
 	if (host.navigate)
 		host.navigate(url);
 }
 
+static void location_set(js_State *state)
+{
+	location_go(js_tostring(state, 1));
+}
+
 /* assign()/replace(): navigate (the history entry is not replaced yet). */
 static void location_assign(js_State *state)
 {
-	if (host.navigate)
-		host.navigate(js_tostring(state, 1));
+	location_go(js_tostring(state, 1));
 	js_pushundefined(state);
 }
 
+static void location_hash_get(js_State *state)
+{
+	const char *h = current_url ? strchr(current_url, '#') : 0;
+
+	js_pushstring(state, h && h[1] ? h : "");
+}
+
+static void location_hash_set(js_State *state)
+{
+	const char *v = js_tostring(state, 1);
+	char url[1024];
+	unsigned int n;
+
+	if (!current_url)
+		return;
+	n = url_doc_len(current_url);
+	if (n + 2 >= sizeof(url))
+		return;
+	memcpy(url, current_url, n);
+	url[n] = '#';
+	snprintf(url + n + 1, sizeof(url) - n - 1, "%s", v[0] == '#' ? v + 1 : v);
+	location_go(url);
+}
+
 static void location_reload(js_State *state)
 {
 	if (host.navigate && current_url)
@@ -2493,10 +2572,12 @@ static void push_location(js_State *state)
 		       p.path ? (int)p.path_len : 0);
 	js_setproperty(state, -2, "pathname");
 	LOC_FIELD(search);
-	LOC_FIELD(hash);
 	LOC_FIELD(origin);
 #undef LOC_FIELD
 
+	js_newcfunction(state, location_hash_get, "get hash", 0);
+	js_newcfunction(state, location_hash_set, "set hash", 1);
+	js_defaccessor(state, -3, "hash", 0);
 	js_newcfunction(state, location_reload, "reload", 0);
 	js_setproperty(state, -2, "reload");
 	js_newcfunction(state, location_assign, "assign", 1);
@@ -3196,6 +3277,11 @@ static void report_top(js_State *state)
 	js_pop(state, 1);
 }
 
+void js_browser_hashchange(void)
+{
+	hash_pending = 1;
+}
+
 void js_browser_tick(void)
 {
 	unsigned int now;
@@ -3203,6 +3289,12 @@ void js_browser_tick(void)
 
 	if (!J)
 		return;
+	if (hash_pending) {
+		hash_pending = 0;
+		js_browser_event(JS_TARGET_WINDOW, "hashchange", 0, 0);
+		if (!J)
+			return;
+	}
 	now = u_ticks_ms();
 	for (i = 0; i < JS_TIMERS; i++) {
 		if (!timers[i].used ||

+ 7 - 0
js_browser.h

@@ -53,6 +53,9 @@ struct js_browser_host {
 	 * With `set` the text is stored (returns 0); otherwise it is read into
 	 * out (returns its length, or -1). */
 	int (*storage)(int kind, const char *set, char *out, int cap);
+	/* Same-document #fragment change: update the page URL in place
+	 * (history, address bar) without loading; 0 on success. */
+	int (*set_hash)(const char *url);
 	const char *page_url;   /* location.href for every script on the page */
 	unsigned int width;     /* viewport size for window.inner* */
 	unsigned int height;
@@ -69,6 +72,10 @@ int js_browser_run(const char *source, const char *url);
  * host.load_script. */
 int js_browser_run_module(const char *source, const char *url);
 
+/* Queue a hashchange event on window (the host changed the fragment, e.g.
+ * a click on <a href="#/apps">); it fires from the next tick. */
+void js_browser_hashchange(void);
+
 /* Run due setTimeout/setInterval callbacks; call once per UI loop. */
 void js_browser_tick(void);
 

+ 71 - 2
ucmd_curl.c

@@ -157,10 +157,74 @@ static void jar_load(void)
 		uhttp_jar_init(&fetch_jar);
 }
 
+/* The jar as loaded before this request, and a scratch copy of the file. */
+static struct uhttp_jar jar_before, jar_file;
+
+static const struct uhttp_cookie *jar_lookup(const struct uhttp_jar *jar,
+					     const struct uhttp_cookie *c)
+{
+	int i;
+
+	for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
+		const struct uhttp_cookie *o = &jar->c[i];
+
+		if (o->used && !strcmp(o->host, c->host) &&
+		    !strcmp(o->path, c->path) && !strcmp(o->name, c->name))
+			return o;
+	}
+	return 0;
+}
+
+/* Replay one cookie into jar_file as a Set-Cookie line (or delete it). */
+static void jar_replay(const struct uhttp_cookie *c, int del)
+{
+	static char line[UHTTP_COOKIE_NAME_MAX + UHTTP_COOKIE_VALUE_MAX +
+			 UHTTP_COOKIE_PATH_MAX + 32];
+	unsigned int n = 0;
+
+	app_str(line, &n, c->name, sizeof(line));
+	app_ch(line, &n, '=', sizeof(line));
+	if (!del)
+		app_str(line, &n, c->value, sizeof(line));
+	app_str(line, &n, "; Path=", sizeof(line));
+	app_str(line, &n, c->path, sizeof(line));
+	if (c->secure)
+		app_str(line, &n, "; Secure", sizeof(line));
+	if (del)
+		app_str(line, &n, "; Max-Age=0", sizeof(line));
+	(void)uhttp_jar_set(&jar_file, c->host, c->path, line);
+}
+
+/* Save this request's cookie changes.  Other helpers may have saved while
+ * the request ran, so the file is re-read and only what this request added,
+ * changed or deleted is applied on top (writing our whole copy back would
+ * drop their cookies). */
 static void jar_save(void)
 {
-	u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&fetch_jar,
-		   (int)sizeof(fetch_jar));
+	int i, n;
+
+	n = u_syscall3(SYS_READ_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
+		       (int)sizeof(jar_file));
+	if (n != (int)sizeof(jar_file))
+		uhttp_jar_init(&jar_file);
+	for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
+		const struct uhttp_cookie *c = &fetch_jar.c[i];
+		const struct uhttp_cookie *o;
+
+		if (!c->used)
+			continue;
+		o = jar_lookup(&jar_before, c);
+		if (!o || strcmp(o->value, c->value) || o->secure != c->secure)
+			jar_replay(c, 0);
+	}
+	for (i = 0; i < UHTTP_COOKIE_MAX; i++) {
+		const struct uhttp_cookie *o = &jar_before.c[i];
+
+		if (o->used && !jar_lookup(&fetch_jar, o))
+			jar_replay(o, 1);
+	}
+	u_syscall3(SYS_WRITE_FILE, (int)FETCH_JAR_PATH, (int)&jar_file,
+		   (int)sizeof(jar_file));
 }
 
 struct fetch_times {
@@ -468,7 +532,12 @@ static void fetch_serve(const char *arg)
 		}
 
 		jar_load();
+		memcpy(&jar_before, &fetch_jar, sizeof(jar_before));
 		jar_changes = fetch_jar.changes;
+		/* Every request: uhttp_init() after a POST clears h.jar, and a
+		 * jar-less request after the OIDC login POST drops the
+		 * interaction cookie ("authorization request has expired"). */
+		h.jar = &fetch_jar;
 		helper_fetch(&h, url, cap, method[0] ? method : 0, ctype,
 			     post_len ? (char *)post : 0, post_len,
 			     buf, &body_len, &body_trunc, &err, &times);

+ 6 - 2
uhttp_cookie.c

@@ -177,6 +177,7 @@ static int store(struct uhttp_jar *jar, const char *host, const char *path,
 			return 0;
 		copy_n(c->value, sizeof(c->value), value, c_len(value));
 		c->secure = secure ? 1 : 0;
+		c->touch = ++jar->seq;
 		jar->changes++;
 		return 1;
 	}
@@ -184,11 +185,13 @@ static int store(struct uhttp_jar *jar, const char *host, const char *path,
 		if (!jar->c[i].used)
 			break;
 	if (i == UHTTP_COOKIE_MAX) {
-		/* Full: evict the oldest cookie. */
+		/* Full: evict the cookie set or updated longest ago, so a
+		 * session cookie the server keeps refreshing outlives a burst
+		 * of one-shot cookies. */
 		int j, old = 0;
 
 		for (j = 1; j < UHTTP_COOKIE_MAX; j++)
-			if (jar->c[j].seq < jar->c[old].seq)
+			if (jar->c[j].touch < jar->c[old].touch)
 				old = j;
 		i = old;
 	}
@@ -203,6 +206,7 @@ static int store(struct uhttp_jar *jar, const char *host, const char *path,
 	c->secure = secure ? 1 : 0;
 	c->used = 1;
 	c->seq = ++jar->seq;
+	c->touch = c->seq;
 	jar->changes++;
 	return 1;
 }

+ 4 - 1
uhttp_cookie.h

@@ -12,7 +12,9 @@
 #ifndef UHTTP_COOKIE_H
 #define UHTTP_COOKIE_H
 
-#define UHTTP_COOKIE_MAX        24
+/* Every OIDC login attempt adds four path-scoped interaction cookies that
+ * live for an hour, and the browser shares one jar across all sites. */
+#define UHTTP_COOKIE_MAX        48
 #define UHTTP_COOKIE_HOST_MAX   128
 #define UHTTP_COOKIE_PATH_MAX   160
 #define UHTTP_COOKIE_NAME_MAX   64
@@ -26,6 +28,7 @@ struct uhttp_cookie {
 	unsigned char used;
 	unsigned char secure;
 	unsigned int seq;               /* insertion order, for sending */
+	unsigned int touch;             /* last set/update, for eviction */
 };
 
 struct uhttp_jar {

+ 14 - 0
uhttp_cookie_test.c

@@ -135,6 +135,20 @@ static void test_import_and_limits(void)
 	CHECK(strstr(hdr("h", "/", 0), "n0=") == NULL);
 	CHECK(strstr(hdr("h", "/", 0), "n1=") == NULL);
 	CHECK(strstr(hdr("h", "/", 0), "n2=v2") != NULL);
+
+	/* Updating a cookie makes it recently used: it survives eviction. */
+	uhttp_jar_init(&jar);
+	CHECK(uhttp_jar_set(&jar, "h", "/", "session=a") == 1);
+	for (i = 0; i < UHTTP_COOKIE_MAX - 1; i++) {
+		char c[32];
+
+		snprintf(c, sizeof(c), "x%d=v", i);
+		CHECK(uhttp_jar_set(&jar, "h", "/", c) == 1);
+	}
+	CHECK(uhttp_jar_set(&jar, "h", "/", "session=b") == 1);
+	CHECK(uhttp_jar_set(&jar, "h", "/", "late=1") == 1);
+	CHECK(strstr(hdr("h", "/", 0), "session=b") != NULL);
+	CHECK(strstr(hdr("h", "/", 0), "x0=") == NULL);
 	uhttp_jar_clear(&jar);
 	CHECK(uhttp_jar_count(&jar, "h") == 0);
 }

Некоторые файлы не были показаны из-за большого количества измененных файлов