Skip to content

Commit a36ecfe

Browse files
refactor: init secret_key
1 parent 51728ba commit a36ecfe

3 files changed

Lines changed: 48 additions & 4 deletions

File tree

docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ services:
2222
# MCP settings
2323
SERVER_IMAGE_HOST: http://YOUR_SERVE_IP:MCP_PORT/images/
2424
# Auth & Security
25-
SECRET_KEY: y5txe1mRmS_JpOrUzFzHEu-kIQn3lf7ll0AOv9DQh0s
25+
SECRET_KEY: ${SECRET_KEY:?Set a unique SECRET_KEY in .env before starting SQLBot}
2626
# CORS settings
2727
BACKEND_CORS_ORIGINS: "http://localhost,http://localhost:5173,https://localhost,https://localhost:5173"
2828
# Logging

installer/install.conf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ SQLBOT_DB_PASSWORD=Password123@pg
2222
# 其他配置
2323
## 普通用户默认密码
2424
SQLBOT_DEFAULT_PWD=SQLBot@123456
25-
## SQLBot Secret Key
26-
SQLBOT_SECRET_KEY=y5txe1mRmS_JpOrUzFzHEu-kIQn3lf7ll0AOv9DQh0s
25+
## SQLBot Secret Key(留空时安装程序会生成并持久化随机密钥)
26+
SQLBOT_SECRET_KEY=
2727
## Cross-Origin Resource Sharing (CORS) 设置
2828
SQLBOT_CORS_ORIGINS=http://localhost,http://localhost:5173,https://localhost,https://localhost:5173
2929
## 日志级别 DEBUG, INFO, WARNING, ERROR

installer/install.sh

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@ function log_content () {
2121
log "\t${1}"
2222
}
2323

24+
COMPROMISED_SECRET_KEY_SHA256='78682d15dcf21e49b462da00b3ab33355c095450e02e2b37ed601e08e1b799c5'
25+
26+
function secret_key_fingerprint() {
27+
printf '%s' "$1" | sha256sum | awk '{print $1}'
28+
}
29+
30+
function prepare_secret_key() {
31+
local current_fingerprint=''
32+
local secret_action='generated'
33+
34+
if [[ -n "${SQLBOT_SECRET_KEY:-}" ]]; then
35+
current_fingerprint=$(secret_key_fingerprint "${SQLBOT_SECRET_KEY}")
36+
fi
37+
38+
if [[ "${current_fingerprint}" == "${COMPROMISED_SECRET_KEY_SHA256}" ]]; then
39+
secret_action='rotated'
40+
SQLBOT_SECRET_KEY=''
41+
fi
42+
43+
if [[ -z "${SQLBOT_SECRET_KEY:-}" ]]; then
44+
SQLBOT_SECRET_KEY=$(
45+
LC_ALL=C tr -dc 'A-Za-z0-9_-' </dev/urandom | head -c 64
46+
)
47+
export SQLBOT_SECRET_KEY
48+
49+
if [[ "${#SQLBOT_SECRET_KEY}" -ne 64 ]]; then
50+
log_content "生成 SQLBot Secret Key 失败"
51+
exit 1
52+
fi
53+
54+
if [[ "${secret_action}" == 'rotated' ]]; then
55+
log_content "检测到已公开的 SQLBot Secret Key,已自动轮换,现有登录会话将失效"
56+
else
57+
log_content "已生成新的 SQLBot Secret Key"
58+
fi
59+
elif [[ "${#SQLBOT_SECRET_KEY}" -lt 32 ]]; then
60+
log_content "SQLBOT_SECRET_KEY 长度不能少于 32 个字符"
61+
exit 1
62+
fi
63+
}
64+
2465
function check_and_prepare_env_params() {
2566
log "当前时间 : $(date)"
2667
log_title "检查安装环境并初始化环境变量"
@@ -56,6 +97,7 @@ function check_and_prepare_env_params() {
5697
mkdir -p ${SQLBOT_BASE}
5798
log_content "全新安装"
5899
fi
100+
prepare_secret_key
59101
set +a
60102
}
61103

@@ -281,4 +323,6 @@ function main() {
281323
start_sqlbot
282324
}
283325
284-
main
326+
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
327+
main
328+
fi

0 commit comments

Comments
 (0)