zeyn 0.0.7

Rust web framework for data heavy applications, written by AI agents and reviewed by people
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
//! Content from other sites: `Embed` frames, the `#[rule(embed)]` field rule, `Image`,
//! `ExternalLink`, and the content security policy they read.

use axum::body::Body;
use axum::http::{Request, header};
use http_body_util::BodyExt;
use std::sync::Arc;
use tower::ServiceExt;
use zeyn::prelude::*;
use zeyn::ui::{EMBED_SANDBOX, Embed, EmbedPermission, ExternalLink, Image};
use zeyn::{Component, Csp, Identity, Render, Rule, Services, embed_url};

fn ctx(csp: Csp) -> Ctx {
    Ctx::for_tests(Identity::anonymous(), Services::default()).with_csp(Arc::new(csp))
}

fn frames() -> Csp {
    Csp::new()
        .frame_src(["https://www.youtube-nocookie.com", "https://*.vimeo.com"])
        .img_src(["https://i.example.com"])
}

fn html(c: &impl Component, ctx: &Ctx) -> String {
    let mut out = String::new();
    c.render(&mut out, &Render::english("s").with_ctx(ctx));
    out
}

#[test]
fn embed_on_an_allowed_host_is_a_locked_lazy_frame() {
    let e = Embed::url("https://www.youtube-nocookie.com/embed/abc?utm_source=x&t=4")
        .title("Product tour")
        .allow([
            EmbedPermission::Fullscreen,
            EmbedPermission::PictureInPicture,
        ])
        .aspect(4, 3);
    let out = html(&e, &ctx(frames()));
    assert!(
        out.contains(
            r#"<iframe src="https://www.youtube-nocookie.com/embed/abc?t=4" title="Product tour""#
        ),
        "{out}"
    );
    assert!(out.contains(&format!(r#"sandbox="{EMBED_SANDBOX}""#)));
    assert_eq!(
        EMBED_SANDBOX,
        "allow-scripts allow-same-origin allow-presentation"
    );
    assert!(out.contains(r#"allow="fullscreen; picture-in-picture""#));
    assert!(out.contains(r#"referrerpolicy="no-referrer" loading="lazy""#));
    assert!(out.contains("--z-aspect: 4 / 3"));
    assert!(
        !out.contains("allow-popups")
            && !out.contains("allow-top-navigation")
            && !out.contains("allow-forms")
    );
    assert!(
        html(
            &Embed::url("https://player.vimeo.com/video/1").title("V"),
            &ctx(frames())
        )
        .contains("<iframe")
    );
}

#[test]
fn embed_on_an_unlisted_host_renders_no_frame() {
    for url in [
        "https://evil.example.com/embed/abc",
        "https://vimeo.com/1",
        "http://www.youtube-nocookie.com/embed/abc",
        "javascript:alert(1)",
        "data:text/html,<script>alert(1)</script>",
        "<script>alert(1)</script>",
        "https://www.youtube-nocookie.com@evil.example.com/",
    ] {
        let out = html(&Embed::url(url).title("Tour"), &ctx(frames()));
        assert!(
            !out.contains("<iframe") && !out.contains("<template"),
            "{url}: {out}"
        );
        assert!(
            out.contains("z-callout z-tone-warning") && out.contains("Tour cannot be shown here"),
            "{url}: {out}"
        );
        assert!(
            !out.contains("evil") && !out.contains("<script"),
            "{url}: {out}"
        );
    }
    let outside_a_request = html(
        &Embed::url("https://www.youtube-nocookie.com/embed/abc").title("T"),
        &Ctx::for_tests(Identity::anonymous(), Services::default()),
    );
    assert!(
        !outside_a_request.contains("<iframe"),
        "no policy means no hosts"
    );
}

#[test]
fn click_to_load_shows_a_poster_button_and_keeps_the_frame_inert() {
    let e = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("Tour")
        .click_to_load("https://i.example.com/p.jpg");
    let out = html(&e, &ctx(frames()));
    let button = out
        .find(r#"<button type="button" class="z-embed-load" data-zeyn-embed-load>"#)
        .expect(&out);
    let template = out
        .find("<template data-zeyn-embed><iframe")
        .expect(&out);
    assert!(button < template);
    assert!(out.contains(r#"<img src="https://i.example.com/p.jpg" alt="""#));
    assert!(
        out.contains("<strong>Load Tour</strong>")
            && out.contains("Content from www.youtube-nocookie.com")
    );
    let unlisted_poster = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("Tour")
        .click_to_load("https://other.test/p.jpg");
    assert!(
        !html(&unlisted_poster, &ctx(frames())).contains("other.test"),
        "a poster from an unlisted host is left out"
    );
}

#[test]
fn a_notice_on_the_gate_is_escaped_translated_and_part_of_the_button_name() {
    let e = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("Product tour")
        .click_to_load("")
        .notice("Sound <b>on</b> & autoplay");
    let out = html(&e, &ctx(frames()));
    let button = &out[out.find("<button").expect(&out)..out.find("</button>").expect(&out)];
    assert!(
        button.contains(r#"<span class="z-embed-notice">Sound &lt;b&gt;on&lt;/b&gt; &amp; autoplay</span><strong>Load Product tour</strong>"#),
        "{out}"
    );
    assert!(!out.contains("<b>"), "{out}");
    assert_eq!(e.schema()["notice"], "Sound <b>on</b> & autoplay");

    let without_poster = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("T")
        .notice("Warning");
    let out = html(&without_poster, &ctx(frames()));
    assert!(
        out.contains("data-zeyn-embed-load") && out.contains("<template data-zeyn-embed><iframe"),
        "{out}"
    );
    assert!(!out.contains("<img"), "{out}");

    let tr = zeyn::I18n::builtin();
    let translated = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("Ürün turu")
        .notice(Text::key("zeyn.ui.link.external"));
    let mut out = String::new();
    translated.render(
        &mut out,
        &Render::new("s", "tr", tr).with_ctx(&ctx(frames())),
    );
    assert!(
        out.contains(r#"<span class="z-embed-notice">dış site</span>"#),
        "{out}"
    );
}

#[test]
fn a_remembered_gate_names_its_scope_and_keeps_the_frame_inert() {
    let e = Embed::url("https://www.youtube-nocookie.com/embed/abc")
        .title("Product tour")
        .notice("Served by another site")
        .remember("external_video-2", DAY);
    let out = html(&e, &ctx(frames()));
    assert!(out.starts_with(r#"<div class="z-embed" style="--z-aspect: 16 / 9" data-zeyn-embed-remember="external_video-2" data-zeyn-embed-for="86400000""#), "{out}");
    assert!(
        out.contains("data-zeyn-embed-load") && out.contains("<template data-zeyn-embed><iframe"),
        "{out}"
    );
    assert!(
        !out.replace("<template data-zeyn-embed><iframe", "")
            .contains("<iframe"),
        "without JavaScript the frame stays inert: {out}"
    );
    assert_eq!(
        e.schema()["remember"],
        serde_json::json!({ "scope": "external_video-2", "ms": 86_400_000 })
    );
    let plain = html(
        &Embed::url("https://www.youtube-nocookie.com/embed/abc")
            .title("T")
            .click_to_load(""),
        &ctx(frames()),
    );
    assert!(!plain.contains("data-zeyn-embed-remember"), "{plain}");
    let max: &'static str = "a".repeat(40).leak();
    let _ = Embed::url("https://a.test/")
        .title("T")
        .remember(max, zeyn::ui::MAX_EMBED_CONSENT);
}

#[test]
fn remember_scopes_outside_letters_digits_dash_and_underscore_panic() {
    let long: &'static str = "a".repeat(41).leak();
    for bad in ["", long, "a b", "a.b", "a\"b", "ç", "a/b", "<x>"] {
        let panic = std::panic::catch_unwind(|| {
            Embed::url("https://a.test/")
                .title("T")
                .remember(bad, DAY)
        });
        let message = *panic
            .err()
            .expect(bad)
            .downcast::<String>()
            .unwrap();
        assert!(
            message.contains("scope") && message.contains("1 to 40 ASCII letters, digits"),
            "{bad}: {message}"
        );
    }
}

#[test]
fn embed_urls_are_canonical_and_never_html() {
    let pasted = r#"<iframe width="560" height="315" src="https://WWW.YouTube-nocookie.com/embed/abc?si=x&amp;utm_source=share&amp;fbclid=1" title="YouTube video player" frameborder="0" allow="autoplay" allowfullscreen></iframe>"#;
    assert_eq!(
        embed_url(pasted).as_deref(),
        Some("https://www.youtube-nocookie.com/embed/abc?si=x")
    );
    assert_eq!(
        embed_url(" https://a.test:443/x?gclid=2&utm_medium=m#t=1 ").as_deref(),
        Some("https://a.test/x#t=1")
    );
    for bad in [
        "javascript:alert(1)",
        "JAVASCRIPT:alert(1)",
        "<script src=\"https://a.test/x.js\"></script>",
        "<iframe src=\"javascript:alert(1)\"></iframe>",
        "http://a.test/",
        "https://a.test/x\"onload=\"alert(1)",
        "https://user:pw@a.test/",
        "//a.test/",
        "",
    ] {
        assert_eq!(embed_url(bad), None, "{bad}");
    }
}

#[entity(table = "videos")]
pub struct Video {
    #[key]
    pub id: VideoId,
    #[rule(required, embed)]
    pub url: String,
}

#[action(public)]
pub async fn save_video(v: Valid<Video>) -> Result<Video> {
    Ok(v.into_inner())
}

fn video_form() -> zeyn::SubmitForm<Video, save_video> {
    Form::of(Video::default())
        .field(Video::url)
        .submit("Save", save_video)
}

#[test]
fn embed_rule_stores_the_canonical_address_of_pasted_embed_code() {
    let ctx = ctx(frames());
    let values = serde_json::json!({ "url": r#"<iframe src="https://www.youtube-nocookie.com/embed/abc?utm_campaign=c&amp;t=9"></iframe>"# });
    let v = video_form()
        .form()
        .read_for(values.as_object().unwrap(), &ctx)
        .unwrap();
    assert_eq!(v.url, "https://www.youtube-nocookie.com/embed/abc?t=9");

    let unlisted = serde_json::json!({ "url": "https://evil.example.com/embed/abc" });
    let errors = video_form()
        .form()
        .read_for(unlisted.as_object().unwrap(), &ctx)
        .unwrap_err();
    assert_eq!(
        errors
            .errors
            .iter()
            .map(|e| (e.field, e.rule))
            .collect::<Vec<_>>(),
        [("url", Rule::Embed)]
    );
    assert!(
        ctx.t(&errors.errors[0].message)
            .contains("evil.example.com is not on the list"),
        "{errors:?}"
    );

    for bad in [
        "javascript:alert(1)",
        "<script>alert(1)</script>",
        "http://www.youtube-nocookie.com/embed/abc",
    ] {
        let values = serde_json::json!({ "url": bad });
        let errors = video_form()
            .form()
            .read_for(values.as_object().unwrap(), &ctx)
            .unwrap_err();
        assert!(
            errors
                .errors
                .iter()
                .any(|e| e.field == "url" && e.rule == Rule::Embed),
            "{bad}: {errors:?}"
        );
    }
}

#[test]
fn embed_rule_refuses_html_outside_forms() {
    let html = Video {
        url: r#"<iframe src="https://www.youtube-nocookie.com/embed/abc"></iframe>"#.into(),
        ..Video::default()
    };
    let rules: Vec<Rule> = Valid::new(html)
        .unwrap_err()
        .errors
        .into_iter()
        .map(|e| e.rule)
        .collect();
    assert_eq!(rules, [Rule::Embed]);
    assert!(
        Valid::new(Video {
            url: "https://www.youtube-nocookie.com/embed/abc".into(),
            ..Video::default()
        })
        .is_ok()
    );
}

#[tokio::test]
async fn api_inputs_with_an_unlisted_embed_host_are_refused() {
    let router = App::new("T")
        .csp(|c| c.frame_src(["https://www.youtube-nocookie.com"]))
        .auth(|_: &axum::http::HeaderMap| Some(Identity::user("ayse")))
        .feature(Feature::new("media").api(save_video))
        .router()
        .unwrap();
    let call = |url: &str| {
        Request::post("/api/v1/media/save_video")
            .header(header::CONTENT_TYPE, "application/json")
            .header(header::AUTHORIZATION, "Bearer test")
            .body(Body::from(
                serde_json::json!({ "id": 1, "url": url }).to_string(),
            ))
            .unwrap()
    };
    let ok = router
        .clone()
        .oneshot(call("https://www.youtube-nocookie.com/embed/abc"))
        .await
        .unwrap();
    assert_eq!(ok.status(), 200);
    let refused = router
        .clone()
        .oneshot(call("https://evil.example.com/embed/abc"))
        .await
        .unwrap();
    assert_ne!(refused.status(), 200);
    let body = String::from_utf8(
        refused
            .into_body()
            .collect()
            .await
            .unwrap()
            .to_bytes()
            .to_vec(),
    )
    .unwrap();
    assert!(body.contains("evil.example.com"), "{body}");
}

#[test]
fn images_render_with_size_alt_and_async_decoding() {
    let img = Image::new("https://i.example.com/logo.png")
        .alt("Northwind logo")
        .size(240, 80)
        .lazy()
        .srcset([
            ("https://i.example.com/logo@2x.png", "2x"),
            ("https://other.test/logo@3x.png", "3x"),
            (
                "https://i.example.com/a.png",
                "2x, https://other.test/b.png 3x",
            ),
            ("https://i.example.com/w.png", "800w"),
        ]);
    let out = html(&img, &ctx(frames()));
    assert!(out.starts_with(r#"<img class="z-image" src="https://i.example.com/logo.png" alt="Northwind logo" width="240" height="80" decoding="async" loading="lazy""#), "{out}");
    assert!(out.contains(r#"srcset="https://i.example.com/logo@2x.png 2x, https://i.example.com/w.png 800w" sizes="(max-width: 240px) 100vw, 240px""#), "{out}");
    assert!(!out.contains("other.test"), "{out}");
    let local = html(
        &Image::new("/static/logo.png")
            .size(10, 10)
            .alt("Logo"),
        &ctx(Csp::new()),
    );
    assert!(
        local.contains(r#"src="/static/logo.png""#)
            && !local.contains("loading=")
            && !local.contains("srcset")
    );
}

#[test]
fn images_from_unlisted_hosts_show_their_alt_text() {
    for src in [
        "https://other.test/x.png",
        "http://i.example.com/x.png",
        "javascript:alert(1)",
        "//other.test/x.png",
        "data:image/svg+xml,<svg onload=alert(1)>",
    ] {
        let out = html(
            &Image::new(src)
                .alt("A <b>chart</b>")
                .size(10, 10),
            &ctx(frames()),
        );
        assert_eq!(
            out,
            r#"<span class="z-image-blocked" data-testid="s.image">A &lt;b&gt;chart&lt;/b&gt;</span>"#,
            "{src}"
        );
    }
}

#[test]
fn external_links_never_hand_over_the_window() {
    let r = Render::english("s");
    let plain = ExternalLink::new("https://example.com/help", "Help").html(&r);
    assert_eq!(
        plain,
        r#"<a class="z-link-external" href="https://example.com/help" rel="noopener noreferrer" data-testid="s.link">Help<span class="z-sr-only"> (external site)</span><span class="z-link-external-icon" aria-hidden="true">&#8599;</span></a>"#
    );
    let user = ExternalLink::new("https://example.org/?a=1&b=2", "Site")
        .user_content()
        .new_tab()
        .html(&r);
    assert!(
        user.contains(r#"href="https://example.org/?a=1&amp;b=2""#),
        "{user}"
    );
    assert!(
        user.contains(r#"rel="noopener noreferrer nofollow ugc" target="_blank""#),
        "{user}"
    );
    assert!(user.contains("(external site, opens in a new tab)"));
    for bad in [
        "javascript:alert(1)",
        "data:text/html,x",
        "/local",
        "https://",
        "https://a b.test",
    ] {
        let out = ExternalLink::new(bad, "x").html(&r);
        assert_eq!(out, r#"<span class="z-link-text">x</span>"#, "{bad}");
    }
    let tr = zeyn::I18n::builtin();
    let out = ExternalLink::new("https://example.com", "Yardım")
        .new_tab()
        .html(&Render::new("s", "tr", tr));
    assert!(out.contains("(dış site, yeni sekmede açılır)"), "{out}");
}

const DAY: std::time::Duration = std::time::Duration::from_secs(24 * 60 * 60);

#[test]
fn remember_times_outside_one_second_to_thirty_days_panic() {
    let too_long = zeyn::ui::MAX_EMBED_CONSENT + std::time::Duration::from_secs(1);
    for bad in [
        std::time::Duration::ZERO,
        std::time::Duration::from_millis(999),
        too_long,
    ] {
        let panic = std::panic::catch_unwind(|| {
            Embed::url("https://a.test/")
                .title("T")
                .remember("notice", bad)
        });
        let message = *panic
            .err()
            .expect("panics")
            .downcast::<String>()
            .unwrap();
        assert!(
            message.contains("1 second to 30 days"),
            "{bad:?}: {message}"
        );
    }
    let _ = Embed::url("https://a.test/")
        .title("T")
        .remember("notice", std::time::Duration::from_secs(1));
}