maincopy-server 0.1.0

Self-hosted publishing server with exact previews and explicit release approval
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! Public subscription controls. GET never changes consent. Tokens are accepted
//! only as purpose-bound bearer controls and are never echoed into page markup.
//! The sole writer orders consent, removal and sender admission; these handlers
//! never transmit mail or infer consent from provider state.

use std::sync::Arc;

use axum::{
    Form, Router,
    body::{Body, to_bytes},
    extract::{
        DefaultBodyLimit, FromRequest as _, Multipart, Path, Request, State,
        rejection::FormRejection,
    },
    http::{
        HeaderMap, HeaderValue, StatusCode,
        header::{
            CACHE_CONTROL, CONTENT_SECURITY_POLICY, CONTENT_TYPE, HOST, ORIGIN, REFERRER_POLICY,
            X_CONTENT_TYPE_OPTIONS, X_FRAME_OPTIONS,
        },
    },
    middleware::{self, Next},
    response::{Html, IntoResponse, Response},
    routing::get,
};
use maincopy_shared::auth_api::SecretString;
use markdown_compiler::PublicationBaseUrl;
use maud::{DOCTYPE, Markup, html};
use serde::Deserialize;
use thiserror::Error;
use time::OffsetDateTime;
use uuid::Uuid;
use zeroize::Zeroizing;

use super::{
    config::{SesMailConfiguration, SubscriptionMode, SubscriptionPolicy},
    control::{ControlClaims, ControlPurpose},
    controls::MailControls,
    identity::EmailAddress,
    message::UNSUBSCRIBE_ROUTE,
    ses::SesCredentials,
    subscriber::{
        ConfirmEnrollment, ControlOutcome, EnrollmentRequestResult, FeedbackHealth,
        ManageEnrollment, RequestEnrollment, SubscriberCommandError, SubscriberDigest,
        SubscriberMode,
        store::{SubscriberMutationError, SubscriberStore},
    },
};

const SUBSCRIBE_ROUTE: &str = "/email/subscribe";
const CONFIRM_ROUTE: &str = "/email/confirm/{token}";
const MAX_FORM_BYTES: usize = 4096;
const PRIVATE_CSP: &str = "default-src 'none'; script-src 'none'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'";

/// Constructed only after startup admits the real controls and subscriber store.
/// The writer independently checks the current policy before accepting signup.
#[derive(Clone)]
pub(crate) struct PublicMailState {
    policy: SubscriptionPolicy,
    origin: PublicationBaseUrl,
    configuration_binding: [u8; 32],
    controls: Arc<MailControls>,
    subscribers: SubscriberStore,
}

