next-rust-server 0.1.11

The Next Rust runtime: HTTP server, request pipeline, SSR/streaming, ISR, middleware, static files and server actions.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Server actions: `#[server_action]` functions exposed at
//! `POST /_next-rust/action/<token>`.
//!
//! Security (in the order the checks run):
//!
//! * Only `POST` is accepted.
//! * Cross-site requests are rejected using `Sec-Fetch-Site` / `Origin`
//!   compared to the request host (`[security] csrf = "origin"`, default).
//! * There is no fixed URL per action. Every page view gets its own signed,
//!   expiring token bound to the visitor's `HttpOnly`, `SameSite=Strict`
//!   binding cookie (see `action_token.rs`). Unknown, expired, tampered
//!   or copied tokens are rejected before any action code runs.
//! * Only `application/json` and `application/x-www-form-urlencoded` bodies
//!   are accepted, so `text/plain` cross-site form tricks can't reach the
//!   JSON parser.
//! * With `csrf = "token"`, a double-submit token (`x-csrf-token` header or
//!   `_csrf` field matching the `next_rust_csrf` cookie) is also required.
//! * Errors are reduced to public messages in production, and responses are
//!   never cached.

use std::collections::BTreeMap;
use std::future::Future;

use http::HeaderMap;
use next_rust_core::config::CsrfMode;
use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::Cookie;
use crate::action_token;
use crate::app::AppInner;
use crate::cookies::Cookies;
use crate::error::{Error, ErrorKind, Result};
use crate::request::Request;
use crate::response::Response;

pub const FLASH_COOKIE: &str = "next_rust_flash";

/// Shown when an action link is expired, copied from another browser or
/// minted by a server with a different secret.
const STALE_MESSAGE: &str = "This page is out of date. Reload it and try again.";

/// Request data available to actions declared as `fn(ctx: ActionContext, input: T)`.
#[derive(Clone)]
pub struct ActionContext {
    pub cookies: Cookies,
    pub headers: HeaderMap,
    pub extensions: http::Extensions,
}

/// The client address, carried in [`ActionContext::extensions`] so the
/// struct's public fields stay as they are.
#[derive(Clone, Copy)]
struct ClientAddr(Option<std::net::IpAddr>);

impl ActionContext {
    pub fn extension<T: Send + Sync + 'static>(&self) -> Option<&T> {
        self.extensions.get::<T>()
    }

    /// The client's IP address, read as [`Request::client_ip`] reads it
    /// (`X-Forwarded-For` only with `[server] trust_proxy`). For per-client
    /// limits on an action: sign-ups, contact forms, password attempts.
    pub fn client_ip(&self) -> Option<std::net::IpAddr> {
        self.extensions.get::<ClientAddr>().and_then(|c| c.0)
    }
}

pub(crate) async fn handle(inner: &AppInner, mut req: Request) -> Response {
    if req.method() != http::Method::POST {
        return Response::text("Method Not Allowed").with_status(405).with_header("allow", "POST");
    }
    let security = &inner.config.security;
    if security.csrf != CsrfMode::Off && !same_origin(&req, &security.allowed_origins) {
        crate::log::warn("server action rejected: cross-origin request");
        return Response::text("Forbidden: cross-origin server action").with_status(403);
    }
    let token = req.path().trim_start_matches("/_next-rust/action/");
    let binding = action_token::binding(req.cookies());
    let found = action_token::verify(token, binding.as_deref())
        .and_then(|key| inner.actions.get(&key).copied().ok_or(action_token::Rejection::Forged));
    let index = match found {
        Ok(index) => index,
        Err(reason) => {
            match reason {
                action_token::Rejection::Expired => crate::log::debug("server action rejected: expired token"),
                other => crate::log::warn(&format!("server action rejected: {}", other.as_str())),
            }
            return stale(&req);
        }
    };
    if let Some(res) = check_content_type(&req) {
        return res;
    }
    if security.csrf == CsrfMode::Token && !double_submit_ok(&mut req).await {
        crate::log::warn("server action rejected: missing or invalid CSRF token");
        return Response::text("Invalid CSRF token").with_status(403);
    }
    let mut res = (inner.routes.actions[index].handler)(req).await;
    res.set_header("cache-control", "no-store");
    res
}

