Harden env save against empty numerics; default coin margin and hedge off.
Reject blank float/int/select on save and parse env with safe helpers so options config edits cannot crash into 502; default OKX_TRADE_MODE=options and OKX_OPTIONS_MARGIN_MODE=coin. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Vendored
+54
@@ -378,6 +378,32 @@ def validate_env_updates(groups: list[dict], updates: dict[str, str]) -> tuple[d
|
||||
allowed[field["key"]] = field
|
||||
clean: dict[str, str] = {}
|
||||
errors: list[str] = []
|
||||
|
||||
def _looks_numeric_key(k: str) -> bool:
|
||||
u = (k or "").upper()
|
||||
return any(
|
||||
s in u
|
||||
for s in (
|
||||
"_BUFFER",
|
||||
"_RATIO",
|
||||
"_PERCENT",
|
||||
"_SECONDS",
|
||||
"_HOURS",
|
||||
"_MINUTES",
|
||||
"_LIMIT",
|
||||
"_AMOUNT",
|
||||
"_BUDGET",
|
||||
"_CAP_",
|
||||
"_MAX_",
|
||||
"_MIN_",
|
||||
"LEVERAGE",
|
||||
"_DTE_",
|
||||
"_USDT",
|
||||
"_USDC",
|
||||
"RISK_PERCENT",
|
||||
)
|
||||
)
|
||||
|
||||
for key, value in (updates or {}).items():
|
||||
if key not in allowed:
|
||||
errors.append(f"未知配置项: {key}")
|
||||
@@ -392,6 +418,17 @@ def validate_env_updates(groups: list[dict], updates: dict[str, str]) -> tuple[d
|
||||
errors.append(f"{key} 长度异常,疑似自动填充;留空则不修改已有密钥")
|
||||
continue
|
||||
ftype = allowed[key].get("type")
|
||||
if val == "":
|
||||
# 空值写入会导致 float('') / int('') 启动崩溃(曾出现 502)
|
||||
if ftype in ("float", "int") or _looks_numeric_key(key):
|
||||
errors.append(f"{key} 不能为空,请填写数字")
|
||||
continue
|
||||
if ftype == "select" or key in SELECT_OPTIONS:
|
||||
errors.append(f"{key} 不能为空,请从下拉选择")
|
||||
continue
|
||||
if ftype == "bool":
|
||||
errors.append(f"{key} 不能为空,请选择 true/false")
|
||||
continue
|
||||
if ftype == "bool":
|
||||
low = val.lower()
|
||||
if low not in ("true", "false", "1", "0", "yes", "no", "on", "off"):
|
||||
@@ -412,6 +449,23 @@ def validate_env_updates(groups: list[dict], updates: dict[str, str]) -> tuple[d
|
||||
errors.append(f"{key} 须为: {labels}")
|
||||
continue
|
||||
val = norm
|
||||
elif ftype == "float" or (ftype != "int" and _looks_numeric_key(key) and "." in val):
|
||||
try:
|
||||
float(val)
|
||||
except ValueError:
|
||||
errors.append(f"{key} 须为数字")
|
||||
continue
|
||||
ftype = "float"
|
||||
elif ftype == "int" or _looks_numeric_key(key):
|
||||
try:
|
||||
# 允许 0.98 类缓冲写成 float 键名但值是 int 也可
|
||||
if "." in val:
|
||||
float(val)
|
||||
else:
|
||||
int(val)
|
||||
except ValueError:
|
||||
errors.append(f"{key} 须为数字")
|
||||
continue
|
||||
clean[key] = val
|
||||
return clean, errors
|
||||
|
||||
|
||||
Reference in New Issue
Block a user