impl PublicMailState {
    pub(super) fn new(
        configuration: SesMailConfiguration,
        credentials: &SesCredentials,
        policy: SubscriptionPolicy,
        origin: PublicationBaseUrl,
        controls: Arc<MailControls>,
        subscribers: SubscriberStore,
    ) -> Self {
        Self {
            policy,
            origin,
            configuration_binding: configuration.provider_binding(credentials),
            controls,
            subscribers,
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SubscribeForm {
    address: SecretString,
    consent: OptIn,
}

#[derive(Deserialize)]
enum OptIn {
    #[serde(rename = "yes")]
    Requested,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ConfirmForm {
    action: ConfirmAction,
}

#[derive(Deserialize)]
enum ConfirmAction {
    #[serde(rename = "confirm")]
    Confirm,
}

#[derive(Deserialize)]
#[serde(untagged, deny_unknown_fields)]
enum RemovalForm {
    Browser {
        action: RemovalAction,
    },
    OneClick {
        #[serde(rename = "List-Unsubscribe")]
        request: OneClick,
    },
}

#[derive(Deserialize)]
enum RemovalAction {
    #[serde(rename = "remove")]
    Remove,
}

#[derive(Deserialize)]
enum OneClick {
    #[serde(rename = "One-Click")]
    Requested,
}

pub(crate) fn router(state: PublicMailState) -> Router {
    Router::new()
        .route(
            SUBSCRIBE_ROUTE,
            get(subscribe_page).post(request_subscription),
        )
        .route(CONFIRM_ROUTE, get(confirm_page).post(confirm_subscription))
        .route(
            UNSUBSCRIBE_ROUTE,
            get(removal_page).post(remove_subscription),
        )
        .route_layer(middleware::from_fn(bounded_body))
        .route_layer(middleware::from_fn_with_state(state.clone(), validate_host))
        .layer(DefaultBodyLimit::max(MAX_FORM_BYTES))
        .with_state(state)
        .layer(middleware::from_fn(private_response))
}

async fn validate_host(
    State(state): State<PublicMailState>,
    request: Request,
    next: Next,
) -> Response {
    let authority = &state.origin.as_url()[url::Position::BeforeHost..url::Position::AfterPort];
    if !has_exact_header(request.headers(), HOST, authority)
        || request
            .uri()
            .authority()
            .is_some_and(|value| value.as_str() != authority)
        || request
            .uri()
            .scheme_str()
            .is_some_and(|value| value != "https")
    {
        return PublicError::WrongHost.into_response();
    }
    // No recipient identity or control state is accepted in query parameters.
    if request.uri().query().is_some() {
        return PublicError::InvalidForm.into_response();
    }
    next.run(request).await
}

async fn bounded_body(request: Request, next: Next) -> Response {
    let (parts, body) = request.into_parts();
    match to_bytes(body, MAX_FORM_BYTES).await {
        Ok(bytes) => {
            next.run(Request::from_parts(parts, Body::from(bytes)))
                .await
        }
        Err(_) => PublicError::TooLarge.into_response(),
    }
}

async fn private_response(request: Request, next: Next) -> Response {
    let mut response = next.run(request).await;
    let headers = response.headers_mut();
    headers.insert(CACHE_CONTROL, HeaderValue::from_static("private, no-store"));
    headers.insert(REFERRER_POLICY, HeaderValue::from_static("no-referrer"));
    headers.insert(
        CONTENT_SECURITY_POLICY,
        HeaderValue::from_static(PRIVATE_CSP),
    );
    headers.insert(X_CONTENT_TYPE_OPTIONS, HeaderValue::from_static("nosniff"));
    headers.insert(X_FRAME_OPTIONS, HeaderValue::from_static("DENY"));
    headers.insert(
        "x-robots-tag",
        HeaderValue::from_static("noindex, nofollow, noarchive"),
    );
    response
}

fn has_exact_header(
    headers: &HeaderMap,
    name: axum::http::header::HeaderName,
    expected: &str,
) -> bool {
    let mut values = headers.get_all(name).iter();
    values
        .next()
        .is_some_and(|value| value.as_bytes() == expected.as_bytes())
        && values.next().is_none()
}

fn require_browser_origin(state: &PublicMailState, headers: &HeaderMap) -> Result<(), PublicError> {
    let expected = state.origin.as_url().origin().ascii_serialization();
    if has_exact_header(headers, ORIGIN, &expected) {
        Ok(())
    } else {
        Err(PublicError::WrongOrigin)
    }
}

async fn subscribe_page(State(state): State<PublicMailState>) -> Result<Response, PublicError> {
    let status = state
        .subscribers
        .status()
        .await
        .map_err(|_| PublicError::Unavailable)?;
    let enabled = state.policy.view().mode == SubscriptionMode::Enabled
        && status.feedback_health == FeedbackHealth::Healthy
        && status.policy.is_some_and(|policy| {
            policy.mode == SubscriberMode::Enabled
                && policy.configuration_binding == state.configuration_binding
        });
    Ok(page(
        StatusCode::OK,
        "Subscribe to article announcements",
        html! {
            (policy_notice(&state.policy))
            @if enabled {
                form method="post" {
                    p { label for="mail-address" { "Email address" } }
                    p { input id="mail-address" name="address" type="email" maxlength="254" autocomplete="email" required; }
                    p { label {
                        input name="consent" type="checkbox" value="yes" required;
                        " I want to receive these article announcements and the confirmation email."
                    } }
                    button type="submit" { "Request confirmation email" }
                }
            } @else {
                p { "New subscriptions are paused. Confirmation and removal links in existing messages still work." }
            }
        },
    ))
}

async fn request_subscription(
    State(state): State<PublicMailState>,
    headers: HeaderMap,
    form: Result<Form<SubscribeForm>, FormRejection>,
) -> Result<Response, PublicError> {
    require_browser_origin(&state, &headers)?;
    if matches!(state.policy.view().mode, SubscriptionMode::Paused) {
        return Err(PublicError::Paused);
    }
    let Form(SubscribeForm {
        address,
        consent: OptIn::Requested,
    }) = form.map_err(form_error)?;
    let address =
        EmailAddress::parse(address.expose_secret()).map_err(|_| PublicError::InvalidAddress)?;
    let mailbox_digest = SubscriberDigest::from_bytes(state.controls.mailbox_digest(&address));
    let outcome = state
        .subscribers
        .request_enrollment(RequestEnrollment {
            address,
            mailbox_digest,
            enrollment: Uuid::new_v4(),
            generation: Uuid::new_v4(),
            confirmation_attempt: Uuid::new_v4(),
            configuration_binding: state.configuration_binding,
        })
        .await
        .map_err(|error| match error {
            SubscriberMutationError::Command(SubscriberCommandError::Paused) => PublicError::Paused,
            SubscriberMutationError::Admission(_) | SubscriberMutationError::Command(_) => {
                PublicError::Unavailable
            }
        })?;
    match outcome {
        EnrollmentRequestResult::Queued | EnrollmentRequestResult::Unchanged => Ok(page(
            StatusCode::ACCEPTED,
            "Request received",
            html! {
                p { "If this address can subscribe, a confirmation email will be sent. Follow the link in that email to confirm your request." }
                (policy_notice(&state.policy))
            },
        )),
    }
}

async fn confirm_page(
    State(state): State<PublicMailState>,
    path: Result<Path<SecretString>, axum::extract::rejection::PathRejection>,
) -> Result<Response, PublicError> {
    verify_token(&state, path, ControlPurpose::Confirm)?;
    Ok(page(
        StatusCode::OK,
        "Confirm your subscription",
        html! {
            (policy_notice(&state.policy))
            p { "Opening this page does not subscribe you. Confirm below only if you requested these article announcements." }
            form method="post" {
                button type="submit" name="action" value="confirm" { "Confirm subscription" }
            }
        },
    ))
}

async fn confirm_subscription(
    State(state): State<PublicMailState>,
    headers: HeaderMap,
    path: Result<Path<SecretString>, axum::extract::rejection::PathRejection>,
    form: Result<Form<ConfirmForm>, FormRejection>,
) -> Result<Response, PublicError> {
    require_browser_origin(&state, &headers)?;
    let Form(ConfirmForm {
        action: ConfirmAction::Confirm,
    }) = form.map_err(form_error)?;
    let claims = verify_token(&state, path, ControlPurpose::Confirm)?;
    let ControlClaims::Confirm {
        enrollment,
        generation,
        confirmation_nonce,
        expires_at,
    } = claims
    else {
        return Err(PublicError::InvalidLink);
    };
    let nonce_digest =
        SubscriberDigest::from_bytes(MailControls::confirmation_digest(&confirmation_nonce));
    let outcome = state
        .subscribers
        .confirm(ConfirmEnrollment {
            enrollment,
            generation,
            nonce_digest,
            expires_at,
        })
        .await
        .map_err(|_| PublicError::Unavailable)?;
    match outcome {
        ControlOutcome::Changed | ControlOutcome::Unchanged => Ok(page(
            StatusCode::OK,
            "Confirmation processed",
            html! {
                p { "If this invitation is current, your subscription is now confirmed. An expired or replaced invitation cannot activate a subscription." }
                (policy_notice(&state.policy))
            },
        )),
    }
}

async fn removal_page(
    State(state): State<PublicMailState>,
    path: Result<Path<SecretString>, axum::extract::rejection::PathRejection>,
) -> Result<Response, PublicError> {
    verify_token(&state, path, ControlPurpose::Manage)?;
    Ok(page(
        StatusCode::OK,
        "Unsubscribe and remove your address",
        html! {
            p { "Opening this page does not change your subscription." }
            p { "Use the button to stop future mail for this signup and remove your address from the active mailing records. A newer signup is unaffected by an older removal link." }
            p { "Messages already being submitted may still arrive. Backup and provider copies follow the retention policy below." }
            form method="post" {
                button type="submit" name="action" value="remove" { "Unsubscribe and remove my address" }
            }
            (policy_notice(&state.policy))
        },
    ))
}

async fn remove_subscription(
    State(state): State<PublicMailState>,
    path: Result<Path<SecretString>, axum::extract::rejection::PathRejection>,
    request: Request,
) -> Result<Response, PublicError> {
    let browser_origin = require_browser_origin(&state, request.headers());
    let form = removal_form(request).await?;
    match form {
        RemovalForm::Browser {
            action: RemovalAction::Remove,
        } => browser_origin?,
        RemovalForm::OneClick {
            request: OneClick::Requested,
        } => {}
    }
    let claims = verify_token(&state, path, ControlPurpose::Manage)?;
    let ControlClaims::Manage {
        enrollment,
        generation,
    } = claims
    else {
        return Err(PublicError::InvalidLink);
    };
    let outcome = state
        .subscribers
        .remove(ManageEnrollment {
            enrollment,
            generation,
        })
        .await
        .map_err(|_| PublicError::Unavailable)?;
    match outcome {
        ControlOutcome::Changed | ControlOutcome::Unchanged => Ok(page(
            StatusCode::OK,
            "Removal processed",
            html! {
                p { "This link no longer authorizes future mail. A newer signup is unaffected. Messages already being submitted may still arrive." }
                p { "Backup and provider copies follow the retention policy." }
                (policy_notice(&state.policy))
            },
        )),
    }
}

async fn removal_form(request: Request) -> Result<RemovalForm, PublicError> {
    let mut content_types = request.headers().get_all(CONTENT_TYPE).iter();
    let content_type = content_types
        .next()
        .ok_or(PublicError::InvalidForm)?
        .to_str()
        .map_err(|_| PublicError::InvalidForm)?;
    if content_types.next().is_some() {
        return Err(PublicError::InvalidForm);
    }
    let media_type = content_type
        .split(';')
        .next()
        .ok_or(PublicError::InvalidForm)?
        .trim();
    if media_type.eq_ignore_ascii_case("multipart/form-data") {
        return multipart_one_click(request).await;
    }
    Form::<RemovalForm>::from_request(request, &())
        .await
        .map(|Form(form)| form)
        .map_err(form_error)
}

async fn multipart_one_click(request: Request) -> Result<RemovalForm, PublicError> {
    let mut multipart = Multipart::from_request(request, &())
        .await
        .map_err(|_| PublicError::InvalidForm)?;
    let field = multipart
        .next_field()
        .await
        .map_err(|_| PublicError::InvalidForm)?
        .ok_or(PublicError::InvalidForm)?;
    if field.name() != Some("List-Unsubscribe") || field.file_name().is_some() {
        return Err(PublicError::InvalidForm);
    }
    let value = Zeroizing::new(field.text().await.map_err(|_| PublicError::InvalidForm)?);
    if value.as_str() != "One-Click"
        || multipart
            .next_field()
            .await
            .map_err(|_| PublicError::InvalidForm)?
            .is_some()
    {
        return Err(PublicError::InvalidForm);
    }
    Ok(RemovalForm::OneClick {
        request: OneClick::Requested,
    })
}

fn verify_token(
    state: &PublicMailState,
    path: Result<Path<SecretString>, axum::extract::rejection::PathRejection>,
    purpose: ControlPurpose,
) -> Result<ControlClaims, PublicError> {
    let Path(token) = path.map_err(|_| PublicError::InvalidLink)?;
    state
        .controls
        .verify(purpose, token.expose_secret(), OffsetDateTime::now_utc())
        .map_err(|_| PublicError::InvalidLink)
}

fn policy_notice(policy: &SubscriptionPolicy) -> Markup {
    let view = policy.view();
    html! {
        p { "Operator: " (view.operator_name) }
        p { "Postal address: " (view.postal_address) }
        p { (view.purpose) }
        p { "Your address is stored by this site and processed by Amazon SES to deliver confirmation and announcement emails." }
        p { "Contact: " (view.contact_address.as_str()) }
        p { a href=(view.privacy_url.as_str()) rel="noreferrer" { "Privacy and retention policy" } }
    }
}

fn page(status: StatusCode, title: &str, content: Markup) -> Response {
    // Content includes only public policy and static text. Forms deliberately
    // omit action: the browser posts to the current token URL without copying
    // the bearer value into a response, hidden field or third-party resource.
    let document = html! {
        (DOCTYPE)
        html lang="en" {
            head { meta charset="utf-8"; meta name="viewport" content="width=device-width, initial-scale=1"; title { (title) } }
            body { main { h1 { (title) } (content) } }
        }
    };
    (status, Html(document.into_string())).into_response()
}

fn form_error(error: FormRejection) -> PublicError {
    if error.status() == StatusCode::PAYLOAD_TOO_LARGE {
        PublicError::TooLarge
    } else {
        PublicError::InvalidForm
    }
}

#[derive(Debug, Error)]
enum PublicError {
    #[error("the public request host does not match the configured HTTPS origin")]
    WrongHost,
    #[error("the browser request origin does not match the configured HTTPS origin")]
    WrongOrigin,
    #[error("the subscription form is invalid")]
    InvalidForm,
    #[error("the email address is unsupported")]
    InvalidAddress,
    #[error("the subscriber control link is invalid or expired")]
    InvalidLink,
    #[error("the subscription form exceeds the byte bound")]
    TooLarge,
    #[error("new subscription requests are paused")]
    Paused,
    #[error("the subscriber operation is unavailable or its outcome is not confirmed")]
    Unavailable,
}

impl IntoResponse for PublicError {
    fn into_response(self) -> Response {
        let (status, message) = match self {
            Self::WrongHost => (
                StatusCode::MISDIRECTED_REQUEST,
                "Use the site's configured public HTTPS address.",
            ),
            Self::WrongOrigin => (
                StatusCode::FORBIDDEN,
                "Submit this form from the site's own HTTPS page.",
            ),
            Self::InvalidForm => (
                StatusCode::BAD_REQUEST,
                "The form could not be accepted. Use the fields and action shown on this page.",
            ),
            Self::InvalidAddress => (StatusCode::BAD_REQUEST, "Enter a valid email address."),
            Self::InvalidLink => (
                StatusCode::BAD_REQUEST,
                "This link is invalid, expired, or intended for a different action. Use the link in the original email.",
            ),
            Self::TooLarge => (
                StatusCode::PAYLOAD_TOO_LARGE,
                "This form exceeds the request limit.",
            ),
            Self::Paused => (
                StatusCode::SERVICE_UNAVAILABLE,
                "New subscriptions are paused. Existing confirmation and removal links remain available.",
            ),
            Self::Unavailable => (
                StatusCode::SERVICE_UNAVAILABLE,
                "The request outcome could not be confirmed. Try the same form or original email link again before starting a new request.",
            ),
        };
        page(status, "Request not completed", html! { p { (message) } })
    }
}

#[cfg(test)]
#[path = "public/tests.rs"]
mod tests;