Repository navigation
Expand file tree
/
Copy pathiframe_test.go
More file actions
193 lines (181 loc) · 8.11 KB
/
Copy pathiframe_test.go
File metadata and controls
193 lines (181 loc) · 8.11 KB
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
// Copyright (c) the go-webengine/engine authors.
// SPDX-License-Identifier: BSD-3-Clause
package engine
import (
"context"
"image"
"net/http"
"net/http/httptest"
"testing"
)
// TestIframeSrcdocRendersIntoParentPage is the real end-to-end path: an
// <iframe srcdoc="..."> is parsed as its own document, laid out and painted
// with no network involved, and composited into the final page exactly like
// an <img>'s bitmap — reusing the SAME replaced-element pipeline canvas.go
// added for <canvas>.
func TestIframeSrcdocRendersIntoParentPage(t *testing.T) {
html := `<html><body style="margin:0">
<iframe id="f" width="40" height="40" srcdoc="<body style="margin:0;background:#ff0000"></body>"></iframe>
</body></html>`
e := &Engine{MaxIframes: 4}
img, info, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 60, 60))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
if info.ContentHeight < 40 {
t.Fatalf("contentHeight = %d, want at least 40 (the iframe's own height)", info.ContentHeight)
}
r, g, b, a := img.At(10, 10).RGBA()
if r>>8 != 255 || g>>8 != 0 || b>>8 != 0 || a>>8 != 255 {
t.Fatalf("pixel at (10,10) = (%d,%d,%d,%d), want opaque red — the iframe's own content never reached the final page bitmap", r>>8, g>>8, b>>8, a>>8)
}
}
// TestIframeSrcFetchesAndRendersARealDocument confirms the src= path, which
// recurses into this engine's own Fetch, not just srcdoc's inline parse.
func TestIframeSrcFetchesAndRendersARealDocument(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<body style="margin:0;background:#00ff00"></body>`))
}))
defer srv.Close()
html := `<html><body style="margin:0"><iframe id="f" width="30" height="30" src="` + srv.URL + `/frame.html"></iframe></body></html>`
e := &Engine{Client: newHTTPClient(), MaxIframes: 4}
img, _, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 60, 60))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
r, g, b, a := img.At(10, 10).RGBA()
if r>>8 != 0 || g>>8 != 255 || b>>8 != 0 || a>>8 != 255 {
t.Fatalf("pixel at (10,10) = (%d,%d,%d,%d), want opaque green — the fetched iframe document never reached the final page bitmap", r>>8, g>>8, b>>8, a>>8)
}
}
// TestMaxIframesBoundsHowManyAreRendered is the budget this engine's own
// "always bounded, never silently wrong" convention requires for anything
// this expensive — a real recursive render per iframe, not a plain fetch.
func TestMaxIframesBoundsHowManyAreRendered(t *testing.T) {
var hits int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<body style="background:#00ff00"></body>`))
}))
defer srv.Close()
html := `<html><body>` +
`<iframe width="10" height="10" src="` + srv.URL + `/a"></iframe>` +
`<iframe width="10" height="10" src="` + srv.URL + `/b"></iframe>` +
`<iframe width="10" height="10" src="` + srv.URL + `/c"></iframe>` +
`</body></html>`
e := &Engine{Client: newHTTPClient(), MaxIframes: 1}
_, _, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 100, 100))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
if hits != 1 {
t.Fatalf("server hit %d times, want exactly 1 (MaxIframes: 1)", hits)
}
}
// TestIframeContentDocumentWorksEndToEnd confirms loadIframe's own
// n.IframeDoc assignment (iframe.go) is what js/iframe.go's
// contentDocument/contentWindow actually read — the real pipeline, not
// just the js package's own unit tests, which set IframeDoc by hand.
func TestIframeContentDocumentWorksEndToEnd(t *testing.T) {
var logs []string
html := `<html><body>
<iframe id="f" srcdoc="<body><p id="greeting">hello from the nested document</p></body>"></iframe>
<script>
var d = document.getElementById('f').contentDocument;
console.log('greeting=' + d.getElementById('greeting').textContent);
</script>
</body></html>`
e := &Engine{MaxIframes: 4, JSLog: func(s string) { logs = append(logs, s) }}
_, _, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 60, 60))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
found := false
for _, l := range logs {
if l == "log: greeting=hello from the nested document" {
found = true
}
}
if !found {
t.Fatalf("expected the parent script to read the nested document's real content, got logs=%v", logs)
}
}
// TestIframeMutationThroughContentDocumentRerenders is the real end-to-end
// path this round added: a parent script mutates a node reached through
// contentDocument, and the iframe's own FINAL visible bitmap reflects the
// change — not the content loadIframe's own one-shot pre-script load
// produced.
func TestIframeMutationThroughContentDocumentRerenders(t *testing.T) {
html := `<html><body style="margin:0">
<iframe id="f" width="40" height="40" srcdoc="<body style="margin:0;background:#ff0000"></body>"></iframe>
<script>
document.getElementById('f').contentDocument.body.style.background = '#00ff00';
</script>
</body></html>`
e := &Engine{MaxIframes: 4}
img, _, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 60, 60))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
r, g, b, a := img.At(10, 10).RGBA()
if r>>8 != 0 || g>>8 != 255 || b>>8 != 0 || a>>8 != 255 {
t.Fatalf("pixel at (10,10) = (%d,%d,%d,%d), want opaque green — the parent's mutation through contentDocument never reached the iframe's final bitmap", r>>8, g>>8, b>>8, a>>8)
}
}
// TestIframeNestedScriptDoesNotRerunOnRefresh confirms refreshIframeImages'
// own stated guarantee: re-rendering after a parent mutation must NOT
// re-execute the nested document's own JavaScript a second time. The
// nested document's own JSLog output shares this engine's single Log
// callback (refreshIframeImages copies *e, including JSLog), so a nested
// script's console.log appearing twice would mean its script ran twice.
func TestIframeNestedScriptDoesNotRerunOnRefresh(t *testing.T) {
var logs []string
html := `<html><body>
<iframe id="f" width="20" height="20" srcdoc="<body><script>console.log('nested-ran')</script></body>"></iframe>
<script>
document.getElementById('f').contentDocument.body.setAttribute('data-touched', '1');
</script>
</body></html>`
e := &Engine{MaxIframes: 4, JSLog: func(s string) { logs = append(logs, s) }}
_, _, err := e.RenderHTML(context.Background(), html, "https://example.com/", image.Rect(0, 0, 60, 60))
if err != nil {
t.Fatalf("RenderHTML error: %v", err)
}
n := 0
for _, l := range logs {
if l == "log: nested-ran" {
n++
}
}
if n != 1 {
t.Fatalf("'nested-ran' logged %d time(s), want exactly 1 — refreshIframeImages must not re-execute the nested document's own JavaScript", n)
}
}
// TestIframeDepthCapStopsSelfEmbedding is the real safety property: a page
// whose iframe src points back at ITSELF (an accident or a tracking-pixel
// style construction, either way a real hazard) must not recurse past
// maxIframeDepth, however many requests budget would otherwise allow.
func TestIframeDepthCapStopsSelfEmbedding(t *testing.T) {
var hits int
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hits++
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<body><iframe width="10" height="10" src="` + srv.URL + `/"></iframe></body>`))
}))
defer srv.Close()
e := &Engine{Client: newHTTPClient(), MaxIframes: 4}
ctx, cancel := context.WithTimeout(context.Background(), 10_000_000_000) // 10s
defer cancel()
_, _, err := e.Render(ctx, srv.URL+"/", image.Rect(0, 0, 100, 100))
if err != nil {
t.Fatalf("Render error: %v", err)
}
// Top-level fetch (1) + one nested iframe render (1): depth cap stops
// before a second level of nesting is ever fetched.
if hits > 2 {
t.Fatalf("server hit %d times, want at most 2 (depth cap should stop self-embedding)", hits)
}
}