Skip to content

Commit b4d21e7

Browse files
committed
prevent API options from being changed via API
1 parent d722d6d commit b4d21e7

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

Diff for: modules/api/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def set_config(self, req: Dict[str, Any]):
570570
raise RuntimeError(f"model {checkpoint_name!r} not found")
571571

572572
for k, v in req.items():
573-
shared.opts.set(k, v)
573+
shared.opts.set(k, v, is_api=True)
574574

575575
shared.opts.save(shared.config_filename)
576576
return

Diff for: modules/options.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class OptionInfo:
11-
def __init__(self, default=None, label="", component=None, component_args=None, onchange=None, section=None, refresh=None, comment_before='', comment_after='', infotext=None):
11+
def __init__(self, default=None, label="", component=None, component_args=None, onchange=None, section=None, refresh=None, comment_before='', comment_after='', infotext=None, restrict_api=False):
1212
self.default = default
1313
self.label = label
1414
self.component = component
@@ -26,6 +26,9 @@ def __init__(self, default=None, label="", component=None, component_args=None,
2626

2727
self.infotext = infotext
2828

29+
self.restrict_api = restrict_api
30+
"""If True, the setting will not be accessible via API"""
31+
2932
def link(self, label, url):
3033
self.comment_before += f"[<a href='{url}' target='_blank'>{label}</a>]"
3134
return self
@@ -71,7 +74,7 @@ def options_section(section_identifier, options_dict):
7174
class Options:
7275
typemap = {int: float}
7376

74-
def __init__(self, data_labels, restricted_opts):
77+
def __init__(self, data_labels: dict[str, OptionInfo], restricted_opts):
7578
self.data_labels = data_labels
7679
self.data = {k: v.default for k, v in self.data_labels.items()}
7780
self.restricted_opts = restricted_opts
@@ -113,24 +116,28 @@ def __getattr__(self, item):
113116

114117
return super(Options, self).__getattribute__(item)
115118

116-
def set(self, key, value):
119+
def set(self, key, value, is_api=False):
117120
"""sets an option and calls its onchange callback, returning True if the option changed and False otherwise"""
118121

119122
oldval = self.data.get(key, None)
120123
if oldval == value:
121124
return False
122125

123-
if self.data_labels[key].do_not_save:
126+
option = self.data_labels[key]
127+
if option.do_not_save:
128+
return False
129+
130+
if is_api and option.restrict_api:
124131
return False
125132

126133
try:
127134
setattr(self, key, value)
128135
except RuntimeError:
129136
return False
130137

131-
if self.data_labels[key].onchange is not None:
138+
if option.onchange is not None:
132139
try:
133-
self.data_labels[key].onchange()
140+
option.onchange()
134141
except Exception as e:
135142
errors.display(e, f"changing setting {key} to {value}")
136143
setattr(self, key, oldval)

Diff for: modules/shared_options.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
}))
113113

114114
options_templates.update(options_section(('API', "API"), {
115-
"api_enable_requests": OptionInfo(True, "Allow http:// and https:// URLs for input images in API"),
116-
"api_forbid_local_requests": OptionInfo(True, "Forbid URLs to local resources"),
117-
"api_useragent": OptionInfo("", "User agent for requests"),
115+
"api_enable_requests": OptionInfo(True, "Allow http:// and https:// URLs for input images in API", restrict_api=True),
116+
"api_forbid_local_requests": OptionInfo(True, "Forbid URLs to local resources", restrict_api=True),
117+
"api_useragent": OptionInfo("", "User agent for requests", restrict_api=True),
118118
}))
119119

120120
options_templates.update(options_section(('training', "Training"), {

0 commit comments

Comments
 (0)