-
Notifications
You must be signed in to change notification settings - Fork 0
/
lullaby.lua
686 lines (541 loc) · 19.1 KB
/
lullaby.lua
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
local html_data = require 'lullaby.html_data'
local sax = require 'lullaby.sax'
local U = require 'lullaby.util'
local escape = require 'lullaby.escape'
--======
--= Helpers
--======
-- We use {k=v} attributes in the user interface for convenience but the
-- lower-level libraries use {{k,v}} because ipairs more predictable than pairs
local function tableToPairs(t)
local kvs = {}
for k,v in pairs(t) do
if type(k) == 'string' then
table.insert(kvs, {k,v})
end
end
-- Make this function deterministic:
table.sort(kvs, function(a, b) return a[1] < b[1] end)
return kvs
end
--======
--= Error messages
--======
--======
--= Process html data from the spec
--======
local ElemMap = {}
for _, name, kind in U.xpairs(html_data.Elems) do
name = name:lower()
ElemMap[name] = {
name=name,
kind=kind,
}
end
local function html_set(names)
return U.Set(U.map(names, string.lower))
end
local UniverseSet = setmetatable({}, {
__index = function() return true end,
})
local AttrMap = {}
for _, name, kind, allowed_elems in U.xpairs(html_data.Attrs) do
name = name:lower()
local attr = {}
attr.name = name
attr.kind = kind[1]
if kind[1] == 'Enum' then
attr.allowed_values = U.Set(kind[2])
end
if allowed_elems == true then
attr.allowed_on = UniverseSet
else
attr.allowed_on = html_set(allowed_elems)
end
AttrMap[name] = attr
end
-- data-xxx attributes:
setmetatable(AttrMap, {
__index = function(self, key)
local name = key:lower()
if string.match(name, 'data-[%w-]+') then
return {name=name, kind='Text', allowed_on=UniverseSet}
else
return nil
end
end
})
--======
--= Internal HTML constructors.
--======
-- See note [Attribute datatypes]
-- Datatype for unsafe attributes (like event handlers)
local RawMt = {}
local function Raw(str)
assert(type(str) == 'string')
return setmetatable({value=str}, RawMt)
end
RawMt.__tostring = function(self)
return self.value
end
local function isRawType(x) return getmetatable(x) == RawMt end
--Datatype for URL attributes
local UrlMt = {}
-- Some web frameworks consider parameters without an equals sign
-- to be a null value (instead of an empty string). In order to support that,
-- we use a special Nil value, similarly to the JSON library.
local Nil = {}
local url_schemes = U.Set{'http', 'https'}
local function _Url(scheme, host, path, kw, isabsolute)
path = path or {}
kw = kw or {}
if scheme then
if not escape.is_valid_url_scheme(scheme) then
error(string.format("Unrecognized scheme %q", tostring(scheme)))
end
if not host then
error("Host must be present if scheme is present")
end
end
if host then
if not escape.is_valid_url_host(host) then
error(string.format("Bad characters in host %q", host))
end
assert(isabsolute)
end
local params = {}
local hash = nil
for k, v in pairs(kw) do
if k == 'params' then params = tableToPairs(v)
elseif k == 'hash' then hash = v
else
if type(k) ~= 'number' then
--Typos or wrong param names
error(string.format("Unrecognized keyword %q", tostring(k)))
end
end
end
if not isabsolute then
if #path == 0 and #params == 0 and not hash then
error("Empty relative Url")
end
end
return setmetatable({
scheme=scheme,
host=host,
path=path,
params=params,
hash=hash,
isabsolute=isabsolute,
}, UrlMt)
end
UrlMt.__tostring = function(self)
local res = {}
local function w(s)
assert(type(s) == 'string')
table.insert(res, s)
end
if self.scheme then
w(self.scheme)
w(':')
end
if self.host then
w('//')
w(self.host)
end
if self.isabsolute then
w('/')
end
w(table.concat(U.map(self.path, escape.url_path), '/'))
if #self.params > 0 then
w('?')
for i, key, value in U.xpairs(self.params) do
if i > 1 then w('&') end
w(escape.url_param(key))
if value ~= Nil then
w('=')
w(escape.url_param(value))
end
end
end
if self.hash then
w('#')
w(escape.url_param(self.hash))
end
return table.concat(res)
end
local function isUrlType(x) return getmetatable(x) == UrlMt end
local function AbsUrl(args)
assert(type(args) == 'table')
return _Url(args[1], args[2], args[3], args, true)
end
local function RelUrl(args)
assert(type(args) == 'table')
return _Url(nil, nil, args[1], args, false)
end
-- Detect missing function wrappers:
-- SPAN{B{"x"}} would convert to <b>x</b><span></span>
-- (not what we want!)
local YIELDER_RETURN = {'yielder_return'}
local function YieldText(text)
if type(text) ~= 'string' then
error("expected string", 2)
end
sax.EmitTextEvent(text)
return YIELDER_RETURN
end
--I'm shoehorning Raw HTML under a text event to avoid having to
-- modify the SAX infrastructure.
local function YieldHtml(html)
if type(html) ~= 'string' then
error("expected string", 2)
end
sax.EmitTextEvent(Raw(html))
return YIELDER_RETURN
end
local function YieldElement(elemname, attrs, body)
-- Check element contents
local elem = ElemMap[elemname:lower()]
if elem.kind == 'Normal' then
--everything is allowed
elseif elem.kind == 'Void' then
if type(body) ~= 'nil' then
error(string.format("Void element %s should not have contents", elem.name))
end
elseif elem.kind == 'Raw' then
if body ~= nil then
if not isRawType(body) then
error(string.format("%s expects raw content", elem.name))
end
local bodystr = body.value
if string.find(bodystr, '</', 1, true) then
-- Technically, we could allow "</" when its not followed by the corresponding tag name
-- (case-insensitively). However, I would rather be more strict just in case.
error(string.format("Close tag in raw content for %s element", elem.name))
end
end
else
error('impossible')
end
-- Check attributes:
local event_attrs = U.xmap(attrs, function(attrname, attrvalue)
if not isRawType(attrvalue) then
local attr = AttrMap[attrname:lower()]
if not attr then
error(string.format("Unknown attribute %q", attrname))
end
if not attr.allowed_on[elem.name] then
error(string.format("Attribute %q not allowed on tag %q", attrname, elem.name))
end
local function check_type(received, expected, ename)
local ok
if type(expected) == 'string' then
ename = expected
ok = (type(received) == expected)
elseif type(expected) == 'function' then
ok = expected(received)
else
error('impossible')
end
if not ok then
error(string.format("Attribute %q received a %s; expected %s", attrname, type(received), ename))
end
end
if attr.kind == 'Text' then
check_type(attrvalue, 'string')
elseif attr.kind == 'Enum' then
check_type(attrvalue, 'string')
if not attr.allowed_values[attrvalue] then
error(string.format("Attribute %q does not allow value %q", attrname, tostring(attrvalue)))
end
elseif attr.kind == 'Boolean' then
check_type(attrvalue, 'boolean')
elseif attr.kind == 'URL' then
check_type(attrvalue, isUrlType, 'Url')
elseif attr.kind == 'Raw' then
check_type(attrvalue, isRawType, 'Raw')
else
error('impossible')
end
end
return {attrname, attrvalue}
end)
sax.EmitStartEvent(elem.name, event_attrs)
if body == YIELDER_RETURN then
error("Missing function wrapper in element body")
elseif type(body) == 'nil' then
-- No contents.
elseif type(body) == 'string' then
YieldText(body)
elseif type(body) == 'function' then
body()
elseif isRawType(body) then
YieldHtml(tostring(body))
else
error(string.format("Tag body has type %s", type(body)))
end
sax.EmitEndEvent(elem.name)
return YIELDER_RETURN
end
--======
--= Public Html constructors
--======
local H = {} --Exports
-- Url Datatype
H.AbsUrl = AbsUrl
H.RelUrl = RelUrl
H.Nil = Nil
-- Unsafe string datatype
H.Raw = Raw
-- Element constructors
-- usage: TAGNAME{ attr=value, body }
for _, elem in pairs(ElemMap) do
H[elem.name:upper()] = function(args)
if type(args) ~= 'table' then
error("Element constructor should receive a table parameter")
end
if #args > 1 then
error("Multiple element body parameters")
end
local body = args[1]
local attrs = tableToPairs(args)
return YieldElement(elem.name, attrs, body)
end
end
-- Text nodes
H.Text = YieldText
-- Raw HTML (escape valve for nasty hacks)
H.RawHtml = YieldHtml
-- string, function -> stream
local function Document(args)
local title = args.title
local head = args.head
local body = args.body
local encoding = args.encoding
return function()
H.RawHtml("<!DOCTYPE html>\n")
H.HTML{function()
H.HEAD{function()
if encoding then
H.META{ charset=Raw(encoding) }
end
if title then
H.TITLE{title}
end
if head then
head()
end
end}
H.BODY{function()
if body then
body()
end
end}
end}
end
end
H.Document = Document
local function _printToFile(indent, file, stream_body)
local stream = sax.from_coro(stream_body)
sax.fold_stream(stream, 0, {
Start = function(depth, evt)
file:write('<'..evt.tagname)
for _, attrname, attrvalue in U.xpairs(evt.attrs) do
if type(attrvalue) == 'boolean' then
if attrvalue then
file:write(string.format(' %s', attrname))
end
else
file:write(string.format(' %s="%s"', attrname, escape.html_double_quoted_attribute(tostring(attrvalue))))
end
end
if indent then
if ElemMap[evt.tagname].kind ~= 'Void' then
file:write('\n', string.rep(' ', depth+1))
else
file:write('\n', string.rep(' ', depth))
end
end
file:write('>')
if evt.tagname == 'pre' or evt.tagname == 'textarea' then
--A single newline may be placed immediately after the start tag of pre and textarea elements. [...]
--The otherwise optional newline must be included if the element's contents themselves start with a
--newline (because otherwise the leading newline in the contents would be ignored).
file:write("\n")
end
return depth + 1
end,
Text = function(_, evt)
if isRawType(evt.text) then
file:write(tostring(evt.text))
else
file:write(escape.html_text(evt.text))
end
end,
End = function(depth, _, evt)
if ElemMap[evt.tagname].kind ~= 'Void' then
file:write('</'..evt.tagname)
if indent then
file:write('\n', string.rep(' ', depth))
end
file:write('>')
end
end,
})
end
local StringBufferMt = {
__index = {
write = function(self, ...)
local args = {...}
local n = #self.parts
for i=1, select('#', ...) do
self.parts[n+i] = args[i]
end
end,
to_str = function(self)
return table.concat(self.parts)
end
}
}
local function StringBuffer()
return setmetatable({ parts = {} }, StringBufferMt)
end
local function _printToString(indent, stream_body)
local buffer = StringBuffer()
_printToFile(indent, buffer, stream_body)
return buffer:to_str()
end
H._printToFile = _printToFile
H._printToString = _printToString
--Compactily serialize an html document stream. Does not insert linebreaks or indentation.
H.printToFile = function(file, doc) return _printToFile(false, file, doc) end
--Serialize an html document stream, inserting linebreaks and indentation.
--Whitespace gets inserted inside the tags so the resulting DOMshould be the same
--as the compact serialization.
H.prettyPrintToFile = function(file, doc) return _printToFile(true, file, doc) end
H.printToString = function(doc) return _printToString(false, doc) end
H.prettyPrintToString = function(doc) return _printToString(true, doc) end
-- Runs a callback with all the names in the HTML namespace on the environment.
-- For compatibility with 5.2, the callback should have exactly one parameter named _ENV
H.usingHtml = function(body)
return U.inEnv(H, body)
end
return H
--[==[ Notes
- See https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
[Design Rationale]
The HTML standard is too complicated for us to be able to enforce that only valid HTML is
generated by our library, so you should still pass the output to a validator. Therefore,
we focus on protecting against injection attacks by representing things using appropriate
datatypes.
Using a streaming interface with coroutines has 2 main advantages:
- Output large documents without creating an intermediate document datastructure
- conditionals, variables, sequencing, subtemplates, etc all provided by Lua.
- Lexical scope instead of ad-hoc dynamically scoped templating.
[Element Types]
The HTML element constructors receive a table as their only parameter. The string keys represent
node attributes and the `1` key rrepresents the element body. It can come in three forms:
-- Empty content (nil)
-- <span></span>
SPAN{ }
-- Text content (string)
-- <span>Hello World</span>
SPAN{ "Hello World" }
-- Mixed content (function)
-- <span>Hello <strong>World</strong></span>
SPAN{function()
Text("Hello ")
STRONG{"World"}
end)
The HTML Standard specifies many restriction on what kind of content is allowed for each
element node: (see http://www.whatwg.org/specs/web-apps/current-work/multipage/syntax.html#elements-0)
1) Normal Elements (ex.: div, span, etc)
These are the "regular" HTML elements. Their contents can contain a mixture of text and otehr elements.
2) Void Elements (ex.: img, br)
These elements are not allowed to have any contents and, when serialized, they do *not* have close tags.
Their constructors will only accept `nil` as a body parameter.
3) Raw text Elements (script and style)
These elements contain Javascript or CSS code that does *not* get entity escaped.
They constructors will only accept strings as the body parameter and those strings must not contain the substring "</"
as that could potentially cause the tag to be prematurely closed.
-- The HTML spec also specifies the following element types: --
4) Escapable Text Elements (ex.: textarea, title)
These elements must not contain any elements, only text. The text is escapable with HTML entities.
This library treats these elements as Normal elements. Incorrectly inserted child elements must
be detected with an HTML validator.
4) Foreign Elements (ex.: MathML, SVG)
These elements are not supported for now.
-- Content Models --
Normal elements are actually divided in many subcategories (Flow, Phrasing, Sectioning, etc) and there are many restrictions
as to what elements can be descendents of each other. For example, `div` elements are not allowed inside `p` so
<p><div>x</div></p>
actually gets converted to a DOM that is equivalent to
<p></p><div>x</div><p></p>
This can be very unintuitive but unfortunately these content model restrictions are very complex to fully enforce.
However, an HTML validator should be able to detect these violations.
[Attribute datatypes]
See http://www.whatwg.org/specs/web-apps/current-work/multipage/section-index.html#attributes-1
Different attributes have different allowed values. Enforcing the standard for every specific
attribute would be too complicated so instead we classify attributes in the following categories:
1) String (ex.: id, class, title)
Safe attributes that can receive any textual user-suplied value.
This includes attributes that really can receive any value (for example, `label` or `title`)
and attributes that shouldn't be receiving user-supplied data (`id`, `class`) but that aren't
really prone to injection attacks.
DIV{ id="a", title=get_title() }
2) Enumerated attributes (ex.: contenteditable, method)
These attributes only accept string values from a fixed set. For example, `formmethod` only
allows "POST" and "GET" as values.
2) Boolean: (ex.: async, disabled)
In HTML, Boolean attributes are represented just by their presence or absense. This means that
we need to be aware of boolean values during serialization
<button disabled> <!-- disabled button -->
<button> <!-- enabled button -->
<button disabled="false"> <!-- This actually counts as disabled=true -->
3) URL: (ex.: href, src)
Urls are very prone to injection attacks if people use string concatenation to insert path
segments or query parameters. Because of this, we use a separate datatype for URLS:
Example:
-- "http://www.example.com/dir1/foo.html?k1=a&k2=b#section1"
local url = AbsUrl(
'http', 'www.example.com', {'dir1', 'foo.html'},
params = {k1='a', k2='b'},
hash = 'section1'
)
A{ "link text", href = url }
--protocol relative Urls:
-- "//www.example.com"
AbsUrl(nil, 'www.example.com')
--absolute-path relative urls
-- "/scripts.foo.js"
AbsUrl(nil, nil, {'scripts', 'foo.js'})
--relative urls
-- "foo/bar.html"
RelUrl({'foo', 'bar.js'})
TODO:
For now, the URL datatype is the simplest thing that could work. Ideally, we would love to
have some more functions for combining URLs and building bigger URLs from smaller ones.
4) Raw: (ex.: inline styles and event handlers, non-whitelisted attributes)
For some attributes, the library cannot guarantee that it will safely handle user-supplied
values. In these cases, we require that the template writer mark the values of
these attributes with a `Raw` wrapper.
local handler='alert("hello world")'
-- Rejected because DIV can be sure that the handler isn user-suplied
DIV{ onclick=handler }
-- Now the template writer is promising that handler is not user-supplied:
DIV{ onclick=Raw(handler) }
The template writer is responsible for being sure that the values passed to Raw are safe.
The template writer is also responsible for correctly building and escaping these values as
all the html will do is escape html entities. For example, to create an alert that writes an
user's name, we need to manually escape the Javascript string contents:
DIV{ onclick=Raw(string.format('alert("%s")', escape.js_string(user.name))) }
Finally, you can use a `Raw` wrapper to bypass the usual attribute checking:
-- Ignore typical datatype restrictions:
DIV{ contenteditable=Raw"asdasdasd" }
A{ "linked text", href=Raw("http://www.example.com") }
BUTTON{ disabled=Raw("disabled") }
-- Place attributes in elements where they are not expected:
DIV{ for=Raw"asd" }
-- Use custom attributes not mentioned in the spec.
DIV{ ["fb:foo"]=Raw"bar" } -- <div fb:foo="bar"></div>
--]==]