Skip to content

Commit 9ee0d37

Browse files
committedOct 24, 2015
Don't allow games or mods to add secure. settings
1 parent e2fc8f7 commit 9ee0d37

File tree

1 file changed

+169
-152
lines changed

1 file changed

+169
-152
lines changed
 

‎builtin/mainmenu/tab_settings.lua

+169-152
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,25 @@ local CHAR_CLASSES = {
2626
}
2727

2828
-- returns error message, or nil
29-
local function parse_setting_line(settings, line, read_all, base_level)
29+
local function parse_setting_line(settings, line, read_all, base_level, allow_secure)
30+
-- comment
31+
local comment = line:match("^#" .. CHAR_CLASSES.SPACE .. "*(.*)$")
32+
if comment then
33+
if settings.current_comment == "" then
34+
settings.current_comment = comment
35+
else
36+
settings.current_comment = settings.current_comment .. "\n" .. comment
37+
end
38+
return
39+
end
40+
41+
-- clear current_comment so only comments directly above a setting are bound to it
42+
-- but keep a local reference to it for variables in the current line
43+
local current_comment = settings.current_comment
44+
settings.current_comment = ""
45+
3046
-- empty lines
3147
if line:match("^" .. CHAR_CLASSES.SPACE .. "*$") then
32-
-- clear current_comment so only comments directly above a setting are bound to it
33-
settings.current_comment = ""
3448
return
3549
end
3650

@@ -45,19 +59,6 @@ local function parse_setting_line(settings, line, read_all, base_level)
4559
return
4660
end
4761

48-
-- comment
49-
local comment = line:match("^#" .. CHAR_CLASSES.SPACE .. "*(.*)$")
50-
if comment then
51-
if settings.current_comment == "" then
52-
settings.current_comment = comment
53-
else
54-
settings.current_comment = settings.current_comment .. "\n" .. comment
55-
end
56-
return
57-
end
58-
59-
local error_msg
60-
6162
-- settings
6263
local first_part, name, readable_name, setting_type = line:match("^"
6364
-- this first capture group matches the whole first part,
@@ -71,158 +72,174 @@ local function parse_setting_line(settings, line, read_all, base_level)
7172
.. CHAR_CLASSES.SPACE .. "?"
7273
.. ")")
7374

74-
if first_part then
75-
if readable_name == "" then
76-
readable_name = nil
75+
if not first_part then
76+
return "Invalid line"
77+
end
78+
79+
if name:match("secure%.[.]*") and not allow_secure then
80+
return "Tried to add \"secure.\" setting"
81+
end
82+
83+
if readable_name == "" then
84+
readable_name = nil
85+
end
86+
local remaining_line = line:sub(first_part:len() + 1)
87+
88+
if setting_type == "int" then
89+
local default, min, max = remaining_line:match("^"
90+
-- first int is required, the last 2 are optional
91+
.. "(" .. CHAR_CLASSES.INTEGER .. "+)" .. CHAR_CLASSES.SPACE .. "?"
92+
.. "(" .. CHAR_CLASSES.INTEGER .. "*)" .. CHAR_CLASSES.SPACE .. "?"
93+
.. "(" .. CHAR_CLASSES.INTEGER .. "*)"
94+
.. "$")
95+
96+
if not default or not tonumber(default) then
97+
return "Invalid integer setting"
7798
end
78-
local remaining_line = line:sub(first_part:len() + 1)
79-
80-
if setting_type == "int" then
81-
local default, min, max = remaining_line:match("^"
82-
-- first int is required, the last 2 are optional
83-
.. "(" .. CHAR_CLASSES.INTEGER .. "+)" .. CHAR_CLASSES.SPACE .. "?"
84-
.. "(" .. CHAR_CLASSES.INTEGER .. "*)" .. CHAR_CLASSES.SPACE .. "?"
85-
.. "(" .. CHAR_CLASSES.INTEGER .. "*)"
86-
.. "$")
87-
if default and tonumber(default) then
88-
min = tonumber(min)
89-
max = tonumber(max)
90-
table.insert(settings, {
91-
name = name,
92-
readable_name = readable_name,
93-
type = "int",
94-
default = default,
95-
min = min,
96-
max = max,
97-
comment = settings.current_comment,
98-
})
99-
else
100-
error_msg = "Invalid integer setting"
101-
end
10299

