日本語

Who the server answers

The server listens on 127.0.0.1 and answers only the application's own windows.

Which requests are answered

App::run serves the application's router on 127.0.0.1, which no other machine can reach. Any program on the same machine can open a connection to that port, so the server checks every request before the router sees it, including a request for a path no route matches:

RequestAnswer
Host is not 127.0.0.1:<port>, as with localhost:<port> or another domain403 Forbidden
Host is right, without the cookie the first window received403 Forbidden
Host is right, with that cookiethe router's answer

The answer to the first window's first page sets a cookie, once in each run of the application. The web views of all the windows are made with one data store, which holds the cookies, so every window of the application sends it. The cookie is HttpOnly, so no script on a page reads it. A request from curl, from another application, or from a page in the person's browser carries no such cookie and gets this:

GET / HTTP/1.1
host: 127.0.0.1:52817

HTTP/1.1 403 Forbidden

The Host check stops a page of another site whose domain was made to resolve to 127.0.0.1: its requests name that domain in Host. Neither check is something the application configures, and neither can be turned off.

No window leaves the application

A window shows only pages of http://127.0.0.1:<port>/. A link to another site, or a window.open of one, opens it in the person's browser when it is an http, https, or mailto URL, and the window stays on its page. A URL of any other scheme, such as file:, is not opened at all. A page of the application asked for with target="_blank" or window.open opens in a new window of the application.

Session cookies without `Secure`

WKWebView, which shows the window on macOS, does not send a Secure cookie to http://127.0.0.1, and neither does Playwright's build of WebKit, the engine of WebKitGTK on Linux. A session layer that marks its cookie Secure, as tower-sessions does by default, signs no one in inside the window: the requests after signing in arrive without the session cookie, so the server sees no one signed in.

tower-sessions 0.14's SessionManagerLayer::with_secure(false) sets the cookie without Secure. Build the router in a function that takes the choice, and pass false in the desktop build and true in the web build:

use hypersig_desktop::App;
use tower_sessions::cookie::SameSite;
use tower_sessions::{MemoryStore, SessionManagerLayer};

/// The application, whose session cookie is marked `Secure` when `secure` is true.
fn app(secure: bool) -> Router {
    let sessions = SessionManagerLayer::new(MemoryStore::default())
        .with_same_site(SameSite::Lax)
        .with_secure(secure);
    pages(Router::new()).hypersig_runtime().layer(sessions)
}

const APP: App = App::new("dev.example.members");

// The desktop build. The web build serves `app(true)`.
fn main() -> Result<(), hypersig_desktop::Error> {
    APP.run(&HOME.url(()), |_os| async { app(false) })
}

Without Secure, the browser sends the cookie over plain HTTP. In the desktop build that HTTP stays on 127.0.0.1, and the checks above answer 403 to every program that is not one of the application's windows, so the session cookie reaches only the application's own server from its own windows. Nothing checks that the web build passes true; a web build that passes false sends its session cookie over plain HTTP wherever the site is served without HTTPS. The session cookie lists the cookie's other attributes.

Next

Signals kept between runs covers the port the server listens on. Opening windows opens the application's pages in new windows from a handler.