/// Mint a URL for action `id` bound to `binding` (used by [`crate::TestClient`]).
pub(crate) fn signed_url(id: &str, binding: &str, ttl: u64) -> String {
    format!("/_next-rust/action/{}", action_token::mint(&action_token::action_key(id), binding, ttl))
}

fn stale(req: &Request) -> Response {
    let res = if wants_json(req) {
        Response::json(&serde_json::json!({ "ok": false, "error": STALE_MESSAGE, "code": "next_rust_stale" }))
            .with_status(403)
    } else {
        // Plain form post: show the message on the page it came from.
        flash_back(req, &[], BTreeMap::new(), Some(STALE_MESSAGE.to_owned()))
    };
    res.with_header("cache-control", "no-store")
}

fn check_content_type(req: &Request) -> Option<Response> {
    let ct = req.header("content-type")?;
    let base = ct.split(';').next().unwrap_or_default().trim().to_ascii_lowercase();
    match base.as_str() {
        "application/json" | "application/x-www-form-urlencoded" => None,
        b if b.starts_with("multipart/") => Some(
            Response::text("multipart bodies are not supported by server actions; use an API route").with_status(415),
        ),
        _ => Some(Response::text("Unsupported Media Type").with_status(415)),
    }
}

async fn double_submit_ok(req: &mut Request) -> bool {
    let Some(cookie) = req.cookies().get(crate::CSRF_COOKIE) else { return false };
    let provided = match req.header("x-csrf-token") {
        Some(v) => Some(v.to_owned()),
        None if is_form(req) => crate::middleware::form_field(req, "_csrf").await,
        None => None,
    };
    provided.is_some_and(|p| crate::constant_time_eq(cookie.as_bytes(), p.as_bytes()))
}

/// `true` unless a browser signals a cross-site request.
pub(crate) fn same_origin(req: &Request, allowed: &[String]) -> bool {
    let fetch_site = req.header("sec-fetch-site");
    if matches!(fetch_site, Some("same-origin") | Some("none")) {
        return true;
    }
    let Some(origin) = req.header("origin") else {
        // Non-browser clients send neither header and cannot carry ambient cookies cross-site.
        return fetch_site.is_none();
    };
    if allowed.iter().any(|a| a == origin) {
        return true;
    }
    let host = if crate::trust_proxy() {
        req.header("x-forwarded-host").or_else(|| req.header("host"))
    } else {
        req.header("host")
    };
    let origin_host = origin.split_once("://").map(|(_, h)| h);
    matches!((origin_host, host), (Some(o), Some(h)) if o.eq_ignore_ascii_case(h))
}

fn is_form(req: &Request) -> bool {
    req.header("content-type").is_some_and(|c| c.starts_with("application/x-www-form-urlencoded"))
}

fn wants_json(req: &Request) -> bool {
    !is_form(req)
        || req.header("x-nr-action").is_some()
        || req.header("accept").is_some_and(|a| a.starts_with("application/json"))
}

async fn read_input<I: DeserializeOwned>(req: &mut Request) -> Result<(I, Vec<(String, String)>)> {
    if req.header("content-type").is_some_and(|c| c.starts_with("multipart/")) {
        return Err(Error::http(415, "multipart bodies are not supported by server actions; use an API route"));
    }
    let bytes = req.bytes().await?;
    if is_form(req) {
        let fields: Vec<(String, String)> = serde_urlencoded::from_bytes(&bytes).unwrap_or_default();
        let input =
            serde_urlencoded::from_bytes(&bytes).map_err(|e| Error::http(400, format!("invalid form data: {e}")))?;
        Ok((input, fields))
    } else {
        let slice: &[u8] = if bytes.iter().all(u8::is_ascii_whitespace) { b"null" } else { &bytes };
        let input = serde_json::from_slice(slice).map_err(|e| Error::http(400, format!("invalid JSON input: {e}")))?;
        Ok((input, Vec::new()))
    }
}

fn local_path(candidate: &str) -> Option<String> {
    (candidate.starts_with('/') && !candidate.starts_with("//") && !candidate.contains('\\'))
        .then(|| candidate.to_owned())
}