103-
elseif setting_type == "string" or setting_type == "noise_params"
104-
or setting_type == "key" then
105-
local default = remaining_line:match("^(.*)$")
106-
if default then
107-
if setting_type ~= "key" or read_all then -- ignore key type if read_all is false
108-
table.insert(settings, {
109-
name = name,
110-
readable_name = readable_name,
111-
type = setting_type,
112-
default = default,
113-
comment = settings.current_comment,
114-
})
115-
end
116-
else
117-
error_msg = "Invalid string setting"
118-
end
100+
min = tonumber(min)
101+
max = tonumber(max)
102+
table.insert(settings, {
103+
name = name,
104+
readable_name = readable_name,
105+
type = "int",
106+
default = default,
107+
min = min,
108+
max = max,
109+
comment = settings.current_comment,
110+
})
111+
return
112+
end
119113

120-
elseif setting_type == "bool" then
121-
if remaining_line == "false" or remaining_line == "true" then
122-
table.insert(settings, {
123-
name = name,
124-
readable_name = readable_name,
125-
type = "bool",
126-
default = remaining_line,
127-
comment = settings.current_comment,
128-
})
129-
else
130-
error_msg = "Invalid boolean setting"
131-
end
114+
if setting_type == "string" or setting_type == "noise_params"
115+
or setting_type == "key" then
116+
local default = remaining_line:match("^(.*)$")
132117

133-
elseif setting_type == "float" then
134-
local default, min, max = remaining_line:match("^"
135-
-- first float is required, the last 2 are optional
136-
.. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "?"
137-
.. "(" .. CHAR_CLASSES.FLOAT .. "*)" .. CHAR_CLASSES.SPACE .. "?"
138-
.. "(" .. CHAR_CLASSES.FLOAT .. "*)"
139-
.."$")
140-
if default and tonumber(default) then
141-
min = tonumber(min)
142-
max = tonumber(max)
143-
table.insert(settings, {
144-
name = name,
145-
readable_name = readable_name,
146-
type = "float",
147-
default = default,
148-
min = min,
149-
max = max,
150-
comment = settings.current_comment,
151-
})
152-
else
153-
error_msg = "Invalid float setting"
154-
end
118+
if not default then
119+
return "Invalid string setting"
120+
end
121+
if setting_type == "key" and not read_all then
122+
-- ignore key type if read_all is false
123+
return
124+
end
155125

156-
elseif setting_type == "enum" then
157-
local default, values = remaining_line:match("^(.+)" .. CHAR_CLASSES.SPACE .. "(.+)$")
158-
if default and values ~= "" then
159-
table.insert(settings, {
160-
name = name,
161-
readable_name = readable_name,
162-
type = "enum",
163-
default = default,
164-
values = values:split(",", true),
165-
comment = settings.current_comment,
166-
})
167-
else
168-
error_msg = "Invalid enum setting"
169-
end
126+
table.insert(settings, {
127+
name = name,
128+
readable_name = readable_name,
129+
type = setting_type,
130+
default = default,
131+
comment = settings.current_comment,
132+
})
133+
return
134+
end
170135

171-
elseif setting_type == "path" then
172-
local default = remaining_line:match("^(.*)$")
173-
if default then
174-
table.insert(settings, {
175-
name = name,
176-
readable_name = readable_name,
177-
type = "path",
178-
default = default,
179-
comment = settings.current_comment,
180-
})
181-
else
182-
error_msg = "Invalid path setting"
183-
end
136+
if setting_type == "bool" then
137+
if remaining_line ~= "false" and remaining_line ~= "true" then
138+
return "Invalid boolean setting"
139+
end
184140

185-
elseif setting_type == "flags" then
186-
local default, possible = remaining_line:match("^"
187-
.. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. ""
188-
.. "(" .. CHAR_CLASSES.FLAGS .. "+)"
189-
.. "$")
190-
if default and possible then
191-
table.insert(settings, {
192-
name = name,
193-
readable_name = readable_name,
194-
type = "flags",
195-
default = default,
196-
possible = possible,
197-
comment = settings.current_comment,
198-
})
199-
else
200-
error_msg = "Invalid flags setting"
201-
end
141+
table.insert(settings, {
142+
name = name,
143+
readable_name = readable_name,
144+
type = "bool",
145+
default = remaining_line,
146+
comment = settings.current_comment,
147+
})
148+
return
149+
end
202150

