Skip to content

Commit eeb7792

Browse files
committed
WIP - inform login page about present cookies
1 parent 0e27614 commit eeb7792

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

pkg/static/login.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,11 @@ import "./login.scss";
343343
// end up on the login page, we are about to load resources
344344
// from two machines into the same browser origin.
345345

346-
const cur_machine = window.localStorage.getItem("current-machine");
347-
348-
// Protect against outdated cur_machine values.
349-
if (cur_machine == "localhost" && !window.location.pathname.startsWith("/="))
350-
return;
351-
if (cur_machine && cur_machine != "localhost" && window.location.pathname.startsWith("/=" + cur_machine))
352-
return;
346+
const logged_into = environment["logged-into"];
347+
const cur_machine = logged_into.length > 0 ? logged_into[0] : null;
353348

354349
function redirect_to_current_machine() {
355-
if (cur_machine == "localhost")
350+
if (cur_machine === ".")
356351
login_reload("/");
357352
else
358353
login_reload("/=" + cur_machine);
@@ -364,7 +359,8 @@ import "./login.scss";
364359
if (!environment.page.allow_multi_host)
365360
redirect_to_current_machine();
366361
else {
367-
id("multihost-message").textContent = format(_("You are already connected to '$0' in this browser session. Connecting to other hosts will allow them to execute arbitrary code on each other. Please be careful."), cur_machine);
362+
id("multihost-message").textContent = format(_("You are already connected to '$0' in this browser session. Connecting to other hosts will allow them to execute arbitrary code on each other. Please be careful."),
363+
cur_machine == "." ? "localhost" : cur_machine);
368364
id("multihost-get-me-there").addEventListener("click", redirect_to_current_machine);
369365
show('#multihost-warning');
370366
}
@@ -442,7 +438,6 @@ import "./login.scss";
442438
oauth_auto_login();
443439
}
444440
} else if (logout_intent) {
445-
window.localStorage.removeItem("current-machine");
446441
show_login(logout_reason);
447442
} else if (need_host()) {
448443
show_login();
@@ -1018,7 +1013,7 @@ import "./login.scss";
10181013
}
10191014
}
10201015

1021-
function setup_localstorage (response, machine) {
1016+
function setup_localstorage (response) {
10221017
/* Clear anything not prefixed with
10231018
* different application from sessionStorage
10241019
*/
@@ -1051,8 +1046,6 @@ import "./login.scss";
10511046
const ca_cert_url = environment.CACertUrl;
10521047
if (ca_cert_url)
10531048
window.sessionStorage.setItem('CACertUrl', ca_cert_url);
1054-
1055-
window.localStorage.setItem('current-machine', machine || "localhost");
10561049
}
10571050

10581051
function run(response) {
@@ -1079,7 +1072,7 @@ import "./login.scss";
10791072
*/
10801073
clear_storage(window.sessionStorage, application, false);
10811074

1082-
setup_localstorage(response, machine);
1075+
setup_localstorage(response);
10831076
login_reload(wanted);
10841077
}
10851078

src/ws/cockpitauth.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,3 +1706,13 @@ cockpit_auth_empty_cookie_value (const gchar *path, gboolean secure)
17061706

17071707
return cookie_line;
17081708
}
1709+
1710+
gchar *
1711+
cockpit_auth_cookie_name (const gchar *path)
1712+
{
1713+
gchar *application = cockpit_auth_parse_application (path, NULL);
1714+
gchar *cookie = application_cookie_name (application);
1715+
g_free (application);
1716+
1717+
return cookie;
1718+
}

src/ws/cockpitauth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ gchar * cockpit_auth_parse_application (const gchar *path,
108108
gchar * cockpit_auth_empty_cookie_value (const gchar *path,
109109
gboolean secure);
110110

111+
gchar * cockpit_auth_cookie_name (const gchar *path);
112+
111113
G_END_DECLS
112114

113115
#endif

src/ws/cockpithandlers.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,48 @@ add_page_to_environment (JsonObject *object,
277277
json_object_set_object_member (object, "page", page);
278278
}
279279

280+
static void
281+
add_logged_into_to_environment (JsonObject *object,
282+
const gchar *path,
283+
GHashTable *request_headers)
284+
{
285+
gchar *h = g_hash_table_lookup (request_headers, "Cookie");
286+
if (!h)
287+
return;
288+
289+
g_autofree gchar *self_cookie = cockpit_auth_cookie_name (path);
290+
291+
JsonArray *logged_into = json_array_new ();
292+
293+
while (*h) {
294+
const gchar *start = h;
295+
while (*h && *h != '=')
296+
h++;
297+
const gchar *equal = h;
298+
while (*h && *h != ';')
299+
h++;
300+
if (*h)
301+
h++;
302+
while (*h && *h == ' ')
303+
h++;
304+
305+
if (g_str_has_prefix (equal, "=deleted"))
306+
continue;
307+
308+
g_autofree gchar *name = g_strndup (start, equal - start);
309+
if (g_str_equal (name, self_cookie))
310+
;
311+
else if (g_str_equal (name, "cockpit"))
312+
json_array_add_string_element(logged_into, ".");
313+
else if (g_str_has_prefix (name, "machine-cockpit+"))
314+
json_array_add_string_element(logged_into, name + strlen("machine-cockpit+"));
315+
}
316+
317+
json_object_set_array_member (object, "logged-into", logged_into);
318+
}
319+
280320
static GBytes *
281-
build_environment (GHashTable *os_release)
321+
build_environment (GHashTable *os_release, const gchar *path, GHashTable *request_headers)
282322
{
283323
/*
284324
* We don't include entirety of os-release into the
@@ -310,6 +350,7 @@ build_environment (GHashTable *os_release)
310350
json_object_set_boolean_member (object, "is_cockpit_client", is_cockpit_client);
311351

312352
add_page_to_environment (object, is_cockpit_client);
353+
add_logged_into_to_environment (object, path, request_headers);
313354

314355
hostname = g_malloc0 (HOST_NAME_MAX + 1);
315356
gethostname (hostname, HOST_NAME_MAX);
@@ -386,7 +427,7 @@ send_login_html (CockpitWebResponse *response,
386427
GBytes *po_bytes;
387428
CockpitWebFilter *filter3 = NULL;
388429

389-
environment = build_environment (ws->os_release);
430+
environment = build_environment (ws->os_release, path, headers);
390431
filter = cockpit_web_inject_new (marker, environment, 1);
391432
g_bytes_unref (environment);
392433
cockpit_web_response_add_filter (response, filter);
@@ -455,6 +496,7 @@ send_login_html (CockpitWebResponse *response,
455496
"Content-Security-Policy", content_security_policy,
456497
"Set-Cookie", cookie_line,
457498
NULL);
499+
cockpit_web_response_set_cache_type (response, COCKPIT_WEB_RESPONSE_NO_CACHE);
458500
if (cockpit_web_response_queue (response, bytes))
459501
cockpit_web_response_complete (response);
460502

0 commit comments

Comments
 (0)