/// The `_redirect` field, when it is a local path.
fn redirect_field(fields: &[(String, String)]) -> Option<String> {
    fields.iter().find(|(k, _)| k == "_redirect").and_then(|(_, v)| local_path(v))
}

/// The page the form was posted from, when the browser says and it is on
/// this site.
fn referring_page(req: &Request) -> Option<String> {
    let host = req.header("host").unwrap_or_default();
    req.header("referer")
        .and_then(|r| r.split_once("://").map(|(_, rest)| rest.to_owned()))
        .and_then(|rest| rest.strip_prefix(host).map(str::to_owned))
        .and_then(|p| local_path(&p))
}

/// After a successful plain-form post: `_redirect`, else the form's page.
fn success_location(req: &Request, fields: &[(String, String)]) -> String {
    redirect_field(fields).or_else(|| referring_page(req)).unwrap_or_else(|| "/".into())
}

/// After a rejected plain-form post: back to the form's page, where the
/// flash is shown. `_redirect` is where the form goes on success, so it is
/// only used when the browser sent no usable referrer.
fn error_location(req: &Request, fields: &[(String, String)]) -> String {
    referring_page(req).or_else(|| redirect_field(fields)).unwrap_or_else(|| "/".into())
}

fn respond<O: Serialize>(req: &Request, json: bool, fields: &[(String, String)], result: Result<O>) -> Response {
    match (json, result) {
        (true, Ok(data)) => Response::json(&serde_json::json!({ "ok": true, "data": data })),
        (false, Ok(_)) => Response::see_other(&success_location(req, fields)),
        (json, Err(e)) => {
            let status = e.status();
            match e.into_kind() {
                ErrorKind::Redirect { location, .. } => {
                    if json {
                        Response::json(&serde_json::json!({ "ok": false, "redirect": location }))
                    } else {
                        Response::see_other(&location)
                    }
                }
                ErrorKind::Validation(errors) => {
                    if json {
                        Response::json(&serde_json::json!({ "ok": false, "errors": errors })).with_status(422)
                    } else {
                        flash_back(req, fields, errors, None)
                    }
                }
                kind => {
                    let err = Error::new(kind);
                    if status.is_server_error() {
                        crate::log::error(&format!("server action failed: {}", err.detailed_message()));
                    }
                    let message = if crate::is_dev() { err.detailed_message() } else { err.public_message() };
                    if json {
                        Response::json(&serde_json::json!({ "ok": false, "error": message }))
                            .with_status(status.as_u16())
                    } else {
                        flash_back(req, fields, BTreeMap::new(), Some(message))
                    }
                }
            }
        }
    }
}

fn flash_back(
    req: &Request,
    fields: &[(String, String)],
    errors: BTreeMap<String, String>,
    message: Option<String>,
) -> Response {
    let values: BTreeMap<String, String> = fields
        .iter()
        .filter(|(k, _)| {
            !k.starts_with('_')
                && !k.to_ascii_lowercase().contains("password")
                && !k.to_ascii_lowercase().contains("token")
        })
        .map(|(k, v)| (k.clone(), v.chars().take(256).collect()))
        .collect();
    let json = serde_json::json!({ "errors": errors, "message": message, "values": values }).to_string();
    let json: String = json.chars().take(3000).collect();
    req.cookies().set(Cookie::encoded(FLASH_COOKIE, &json).max_age(std::time::Duration::from_secs(60)));
    Response::see_other(&error_location(req, fields))
}

pub(crate) fn decode_flash(raw: &str) -> crate::context::FormState {
    #[derive(serde::Deserialize, Default)]
    struct Flash {
        #[serde(default)]
        errors: BTreeMap<String, String>,
        message: Option<String>,
        #[serde(default)]
        values: BTreeMap<String, String>,
    }
    let f: Flash = serde_json::from_str(raw).unwrap_or_default();
    crate::context::FormState { errors: f.errors, message: f.message, values: f.values }
}

