From a6f52ce384a54206564bee1ad5155c1fdd1ca361 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 21 Jun 2022 12:13:04 +0100 Subject: [PATCH 1/4] add cookie utils module for requests and responses --- peach-web/src/utils/cookie.rs | 64 +++++++++++++++++++++++++++++++++++ peach-web/src/utils/mod.rs | 1 + 2 files changed, 65 insertions(+) create mode 100644 peach-web/src/utils/cookie.rs diff --git a/peach-web/src/utils/cookie.rs b/peach-web/src/utils/cookie.rs new file mode 100644 index 0000000..4e59291 --- /dev/null +++ b/peach-web/src/utils/cookie.rs @@ -0,0 +1,64 @@ +use rouille::{input, Request, Response}; + +// The CookieRequest and CookieResponse traits are currently only used +// to add, retrieve and reset the `back_url` cookie. That cookie is +// used to set the URL of the in-UI back button when visiting a page +// which can be arrived at via several paths. +// +// An example of this is the Scuttlebutt Settings menu (/settings/scuttlebutt), +// which can be accessed via the Settings menu (/settings) or the Scuttlebutt +// Status page (/status/scuttlebutt). We need to be able to set the path of +// the back button to point to the correct page (ie. the one from which we've +// come). +// +// The `back_url` cookie is also used on the Profile page +// (/scuttlebutt/profile). + +/// Cookie trait for `Request`. +pub trait CookieRequest { + /// Retrieve a cookie value from a `Request`. + fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str>; +} + +impl CookieRequest for Request { + fn retrieve_cookie(&self, cookie_name: &str) -> Option<&str> { + // check for cookie using given name + let cookie_val = input::cookies(self) + .find(|&(n, _)| n == cookie_name) + // return the value of the cookie (key is already known) + .map(|key_val| key_val.1); + + cookie_val + } +} + +/// Cookie trait for `Response`. +pub trait CookieResponse { + /// Add a cookie containing the given data to a `Response`. Data should be + /// in the form of `cookie_name=cookie_val`. + fn add_cookie(self, cookie_name_val: &str) -> Response; + /// Reset a cookie value for a `Response`. + fn reset_cookie(self, cookie_name: &str) -> Response; +} + +impl CookieResponse for Response { + fn add_cookie(self, cookie_name_val: &str) -> Response { + // set the cookie header + // max-age is currently set to 3600 seconds (1 hour) + self.with_additional_header( + "Set-Cookie", + format!("{}; Max-Age=3600; SameSite=Lax; Path=/", cookie_name_val), + ) + } + + fn reset_cookie(self, cookie_name: &str) -> Response { + // set a blank cookie to clear the cookie from the previous request + self.with_additional_header( + "Set-Cookie", + format!( + "{}=; Max-Age=0; SameSite=Lax; Path=/; Expires=Fri, 21 Aug 1987 12:00:00 UTC", + cookie_name + ), + ) + } +} diff --git a/peach-web/src/utils/mod.rs b/peach-web/src/utils/mod.rs index e02227e..20b0a55 100644 --- a/peach-web/src/utils/mod.rs +++ b/peach-web/src/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod cookie; pub mod flash; pub mod sbot; pub mod theme; -- 2.49.0 From 5bd8a68ddfef21a5a596a6ce14bb5bf4f9278c26 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 21 Jun 2022 12:13:36 +0100 Subject: [PATCH 2/4] set, retrieve and reset back_url cookies --- peach-web/src/private_router.rs | 22 +++++++++++++++++-- peach-web/src/routes/scuttlebutt/profile.rs | 12 ++++++++-- .../routes/settings/scuttlebutt/configure.rs | 7 ++++-- .../src/routes/settings/scuttlebutt/menu.rs | 9 +++++--- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/peach-web/src/private_router.rs b/peach-web/src/private_router.rs index e48f337..5bc7665 100644 --- a/peach-web/src/private_router.rs +++ b/peach-web/src/private_router.rs @@ -1,6 +1,10 @@ use rouille::{router, Request, Response}; -use crate::{routes, templates, utils::flash::FlashResponse, SessionData}; +use crate::{ + routes, templates, + utils::{cookie::CookieResponse, flash::FlashResponse}, + SessionData, +}; // TODO: add mount_peachcloud_routes() // https://github.com/tomaka/rouille/issues/232#issuecomment-919225104 @@ -22,6 +26,8 @@ pub fn mount_peachpub_routes( router!(request, (GET) (/) => { Response::html(routes::home::build_template()) + // reset the back_url cookie each time we visit the homepage + .reset_cookie("back_url") }, (GET) (/auth/change) => { @@ -49,6 +55,9 @@ pub fn mount_peachpub_routes( (GET) (/scuttlebutt/blocks) => { Response::html(routes::scuttlebutt::blocks::build_template()) + // add a back_url cookie to allow the path of the back button + // to be set correctly on the /scuttlebutt/profile page + .add_cookie("back_url=/scuttlebutt/blocks") }, (POST) (/scuttlebutt/follow) => { @@ -57,10 +66,16 @@ pub fn mount_peachpub_routes( (GET) (/scuttlebutt/follows) => { Response::html(routes::scuttlebutt::follows::build_template()) + // add a back_url cookie to allow the path of the back button + // to be set correctly on the /scuttlebutt/profile page + .add_cookie("back_url=/scuttlebutt/follows") }, (GET) (/scuttlebutt/friends) => { Response::html(routes::scuttlebutt::friends::build_template()) + // add a back_url cookie to allow the path of the back button + // to be set correctly on the /scuttlebutt/profile page + .add_cookie("back_url=/scuttlebutt/friends") }, (GET) (/scuttlebutt/invites) => { @@ -117,6 +132,9 @@ pub fn mount_peachpub_routes( (POST) (/scuttlebutt/search) => { routes::scuttlebutt::search::handle_form(request) + // add a back_url cookie to allow the path of the back button + // to be set correctly on the /scuttlebutt/profile page + .add_cookie("back_url=/scuttlebutt/search") }, (POST) (/scuttlebutt/unblock) => { @@ -187,7 +205,7 @@ pub fn mount_peachpub_routes( }, (GET) (/status/scuttlebutt) => { - Response::html(routes::status::scuttlebutt::build_template()) + Response::html(routes::status::scuttlebutt::build_template()).add_cookie("back_url=/status/scuttlebutt") }, // render the not_found template and set a 404 status code if none of diff --git a/peach-web/src/routes/scuttlebutt/profile.rs b/peach-web/src/routes/scuttlebutt/profile.rs index 43875ac..007aeb8 100644 --- a/peach-web/src/routes/scuttlebutt/profile.rs +++ b/peach-web/src/routes/scuttlebutt/profile.rs @@ -4,7 +4,7 @@ use rouille::Request; use crate::{ templates, - utils::{flash::FlashRequest, sbot, sbot::Profile, theme}, + utils::{cookie::CookieRequest, flash::FlashRequest, sbot, sbot::Profile, theme}, }; // ROUTE: /scuttlebutt/profile @@ -174,7 +174,15 @@ pub fn build_template(request: &Request, ssb_id: Option) -> PreEscaped templates::inactive::build_template("Profile is unavailable."), }; - let body = templates::nav::build_template(profile_template, "Profile", Some("/")); + // a request to /scuttlebutt/profile can originate via the Friends, + // Follows or Blocks menu - as well as the Search page and Homepage. + // therefore, we check to see if the `back_url` cookie has been set + // and assign the path of the back button accordingly. + // for example, if the request has come via the Friends menu then the + // `back_url` cookie will be set with a value of "/scuttlebutt/friends". + let back_url = request.retrieve_cookie("back_url").or(Some("/")); + + let body = templates::nav::build_template(profile_template, "Profile", back_url); // query the current theme so we can pass it into the base template builder let theme = theme::get_theme(); diff --git a/peach-web/src/routes/settings/scuttlebutt/configure.rs b/peach-web/src/routes/settings/scuttlebutt/configure.rs index dcba7b4..d2a54cf 100644 --- a/peach-web/src/routes/settings/scuttlebutt/configure.rs +++ b/peach-web/src/routes/settings/scuttlebutt/configure.rs @@ -183,8 +183,11 @@ pub fn build_template(request: &Request) -> PreEscaped { // wrap the nav bars around the settings menu template content // parameters are template, title and back url - let body = - templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings")); + let body = templates::nav::build_template( + menu_template, + "Scuttlebutt Settings", + Some("/settings/scuttlebutt"), + ); // query the current theme so we can pass it into the base template builder let theme = theme::get_theme(); diff --git a/peach-web/src/routes/settings/scuttlebutt/menu.rs b/peach-web/src/routes/settings/scuttlebutt/menu.rs index 66997fd..4360898 100644 --- a/peach-web/src/routes/settings/scuttlebutt/menu.rs +++ b/peach-web/src/routes/settings/scuttlebutt/menu.rs @@ -4,7 +4,7 @@ use rouille::Request; use crate::{ templates, - utils::{flash::FlashRequest, theme}, + utils::{cookie::CookieRequest, flash::FlashRequest, theme}, }; /// Read the status of the go-sbot service and render buttons accordingly. @@ -53,10 +53,13 @@ pub fn build_template(request: &Request) -> PreEscaped { } }; + // retrieve the value of the "back_url" cookie + // if the cookie value is not found then set a hardcoded fallback value + let back_url = request.retrieve_cookie("back_url").or(Some("/settings")); + // wrap the nav bars around the settings menu template content // parameters are template, title and back url - let body = - templates::nav::build_template(menu_template, "Scuttlebutt Settings", Some("/settings")); + let body = templates::nav::build_template(menu_template, "Scuttlebutt Settings", back_url); // query the current theme so we can pass it into the base template builder let theme = theme::get_theme(); -- 2.49.0 From a60d892e953c8bd1dff901708b36530c9e879e14 Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 21 Jun 2022 12:14:14 +0100 Subject: [PATCH 3/4] bump the patch version --- peach-web/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/peach-web/Cargo.toml b/peach-web/Cargo.toml index 7afc5f7..2ad1068 100644 --- a/peach-web/Cargo.toml +++ b/peach-web/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "peach-web" -version = "0.6.16" +version = "0.6.17" authors = ["Andrew Reid ", "Max Fowler "] edition = "2018" description = "peach-web is a web application which provides a web interface for monitoring and interacting with the PeachCloud device. This allows administration of the single-board computer (ie. Raspberry Pi) running PeachCloud, as well as the ssb-server and related plugins." -- 2.49.0 From 65b5f95a900f60aeb13fe4506fa5ff300cdb401d Mon Sep 17 00:00:00 2001 From: glyph Date: Tue, 21 Jun 2022 12:14:30 +0100 Subject: [PATCH 4/4] update lockfile --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 505c83a..21807a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2331,7 +2331,7 @@ dependencies = [ [[package]] name = "peach-web" -version = "0.6.15" +version = "0.6.17" dependencies = [ "async-std", "base64 0.13.0", -- 2.49.0