/
demo.lua
executable file
·159 lines (133 loc) · 3.24 KB
/
demo.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
#! /lua/lua
-- vi: set noexpandtab :vi
print("Content-Type: text/html\r\n\r\n")
print[[
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Lua: demo</TITLE>
<LINK REL="stylesheet" TYPE="text/css" HREF="https://lua.org/lua.css">
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
<STYLE TYPE="text/css">
textarea {
font-family: monospace ;
}
input {
margin-right: 1em ;
}
</STYLE>
</HEAD>
<BODY>
<H1>
<A HREF="https://lua.org/home.html"><IMG SRC="https://lua.org/images/logo.gif" ALT="Lua"></A>
Demo
</H1>
<P>
Try Lua before
<A HREF="https://lua.org/download.html">downloading</A> it.
Enter your Lua program
or
choose one of the demo programs below.
<DIV CLASS="menubar">
<A HREF="./demo.lua?hello">hello</A>
</DIV>
<FORM ACTION="./demo.lua" METHOD="POST">
<TEXTAREA ROWS="16" COLS="72" NAME="input" maxlength="2000">]]
-- demo.lua
-- run user programs in constrained environment
local T=""
local E,I
-- save functions needed at the end
local _G=_G
local collectgarbage=collectgarbage
local pack=table.pack
local print=print
local tostring=tostring
local unpack=table.unpack
local write=io.write
-- snippets
local demos = {}
demos["hello"] = "setmetatable({}, {__gc = function() print[[hello shenjack]] end})"
local query=os.getenv("QUERY_STRING")
if query ~= "" then
T=demos[query] or ""
end
-- convert input to plain text
if os.getenv("REQUEST_METHOD") == "POST" then
T=io.read"*a"
T=string.match(T,"=(.-)$") or ""
T=string.gsub(T,"+"," ")
T=string.gsub(T,"%%(%x%x)",function (x) return string.char(tonumber(x,16)) end)
T=string.gsub(T,"^%s*=%s*","return ")
if T ~= "" then
log=io.open("/logs/lua.log", "a")
log:write("-- ", os.date(), "\n", T, "\n\n")
log:close()
end
end
-- continue HTML began in shell script
write(T)
write[[</TEXTAREA>
<P>
<INPUT TYPE="submit" VALUE="run">
<INPUT TYPE="button" VALUE="clear" onclick="this.form.elements['input'].value=''">
<INPUT TYPE="reset" VALUE="restore">
<INPUT TYPE="button" VALUE="restart" onclick="window.location.href='./demo.lua'">
</FORM>
]]
io.flush()
-- the serious thing begins
if os.getenv("REQUEST_METHOD") == "POST" then
-- limit resource
local steplimit = 100
local memlimit = 100
local count = 0
local function step ()
count = count + 1
if collectgarbage("count") > memlimit then
error("DoSer uses too much memory")
end
if count > steplimit then
error("DoSer uses too much CPU")
end
end
debug.sethook(step, "", 100)
-- delete unsafe functions
arg=nil
debug.debug=nil
debug.getfenv=getfenv
debug.getregistry=nil
dofile=nil
io={write=io.write}
loadfile=nil
os.execute=nil
os.getenv=nil
os.remove=nil
os.rename=nil
os.tmpname=nil
package.loaded.io=io
package.loaded.package=nil
package=nil
require=nil
-- run program in global environment
write[[<H2>Output</H2>
<TEXTAREA ROWS="8" COLS="72">]]
T,E=load(T,"=input","t")
if not T then
print(E) E="failed to compile" I="alert"
else
T=pack(pcall(T))
_G.tostring=tostring
if not T[1] then
print(T[2]) E="failed to run" I="alert"
else
print(unpack(T,2,T.n)) E="ran successfully" I="ok"
end
collectgarbage()
end
write("\n-- steps/100:\t", count, "\n")
write("-- memory:\t", string.format("%.2f",collectgarbage("count")), "\n")
-- continue HTML
write('</TEXTAREA><P><IMG SRC="https://lua.org/images/',I,'.png" ALIGN="absbottom">\n')
write('Your program ',E,'.\n')
end