AtlatestRepositorysigil-doc

sigil-doc / tree / testtest-video-destination.sgl

1;; SD-P2 follow-up: a video destination is a link too.
2;;
3;; Before this change `video-destination` handed the `url` provider's source
4;; straight to the print backend's link emitter with no check of any kind, six
5;; lines from the code that builds a checked link. This closes that.
6;;
7;; The distinction the tests below hold to: for `cloudflare` and `youtube` the
8;; source is an OPAQUE PROVIDER ID, not a URL, and the semantic layer has no
9;; business parsing it. Only the destination the print backend CONSTRUCTS from
10;; it is a URL, so that is where it is checked. For the `url` provider the
11;; source IS the destination, so the authoring constructor checks it directly.
12(import (sigil test) (sigil string) (sigil doc) (sigil doc publication)
13 (sigil doc print) (sigil doc render html) (sigil doc render typst))
15(define loc (make-srcloc "video.sgd" 1 1 0 1))
16(define (txt value) (make-text value loc))
17(define (raised-message thunk)
18 (guard (exn (else (exception-message exn))) (thunk) #f))
19(define (raises? thunk) (guard (_ (else #t)) (thunk) #f))
21(define (document-with node)
22 (make-document 'chapter "Video" '() (list node) loc))
23(define (typst-of node)
24 (print-publication->typst
25 (make-print-publication "Video" #f '()
26 (list (make-publication-document "video" (document-with node))) '())))
27(define (html-of node)
28 (let* ((doc (document-with node))
29 (item (make-publication-document "video" doc))
30 (index (build-publication-index (list item))))
31 (document->html-fragment doc
32 (make-html-render-context index "video" (lambda (target) "#") '() 1))))
34(test "a url-provider source is held to the link-target policy"
35 (assert-equal "https://example.com/demo?a=1&b=2"
36 (video-source (make-video "https://example.com/demo?a=1&b=2" 'url #f 'link loc)))
37 (assert-equal "http://example.com/demo"
38 (video-source (make-video "http://example.com/demo" 'url #f 'link loc)))
39 (for-each
40 (lambda (entry)
41 (let ((message (raised-message (lambda () (make-video (car entry) 'url #f 'link loc)))))
42 (assert-true (string? message))
43 (assert-true (string-contains? message (cadr entry)))))
44 (list
45 (list "javascript:alert(1)" "scheme javascript is refused")
46 (list "JaVaScRiPt:alert(1)" "scheme javascript is refused")
47 (list "file:///etc/passwd" "scheme file is refused")
48 ;; a video is not correspondence, so mailto is outside this policy even
49 ;; though the link node admits it
50 (list "mailto:name@example.com" "scheme mailto is refused")
51 (list "demo-video-id" "needs an explicit scheme")
52 (list "https://example.com/a b" "printable ASCII")
53 (list "https://" "needs an authority"))))
55(test "an opaque provider id is not parsed as a URL"
56 ;; The semantic layer keeps ids opaque: they are provider vocabulary, and a
57 ;; provider is free to change their shape without the model objecting.
58 (for-each
59 (lambda (id)
60 (assert-equal id (video-source (make-video id 'cloudflare #f 'omit loc))))
61 (list "demo-video-id" "cf-007fa4eb00931b0deb265cfda5d37875" "48JlgiBpw_I"))
62 ;; and the ordinary rendering escapes rather than trusts them
63 (let ((html (html-of (make-video "demo\" onload=\"alert(1)" 'cloudflare #f 'omit loc))))
64 (assert-true (string-contains? html "data-video-source=\"demo" onload="alert(1)\""))
65 (assert-false (string-contains? html "onload=\"alert(1)\""))))
67(test "the print backend refuses a destination it cannot safely emit"
68 ;; Reachable through the public API with no neutering: an opaque id the model
69 ;; correctly does not police can still build an unusable destination, and the
70 ;; emission point is what catches it.
71 (for-each
72 (lambda (id)
73 (assert-true
74 (raises? (lambda () (typst-of (make-video id 'cloudflare #f 'link loc))))))
75 (list "has a space" "has\nnewline" "has\\backslash" "café"))
76 ;; positive control: the same path renders an ordinary id, so this test cannot
77 ;; pass by refusing everything
78 (assert-true
79 (string-contains? (typst-of (make-video "cf-007fa4eb00931b0deb265cfda5d37875" 'cloudflare #f 'link loc))
80 "#link(\"https://iframe.videodelivery.net/cf-007fa4eb00931b0deb265cfda5d37875\")")))
82;; Raised by the leader and worth answering with bytes rather than reasoning:
83;; an opaque id is not a URL, but it IS INTERPOLATED INTO ONE, which makes it an
84;; injection surface rather than a validation surface. Can an id carrying a
85;; quote, backslash, newline, angle bracket or space escape the string it lands
86;; in? Measured below at both emission points.
87(test "an interpolated provider id cannot escape the string it lands in"
88 ;; Refused outright by the constructed-destination policy.
89 (for-each
90 (lambda (id)
91 (assert-true (raises? (lambda () (typst-of (make-video id 'cloudflare #f 'link loc))))))
92 (list "back\\slash" "new\nline" "a space" "tab\there"))
93 ;; Admitted, because a quote or an angle bracket is legal printable ASCII, and
94 ;; then provably neutralised by the emitter rather than trusted.
95 (let ((typst (typst-of (make-video "quote\"id" 'cloudflare #f 'link loc))))
96 (assert-true (string-contains? typst "#link(\"https://iframe.videodelivery.net/quote\\\"id\")"))
97 ;; the quote never appears unescaped, so it cannot terminate the destination
98 (assert-false (string-contains? typst "videodelivery.net/quote\"id")))
99 (let ((html (html-of (make-video "angle<id>\"x" 'cloudflare #f 'omit loc))))
100 (assert-true (string-contains? html "data-video-source=\"angle&lt;id&gt;&quot;x\""))
101 (assert-false (string-contains? html "<id>"))))
103(test "every video source already shipped still builds and renders"
104 ;; Measured population at the time of the change: 47 course videos (46
105 ;; cloudflare, 1 youtube), 1 repository fixture, 1 url-provider test source.
106 ;; None is refused; this is not a breaking change to shipped content.
107 (for-each
108 (lambda (entry)
109 (let* ((node (make-video (car entry) (cadr entry) #f 'link loc))
110 (typst (typst-of node)))
111 (assert-true (string-contains? typst (caddr entry)))))
112 (list
113 (list "cf-007fa4eb00931b0deb265cfda5d37875" 'cloudflare
114 "#link(\"https://iframe.videodelivery.net/cf-007fa4eb00931b0deb265cfda5d37875\")")
115 (list "48JlgiBpw_I" 'youtube
116 "#link(\"https://www.youtube.com/watch?v=48JlgiBpw_I\")")
117 (list "demo-video-id" 'cloudflare
118 "#link(\"https://iframe.videodelivery.net/demo-video-id\")")
119 (list "https://example.com/demo?a=1&b=2" 'url
120 "#link(\"https://example.com/demo?a=1&b=2\")"))))