203-
-- TODO: flags, noise_params (, struct)
151+
if setting_type == "float" then
152+
local default, min, max = remaining_line:match("^"
153+
-- first float is required, the last 2 are optional
154+
.. "(" .. CHAR_CLASSES.FLOAT .. "+)" .. CHAR_CLASSES.SPACE .. "?"
155+
.. "(" .. CHAR_CLASSES.FLOAT .. "*)" .. CHAR_CLASSES.SPACE .. "?"
156+
.. "(" .. CHAR_CLASSES.FLOAT .. "*)"
157+
.."$")
204158

205-
else
206-
error_msg = "Invalid setting type \"" .. type .. "\""
159+
if not default or not tonumber(default) then
160+
return "Invalid float setting"
207161
end
208-
else
209-
error_msg = "Invalid line"
162+
163+
min = tonumber(min)
164+
max = tonumber(max)
165+
table.insert(settings, {
166+
name = name,
167+
readable_name = readable_name,
168+
type = "float",
169+
default = default,
170+
min = min,
171+
max = max,
172+
comment = settings.current_comment,
173+
})
174+
return
175+
end
176+
177+
if setting_type == "enum" then
178+
local default, values = remaining_line:match("^(.+)" .. CHAR_CLASSES.SPACE .. "(.+)$")
179+
180+
if not default or values == "" then
181+
return "Invalid enum setting"
182+
end
183+
184+
table.insert(settings, {
185+
name = name,
186+
readable_name = readable_name,
187+
type = "enum",
188+
default = default,
189+
values = values:split(",", true),
190+
comment = settings.current_comment,
191+
})
192+
return
193+
end
194+
195+
if setting_type == "path" then
196+
local default = remaining_line:match("^(.*)$")
197+
198+
if not default then
199+
return "Invalid path setting"
200+
end
201+
202+
table.insert(settings, {
203+
name = name,
204+
readable_name = readable_name,
205+
type = "path",
206+
default = default,
207+
comment = settings.current_comment,
208+
})
209+
return
210+
end
211+
212+
if setting_type == "flags" then
213+
local default, possible = remaining_line:match("^"
214+
.. "(" .. CHAR_CLASSES.FLAGS .. "+)" .. CHAR_CLASSES.SPACE .. ""
215+
.. "(" .. CHAR_CLASSES.FLAGS .. "+)"
216+
.. "$")
217+
218+
if not default or not possible then
219+
return "Invalid flags setting"
220+
end
221+
222+
table.insert(settings, {
223+
name = name,
224+
readable_name = readable_name,
225+
type = "flags",
226+
default = default,
227+
possible = possible,
228+
comment = settings.current_comment,
229+
})
230+
return
210231
end
211-
-- clear current_comment since we just used it
212-
-- if we not just used it, then clear it since we only want comments
213-
-- directly above the setting to be bound to it
214-
settings.current_comment = ""
215232

216-
return error_msg
233+
return "Invalid setting type \"" .. setting_type .. "\""
217234
end
218235

219-
local function parse_single_file(file, filepath, read_all, result, base_level)
236+
local function parse_single_file(file, filepath, read_all, result, base_level, allow_secure)
220237
-- store this helper variable in the table so it's easier to pass to parse_setting_line()
221238
result.current_comment = ""
222239

223240
local line = file:read("*line")
224241
while line do
225-
local error_msg = parse_setting_line(result, line, read_all, base_level)
242+
local error_msg = parse_setting_line(result, line, read_all, base_level, allow_secure)
226243
if error_msg then
227244
core.log("error", error_msg .. " in " .. filepath .. " \"" .. line .. "\"")
228245
end
@@ -243,7 +260,7 @@ local function parse_config_file(read_all, parse_mods)
243260
return settings
244261
end
245262

246-
parse_single_file(file, builtin_path, read_all, settings, 0)
263+
parse_single_file(file, builtin_path, read_all, settings, 0, true)
247264

248265
file:close()
249266

@@ -272,7 +289,7 @@ local function parse_config_file(read_all, parse_mods)
272289
type = "category",
273290
})
274291

275-
parse_single_file(file, path, read_all, settings, 2)
292+
parse_single_file(file, path, read_all, settings, 2, false)
276293

277294
file:close()
278295
end
@@ -305,7 +322,7 @@ local function parse_config_file(read_all, parse_mods)
305322
type = "category",
306323
})
307324

308-
parse_single_file(file, path, read_all, settings, 2)
325+
parse_single_file(file, path, read_all, settings, 2, false)
309326

310327
file:close()
311328
end

0 commit comments

Comments
 (0)
Please sign in to comment.