/// Adapter for `async fn action(input: I) -> Result<O>` (used by `#[server_action]`).
pub async fn run_action<I, O, F, Fut>(mut req: Request, f: F) -> Response
where
    I: DeserializeOwned,
    O: Serialize,
    F: FnOnce(I) -> Fut,
    Fut: Future<Output = Result<O>>,
{
    let json = wants_json(&req);
    match read_input::<I>(&mut req).await {
        Ok((input, fields)) => {
            let result = f(input).await;
            respond(&req, json, &fields, result)
        }
        Err(e) => respond::<()>(&req, json, &[], Err(e)),
    }
}

/// Adapter for `async fn action(ctx: ActionContext, input: I) -> Result<O>`.
pub async fn run_action_ctx<I, O, F, Fut>(mut req: Request, f: F) -> Response
where
    I: DeserializeOwned,
    O: Serialize,
    F: FnOnce(ActionContext, I) -> Fut,
    Fut: Future<Output = Result<O>>,
{
    let json = wants_json(&req);
    let mut ctx = ActionContext {
        cookies: req.cookies().clone(),
        headers: req.headers().clone(),
        extensions: req.extensions().clone(),
    };
    ctx.extensions.insert(ClientAddr(req.client_ip()));
    match read_input::<I>(&mut req).await {
        Ok((input, fields)) => {
            let result = f(ctx, input).await;
            respond(&req, json, &fields, result)
        }
        Err(e) => respond::<()>(&req, json, &[], Err(e)),
    }
}

/// Adapter for `async fn action(ctx: ActionContext) -> Result<O>`: the request
/// context and no input (a submitted form's fields are only echoed back into
/// the flash, as for an action without arguments).
pub async fn run_action_ctx0<O, F, Fut>(mut req: Request, f: F) -> Response
where
    O: Serialize,
    F: FnOnce(ActionContext) -> Fut,
    Fut: Future<Output = Result<O>>,
{
    let json = wants_json(&req);
    let mut ctx = ActionContext {
        cookies: req.cookies().clone(),
        headers: req.headers().clone(),
        extensions: req.extensions().clone(),
    };
    ctx.extensions.insert(ClientAddr(req.client_ip()));
    let fields: Vec<(String, String)> = if is_form(&req) {
        req.bytes().await.ok().and_then(|b| serde_urlencoded::from_bytes(&b).ok()).unwrap_or_default()
    } else {
        Vec::new()
    };
    let result = f(ctx).await;
    respond(&req, json, &fields, result)
}

/// Adapter for `async fn action() -> Result<O>`.
pub async fn run_action0<O, F, Fut>(mut req: Request, f: F) -> Response
where
    O: Serialize,
    F: FnOnce() -> Fut,
    Fut: Future<Output = Result<O>>,
{
    let json = wants_json(&req);
    let fields: Vec<(String, String)> = if is_form(&req) {
        req.bytes().await.ok().and_then(|b| serde_urlencoded::from_bytes(&b).ok()).unwrap_or_default()
    } else {
        Vec::new()
    };
    let result = f().await;
    respond(&req, json, &fields, result)
}

/// Reference to a server action, created with `action!(path::to::fn)`.
///
/// As a part of `form![..]` it sets `method="post"` and the action URL, so
/// the form works without JavaScript and is enhanced when the client runtime
/// is loaded. The URL is different for every visitor and page view.
#[derive(Debug, Clone, Copy)]
pub struct ActionRef {
    pub id: &'static str,
}

impl ActionRef {
    pub const fn new(id: &'static str) -> Self {
        ActionRef { id }
    }

    /// Placeholder URL for rendered HTML. Every HTML response replaces it
    /// with a URL signed for the visitor, so it only works inside a page.
    pub fn url(&self) -> String {
        action_token::marker_url(self.id)
    }
}

/// `on_press = action!(save)` on UI components.
impl From<ActionRef> for next_rust_ui::Press {
    fn from(action: ActionRef) -> Self {
        next_rust_ui::Press::action(action.url())
    }
}

impl next_rust_view::Part for ActionRef {
    fn apply(self, el: &mut next_rust_view::Element) {
        el.set_attr(next_rust_view::attrs::action(self.url()));
        el.set_attr(next_rust_view::attrs::method("post"));
        el.set_attr(next_rust_view::attrs::data("nr-action", self.url()));
    }
}