@intra-mart/accel 0.3.1 → 0.4.0-dev.202607170346
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -12
- package/assets/githooks/.githooks/.gitattributes +2 -0
- package/assets/githooks/.githooks/pre-commit/_meta.json +24 -0
- package/assets/githooks/.githooks/pre-commit/pre-commit_en +268 -0
- package/assets/githooks/.githooks/pre-commit/pre-commit_ja +255 -0
- package/assets/githooks/.githooks/pre-commit/pre-commit_zh_CN +255 -0
- package/dist/asset/asset-tree.d.ts +5 -0
- package/dist/asset/asset-tree.js +7 -0
- package/dist/asset/deployer.d.ts +2 -1
- package/dist/asset/deployer.js +28 -59
- package/dist/asset/file-provider.js +19 -18
- package/dist/asset/git-provider.d.ts +14 -0
- package/dist/asset/git-provider.js +163 -0
- package/dist/asset/installer.d.ts +6 -0
- package/dist/asset/installer.js +31 -0
- package/dist/asset/renderer.d.ts +2 -0
- package/dist/asset/renderer.js +22 -0
- package/dist/asset/types.d.ts +1 -1
- package/dist/asset/updater.d.ts +52 -0
- package/dist/asset/updater.js +297 -0
- package/dist/asset/walker.d.ts +4 -2
- package/dist/asset/walker.js +74 -45
- package/dist/commands/asset-provider.d.ts +7 -0
- package/dist/commands/asset-provider.js +9 -0
- package/dist/commands/attach.d.ts +0 -4
- package/dist/commands/attach.js +11 -20
- package/dist/commands/deploy.d.ts +11 -6
- package/dist/commands/deploy.js +77 -23
- package/dist/commands/init.d.ts +0 -4
- package/dist/commands/init.js +11 -20
- package/dist/commands/update.d.ts +86 -0
- package/dist/commands/update.js +448 -0
- package/dist/core/constants.d.ts +3 -3
- package/dist/core/constants.js +4 -2
- package/dist/core/types.d.ts +13 -1
- package/dist/deploy/api-client.d.ts +6 -2
- package/dist/deploy/api-client.js +8 -4
- package/dist/deploy/deploy-files.d.ts +20 -0
- package/dist/deploy/deploy-files.js +37 -0
- package/dist/git/errors.d.ts +6 -0
- package/dist/git/errors.js +9 -0
- package/dist/git/http.d.ts +3 -0
- package/dist/git/http.js +58 -0
- package/dist/git/object-store.d.ts +13 -0
- package/dist/git/object-store.js +79 -0
- package/dist/git/pkt-line.d.ts +13 -0
- package/dist/git/pkt-line.js +41 -0
- package/dist/git/smart-http.d.ts +18 -0
- package/dist/git/smart-http.js +109 -0
- package/dist/i18n/en.js +53 -6
- package/dist/i18n/ja.js +53 -6
- package/dist/i18n/zh_CN.js +53 -6
- package/dist/index.js +4 -1
- package/dist/interactive/progress.js +1 -0
- package/dist/interactive/prompts.d.ts +7 -1
- package/dist/interactive/prompts.js +63 -64
- package/dist/markdown/section-replacement.js +34 -8
- package/dist/utils/args.d.ts +1 -0
- package/dist/utils/args.js +4 -0
- package/dist/utils/fs-list.d.ts +1 -0
- package/dist/utils/fs-list.js +11 -0
- package/dist/utils/githooks-sync.d.ts +1 -0
- package/dist/utils/githooks-sync.js +91 -0
- package/dist/utils/mcp-sync.js +6 -0
- package/dist/utils/merge-file.d.ts +6 -0
- package/dist/utils/merge-file.js +20 -0
- package/package.json +8 -9
- package/dist/asset/github-provider.d.ts +0 -2
- package/dist/asset/github-provider.js +0 -37
- package/dist/asset/local-provider.d.ts +0 -2
- package/dist/asset/local-provider.js +0 -24
- package/dist/utils/archive.d.ts +0 -3
- package/dist/utils/archive.js +0 -20
- package/dist/utils/https.d.ts +0 -2
- package/dist/utils/https.js +0 -44
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# @accel-cli-managed
|
|
3
|
+
#
|
|
4
|
+
# pre-commit:
|
|
5
|
+
# git にステージされた変更内容に Authentication ヘッダや API キーに類似した文字列が
|
|
6
|
+
# コード上に含まれていないかチェックし、検出時はコミットをブロックする hook スクリプト。
|
|
7
|
+
#
|
|
8
|
+
# 有効化方法:
|
|
9
|
+
# chmod +x .githooks/pre-commit
|
|
10
|
+
# git config core.hooksPath .githooks
|
|
11
|
+
#
|
|
12
|
+
set -euo pipefail
|
|
13
|
+
shopt -s nocasematch
|
|
14
|
+
|
|
15
|
+
# =============================================================================
|
|
16
|
+
# 変更内容の差分を取得
|
|
17
|
+
# =============================================================================
|
|
18
|
+
|
|
19
|
+
# ファイル数分 git diff を呼ぶと大量ファイル変更時に著しく遅くなるため、差分取得は 1回にまとめる。
|
|
20
|
+
# 検出対象は以下に限定するため、git diff の対象ファイルをあらかじめ絞り込む。
|
|
21
|
+
# - intra-mart OAuth クライアントキーが記載される mcp 設定ファイル(.json / .toml)
|
|
22
|
+
diff_output=$(git diff --cached -U0 --no-color --diff-filter=ACMR -- '*.json' '*.toml' 2>/dev/null | tr -d '\0' || true)
|
|
23
|
+
if [ -z "$diff_output" ]; then
|
|
24
|
+
exit 0
|
|
25
|
+
fi
|
|
26
|
+
|
|
27
|
+
found=0
|
|
28
|
+
findings=()
|
|
29
|
+
|
|
30
|
+
# =============================================================================
|
|
31
|
+
# base64 デコード関数
|
|
32
|
+
# =============================================================================
|
|
33
|
+
|
|
34
|
+
# Git Bash (Windows) 等 fork/exec が高コストな環境では、
|
|
35
|
+
# 候補トークン数に比例してプロセス起動が積み重なり、チェック動作が遅くなるため、
|
|
36
|
+
# base64 デコードは外部プロセス(base64/tr)を起動せず bash 組み込み演算のみで行う。
|
|
37
|
+
# macOS 標準の /bin/bash 3.2 でも動作させるため、
|
|
38
|
+
# 連想配列(bash 4.0+)は使わず、文字列検索で文字から値へ変換する。
|
|
39
|
+
B64_ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
40
|
+
|
|
41
|
+
# 指定した base64 文字の 6bit 値を変数 B64_LAST_VAL にセットする。
|
|
42
|
+
# 見つからない場合は B64_LAST_VAL を空にして 1 を返却する。
|
|
43
|
+
_b64_char_value() {
|
|
44
|
+
case "$1" in
|
|
45
|
+
-) B64_LAST_VAL=62; return 0 ;;
|
|
46
|
+
_) B64_LAST_VAL=63; return 0 ;;
|
|
47
|
+
esac
|
|
48
|
+
local idx="${B64_ALPHABET%%"$1"*}"
|
|
49
|
+
if [ "$idx" = "$B64_ALPHABET" ]; then
|
|
50
|
+
B64_LAST_VAL=""
|
|
51
|
+
return 1
|
|
52
|
+
fi
|
|
53
|
+
B64_LAST_VAL=${#idx}
|
|
54
|
+
return 0
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
# トークン文字列を base64(urlsafe も許容)としてデコードし、
|
|
58
|
+
# intra-mart OAuth クライアントキー(16 進文字列 + '@' + テナントID)で使われる英数字+記号で
|
|
59
|
+
# 構成される場合に true を返却する。
|
|
60
|
+
decode_contains_at() {
|
|
61
|
+
local token="$1"
|
|
62
|
+
local len=${#token}
|
|
63
|
+
# ループ内で毎回 local 宣言をやり直すコストを避けるため、変数は先に一括宣言する
|
|
64
|
+
local i=0 has_at=1 c0 c1 c2 c3 v0 v1 v2 v3 nbytes val b1 b2 b3
|
|
65
|
+
|
|
66
|
+
while (( i < len )); do
|
|
67
|
+
# base64 は「4文字(6bit×4)」から「3バイト(8bit×3)」を復元する仕組みのため、
|
|
68
|
+
# 先頭から4文字ずつ切り出して処理する。
|
|
69
|
+
c0="${token:i:1}"; c1="${token:i+1:1}"; c2="${token:i+2:1}"; c3="${token:i+3:1}"
|
|
70
|
+
[[ -z "$c0" || -z "$c1" ]] && break
|
|
71
|
+
|
|
72
|
+
# 各文字を base64 アルファベット上の位置(6bit の値, 0〜63)に変換する
|
|
73
|
+
_b64_char_value "$c0" || return 1
|
|
74
|
+
v0=$B64_LAST_VAL
|
|
75
|
+
_b64_char_value "$c1" || return 1
|
|
76
|
+
v1=$B64_LAST_VAL
|
|
77
|
+
|
|
78
|
+
# 3, 4文字目がパディング("=")または存在しない場合、
|
|
79
|
+
# このグループから復元できるバイト数は 3 未満になる。
|
|
80
|
+
# (base64 は末尾グループのみ 2 バイト・1 バイト復元になる可能性がある)
|
|
81
|
+
nbytes=3; v2=0; v3=0
|
|
82
|
+
if [[ -z "$c2" || "$c2" == "=" ]]; then
|
|
83
|
+
nbytes=1
|
|
84
|
+
else
|
|
85
|
+
_b64_char_value "$c2" || return 1
|
|
86
|
+
v2=$B64_LAST_VAL
|
|
87
|
+
if [[ -z "$c3" || "$c3" == "=" ]]; then
|
|
88
|
+
nbytes=2
|
|
89
|
+
else
|
|
90
|
+
_b64_char_value "$c3" || return 1
|
|
91
|
+
v3=$B64_LAST_VAL
|
|
92
|
+
fi
|
|
93
|
+
fi
|
|
94
|
+
|
|
95
|
+
# 4つの 6bit 値(v0〜v3, 計 24bit)を1つの整数に結合し、
|
|
96
|
+
# 8bit ずつ 3 つに割り直すことで元のバイト列を復元する。
|
|
97
|
+
# 6bit×4 = [v0][v1][v2][v3] → 8bit×3 = [b1][b2][b3]
|
|
98
|
+
val=$(( (v0 * 262144) + (v1 * 4096) + (v2 * 64) + v3 ))
|
|
99
|
+
b1=$(( (val / 65536) % 256 ))
|
|
100
|
+
|
|
101
|
+
# intra-mart OAuth クライアントキーで使われないバイト値が含まれている場合は、
|
|
102
|
+
# 誤検知を防止するために、判定をスキップする。
|
|
103
|
+
(( (b1 >= 48 && b1 <= 57) || (b1 >= 65 && b1 <= 90) || (b1 >= 97 && b1 <= 122) || b1 == 64 || b1 == 95 || b1 == 45 )) || return 1
|
|
104
|
+
|
|
105
|
+
# 1バイト目が "@"(10 進数で 64)であれば、
|
|
106
|
+
# intra-mart OAuth クライアントキーである可能性があるため、
|
|
107
|
+
# has_at を 0 にする。
|
|
108
|
+
(( b1 == 64 )) && has_at=0
|
|
109
|
+
|
|
110
|
+
# 2バイト目・3バイト目についても b1 と同様の判定を行う。
|
|
111
|
+
# ただし、このグループから復元できるのが 1 バイトのみ(nbytes=1)の場合は、
|
|
112
|
+
# b2・b3 は存在しないため、nbytes に応じて判定自体をスキップする。
|
|
113
|
+
if (( nbytes >= 2 )); then
|
|
114
|
+
b2=$(( (val / 256) % 256 ))
|
|
115
|
+
(( (b2 >= 48 && b2 <= 57) || (b2 >= 65 && b2 <= 90) || (b2 >= 97 && b2 <= 122) || b2 == 64 || b2 == 95 || b2 == 45 )) || return 1
|
|
116
|
+
(( b2 == 64 )) && has_at=0
|
|
117
|
+
fi
|
|
118
|
+
if (( nbytes >= 3 )); then
|
|
119
|
+
b3=$(( val % 256 ))
|
|
120
|
+
(( (b3 >= 48 && b3 <= 57) || (b3 >= 65 && b3 <= 90) || (b3 >= 97 && b3 <= 122) || b3 == 64 || b3 == 95 || b3 == 45 )) || return 1
|
|
121
|
+
(( b3 == 64 )) && has_at=0
|
|
122
|
+
fi
|
|
123
|
+
|
|
124
|
+
# 3バイトに満たないグループが出た時点で、それがトークンの末尾になる(パディング済み)。
|
|
125
|
+
# そのため、ループを抜ける。それ以外の場合は、次の4文字へ進む。
|
|
126
|
+
(( nbytes < 3 )) && break
|
|
127
|
+
i=$((i + 4))
|
|
128
|
+
done
|
|
129
|
+
|
|
130
|
+
# トークン全体を最後まで走査できた場合は、文字列が英数字+記号で構成される。
|
|
131
|
+
# その中で、"@" が含まれていた場合は、has_at=0(成功)となり、その値を戻り値として返却する。
|
|
132
|
+
return "$has_at"
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
# =============================================================================
|
|
136
|
+
# 追加行の base64 風トークンをチェックする関数
|
|
137
|
+
# =============================================================================
|
|
138
|
+
|
|
139
|
+
# 同一トークンの再デコードを避けるための判定結果キャッシュ。
|
|
140
|
+
# 検出対象の文字列を decode_contains_at() に渡した結果を、トークン文字列ごとにキャッシュする。
|
|
141
|
+
# 連想配列(bash 4.0+)は使用しない。
|
|
142
|
+
TOKENS_MATCHED=$'\x01'
|
|
143
|
+
TOKENS_UNMATCHED=$'\x01'
|
|
144
|
+
|
|
145
|
+
# 追加行 1行から 40文字以上の base64 風トークンを正規表現ですべて抜き出し、
|
|
146
|
+
# 各トークンを decode_contains_at() に渡して intra-mart OAuth クライアントキー形式かどうかを判定する。
|
|
147
|
+
# 該当すれば findings に検出内容を追記し found=1 をセットする。
|
|
148
|
+
check_base64_tokens() {
|
|
149
|
+
local file="$1" lineno="$2" content="$3"
|
|
150
|
+
# 40 文字未満なら候補トークンは存在しないため、処理を終了する。
|
|
151
|
+
(( ${#content} < 40 )) && return
|
|
152
|
+
local remaining="$content" token is_match
|
|
153
|
+
|
|
154
|
+
# 行ごとに grep サブプロセスを起動すると、大量ファイル変更時に著しく遅くなるため、
|
|
155
|
+
# bash 組み込みの正規表現マッチのみで候補トークンを抽出する。
|
|
156
|
+
while [[ "$remaining" =~ ([A-Za-z0-9+/_-]{40,}={0,2}) ]]; do
|
|
157
|
+
token="${BASH_REMATCH[1]}"
|
|
158
|
+
remaining="${remaining#*"$token"}"
|
|
159
|
+
|
|
160
|
+
# 同じトークンが複数ファイル・複数行に出現するケース(同じ設定を複数箇所に
|
|
161
|
+
# コピーした場合等)があるため、判定結果はキャッシュを使い回しつつ、
|
|
162
|
+
# 出現箇所ごとに findings へ記録する。
|
|
163
|
+
if [[ "$TOKENS_MATCHED" == *$'\x01'"$token"$'\x01'* ]]; then
|
|
164
|
+
is_match=0
|
|
165
|
+
elif [[ "$TOKENS_UNMATCHED" == *$'\x01'"$token"$'\x01'* ]]; then
|
|
166
|
+
is_match=1
|
|
167
|
+
elif decode_contains_at "$token"; then
|
|
168
|
+
TOKENS_MATCHED+="$token"$'\x01'
|
|
169
|
+
is_match=0
|
|
170
|
+
else
|
|
171
|
+
TOKENS_UNMATCHED+="$token"$'\x01'
|
|
172
|
+
is_match=1
|
|
173
|
+
fi
|
|
174
|
+
|
|
175
|
+
if (( is_match == 0 )); then
|
|
176
|
+
findings+=("[$file:$lineno] intra-mart OAuth クライアントキー文字列を検出しました: ${token:0:12}...")
|
|
177
|
+
found=1
|
|
178
|
+
fi
|
|
179
|
+
done
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
# =============================================================================
|
|
183
|
+
# 差分の各行をチェック
|
|
184
|
+
# =============================================================================
|
|
185
|
+
|
|
186
|
+
# 検出対象は以下に限定するため、一部の拡張子のみを対象とする。
|
|
187
|
+
# - intra-mart OAuth クライアントキーが記載される mcp 設定ファイル(.json / .toml)
|
|
188
|
+
INCLUDE_FILE_RE='\.(json|toml)$'
|
|
189
|
+
|
|
190
|
+
# package-lock.json は大量のハッシュ文字列が認証キーと誤認されやすいため、
|
|
191
|
+
# スキャン対象から除外する。
|
|
192
|
+
EXCLUDE_FILE_RE='(^|/)package-lock\.json$'
|
|
193
|
+
|
|
194
|
+
current_file=""
|
|
195
|
+
current_line=0
|
|
196
|
+
is_binary=0
|
|
197
|
+
skip_file=0
|
|
198
|
+
|
|
199
|
+
while IFS= read -r line; do
|
|
200
|
+
case "$line" in
|
|
201
|
+
"diff --git "*)
|
|
202
|
+
# 例: diff --git a/path/to/file b/path/to/file
|
|
203
|
+
current_file="${line#diff --git a/}"
|
|
204
|
+
current_file="${current_file%% b/*}"
|
|
205
|
+
is_binary=0
|
|
206
|
+
if [[ "$current_file" =~ $INCLUDE_FILE_RE ]] && [[ ! "$current_file" =~ $EXCLUDE_FILE_RE ]]; then
|
|
207
|
+
skip_file=0
|
|
208
|
+
else
|
|
209
|
+
skip_file=1
|
|
210
|
+
fi
|
|
211
|
+
;;
|
|
212
|
+
"Binary files "*|"GIT binary patch"*)
|
|
213
|
+
is_binary=1
|
|
214
|
+
;;
|
|
215
|
+
"@@ "*)
|
|
216
|
+
# 例: @@ -12,0 +13,2 @@ ... → 新ファイル側の開始行番号 (13) を取得する
|
|
217
|
+
if [[ "$line" =~ \+([0-9]+) ]]; then
|
|
218
|
+
current_line="${BASH_REMATCH[1]}"
|
|
219
|
+
fi
|
|
220
|
+
;;
|
|
221
|
+
"+++ "*|"--- "*)
|
|
222
|
+
: # diff ヘッダ行は無視
|
|
223
|
+
;;
|
|
224
|
+
"+"*)
|
|
225
|
+
if (( is_binary == 1 || skip_file == 1 )); then
|
|
226
|
+
continue
|
|
227
|
+
fi
|
|
228
|
+
content="${line#+}"
|
|
229
|
+
check_base64_tokens "$current_file" "$current_line" "$content"
|
|
230
|
+
current_line=$((current_line + 1))
|
|
231
|
+
;;
|
|
232
|
+
*)
|
|
233
|
+
: ;;
|
|
234
|
+
esac
|
|
235
|
+
done <<< "$diff_output"
|
|
236
|
+
|
|
237
|
+
# =============================================================================
|
|
238
|
+
# 検出結果の表示
|
|
239
|
+
# =============================================================================
|
|
240
|
+
|
|
241
|
+
if [ "$found" -eq 1 ]; then
|
|
242
|
+
echo "----------------------------------------------------------------------"
|
|
243
|
+
echo "🚫 コミットを中止しました: 機密情報に類似する文字列が検出されました"
|
|
244
|
+
echo "----------------------------------------------------------------------"
|
|
245
|
+
for msg in "${findings[@]}"; do
|
|
246
|
+
echo " - $msg"
|
|
247
|
+
done
|
|
248
|
+
echo ""
|
|
249
|
+
echo "上記の箇所に、機密情報に類似する文字列が含まれている可能性があります。"
|
|
250
|
+
echo "コミット前に必ず内容を確認し、必要に応じて削除するか、他の文字列に置き換えてください。"
|
|
251
|
+
echo "検出内容に問題がない場合は、git commit --no-verify で回避してください。"
|
|
252
|
+
exit 1
|
|
253
|
+
fi
|
|
254
|
+
|
|
255
|
+
exit 0
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# @accel-cli-managed
|
|
3
|
+
#
|
|
4
|
+
# pre-commit:
|
|
5
|
+
# 扫描已暂存到 git 的变更内容中是否包含疑似敏感信息的字符串,
|
|
6
|
+
# 若检测到则阻止本次提交的 hook 脚本。
|
|
7
|
+
#
|
|
8
|
+
# 启用方法:
|
|
9
|
+
# chmod +x .githooks/pre-commit
|
|
10
|
+
# git config core.hooksPath .githooks
|
|
11
|
+
#
|
|
12
|
+
set -euo pipefail
|
|
13
|
+
shopt -s nocasematch
|
|
14
|
+
|
|
15
|
+
# =============================================================================
|
|
16
|
+
# 获取变更差异
|
|
17
|
+
# =============================================================================
|
|
18
|
+
|
|
19
|
+
# 如果按文件数逐个调用 git diff,在变更文件较多时会显著变慢,
|
|
20
|
+
# 因此差异获取统一合并为 1 次。
|
|
21
|
+
# 由于检测范围仅限于以下内容,因此通过 git 的路径规格预先筛选目标文件:
|
|
22
|
+
# - 记载 intra-mart OAuth 客户端密钥的 mcp 配置文件(.json / .toml)
|
|
23
|
+
diff_output=$(git diff --cached -U0 --no-color --diff-filter=ACMR -- '*.json' '*.toml' 2>/dev/null | tr -d '\0' || true)
|
|
24
|
+
if [ -z "$diff_output" ]; then
|
|
25
|
+
exit 0
|
|
26
|
+
fi
|
|
27
|
+
|
|
28
|
+
found=0
|
|
29
|
+
findings=()
|
|
30
|
+
|
|
31
|
+
# =============================================================================
|
|
32
|
+
# base64 解码函数
|
|
33
|
+
# =============================================================================
|
|
34
|
+
|
|
35
|
+
# 在 Git Bash (Windows) 等 fork/exec 成本较高的环境中,若按候选 token
|
|
36
|
+
# 数量逐一启动进程,检查动作将随候选数量增多而变慢,
|
|
37
|
+
# 因此 base64 解码不启动外部进程(base64/tr),仅使用 bash 内置运算完成。
|
|
38
|
+
# 为了在 macOS 系统自带的 /bin/bash 3.2 上也能运行,
|
|
39
|
+
# 不使用关联数组(bash 4.0+),改用字符串检索完成字符到数值的转换。
|
|
40
|
+
B64_ALPHABET="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
41
|
+
|
|
42
|
+
# 将指定 base64 字符对应的 6bit 数值写入变量 B64_LAST_VAL。
|
|
43
|
+
# 若找不到,则将 B64_LAST_VAL 置空并返回 1。
|
|
44
|
+
_b64_char_value() {
|
|
45
|
+
case "$1" in
|
|
46
|
+
-) B64_LAST_VAL=62; return 0 ;;
|
|
47
|
+
_) B64_LAST_VAL=63; return 0 ;;
|
|
48
|
+
esac
|
|
49
|
+
local idx="${B64_ALPHABET%%"$1"*}"
|
|
50
|
+
if [ "$idx" = "$B64_ALPHABET" ]; then
|
|
51
|
+
B64_LAST_VAL=""
|
|
52
|
+
return 1
|
|
53
|
+
fi
|
|
54
|
+
B64_LAST_VAL=${#idx}
|
|
55
|
+
return 0
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
# 将 token 字符串以 base64(同时兼容 urlsafe 字符集)解码,若解码结果
|
|
59
|
+
# 仅由 intra-mart OAuth 客户端密钥(16进制字符串 + '@' + 租户ID)所使用的
|
|
60
|
+
# 英数字与符号构成,则返回 true。
|
|
61
|
+
decode_contains_at() {
|
|
62
|
+
local token="$1"
|
|
63
|
+
local len=${#token}
|
|
64
|
+
# 为避免循环体内每次都重新执行 local 声明带来的开销,变量统一在循环外一次性声明
|
|
65
|
+
local i=0 has_at=1 c0 c1 c2 c3 v0 v1 v2 v3 nbytes val b1 b2 b3
|
|
66
|
+
|
|
67
|
+
while (( i < len )); do
|
|
68
|
+
# base64 是从「4个字符(6bit×4)」还原「3个字节(8bit×3)」的机制,
|
|
69
|
+
# 因此从头开始每次截取 4 个字符进行处理。
|
|
70
|
+
c0="${token:i:1}"; c1="${token:i+1:1}"; c2="${token:i+2:1}"; c3="${token:i+3:1}"
|
|
71
|
+
[[ -z "$c0" || -z "$c1" ]] && break
|
|
72
|
+
|
|
73
|
+
# 将每个字符转换为其在 base64 字母表中的位置(6bit 数值,0〜63)
|
|
74
|
+
_b64_char_value "$c0" || return 1
|
|
75
|
+
v0=$B64_LAST_VAL
|
|
76
|
+
_b64_char_value "$c1" || return 1
|
|
77
|
+
v1=$B64_LAST_VAL
|
|
78
|
+
|
|
79
|
+
# 若第 3、4 个字符是填充符("=")或不存在,
|
|
80
|
+
# 则该分组能还原的字节数会少于 3。
|
|
81
|
+
# (base64 仅末尾分组可能还原为 2 字节或 1 字节)
|
|
82
|
+
nbytes=3; v2=0; v3=0
|
|
83
|
+
if [[ -z "$c2" || "$c2" == "=" ]]; then
|
|
84
|
+
nbytes=1
|
|
85
|
+
else
|
|
86
|
+
_b64_char_value "$c2" || return 1
|
|
87
|
+
v2=$B64_LAST_VAL
|
|
88
|
+
if [[ -z "$c3" || "$c3" == "=" ]]; then
|
|
89
|
+
nbytes=2
|
|
90
|
+
else
|
|
91
|
+
_b64_char_value "$c3" || return 1
|
|
92
|
+
v3=$B64_LAST_VAL
|
|
93
|
+
fi
|
|
94
|
+
fi
|
|
95
|
+
|
|
96
|
+
# 将 4 个 6bit 数值(v0〜v3,共 24bit)合并为一个整数,
|
|
97
|
+
# 再按 8bit 拆分为 3 个字节,从而还原出原始字节序列。
|
|
98
|
+
# 6bit×4 = [v0][v1][v2][v3] → 8bit×3 = [b1][b2][b3]
|
|
99
|
+
val=$(( (v0 * 262144) + (v1 * 4096) + (v2 * 64) + v3 ))
|
|
100
|
+
b1=$(( (val / 65536) % 256 ))
|
|
101
|
+
|
|
102
|
+
# 若出现 intra-mart OAuth 客户端密钥中不会使用的字节值,
|
|
103
|
+
# 为避免误判,跳过后续判定。
|
|
104
|
+
(( (b1 >= 48 && b1 <= 57) || (b1 >= 65 && b1 <= 90) || (b1 >= 97 && b1 <= 122) || b1 == 64 || b1 == 95 || b1 == 45 )) || return 1
|
|
105
|
+
|
|
106
|
+
# 若第 1 个字节为 "@"(十进制 64),
|
|
107
|
+
# 则可能是 intra-mart OAuth 客户端密钥,
|
|
108
|
+
# 将 has_at 置为 0。
|
|
109
|
+
(( b1 == 64 )) && has_at=0
|
|
110
|
+
|
|
111
|
+
# 第 2、3 个字节也进行与 b1 相同的判定。
|
|
112
|
+
# 但若该分组只能还原 1 个字节(nbytes=1),
|
|
113
|
+
# 则 b2、b3 不存在,因此根据 nbytes 跳过判定本身。
|
|
114
|
+
if (( nbytes >= 2 )); then
|
|
115
|
+
b2=$(( (val / 256) % 256 ))
|
|
116
|
+
(( (b2 >= 48 && b2 <= 57) || (b2 >= 65 && b2 <= 90) || (b2 >= 97 && b2 <= 122) || b2 == 64 || b2 == 95 || b2 == 45 )) || return 1
|
|
117
|
+
(( b2 == 64 )) && has_at=0
|
|
118
|
+
fi
|
|
119
|
+
if (( nbytes >= 3 )); then
|
|
120
|
+
b3=$(( val % 256 ))
|
|
121
|
+
(( (b3 >= 48 && b3 <= 57) || (b3 >= 65 && b3 <= 90) || (b3 >= 97 && b3 <= 122) || b3 == 64 || b3 == 95 || b3 == 45 )) || return 1
|
|
122
|
+
(( b3 == 64 )) && has_at=0
|
|
123
|
+
fi
|
|
124
|
+
|
|
125
|
+
# 一旦出现还原字节数不足 3 的分组,说明该分组即为 token 的末尾(已填充)。
|
|
126
|
+
# 因此退出循环;否则继续处理下 4 个字符。
|
|
127
|
+
(( nbytes < 3 )) && break
|
|
128
|
+
i=$((i + 4))
|
|
129
|
+
done
|
|
130
|
+
|
|
131
|
+
# 能扫描到 token 末尾,说明该字符串仅由英数字与符号构成。
|
|
132
|
+
# 期间若发现 "@",则 has_at=0(成功),并将该值作为返回值返回。
|
|
133
|
+
return "$has_at"
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
# =============================================================================
|
|
137
|
+
# 检查添加行中 base64 风格 token 的函数
|
|
138
|
+
# =============================================================================
|
|
139
|
+
|
|
140
|
+
# 用于避免重复解码同一 token 的判定结果缓存。
|
|
141
|
+
# 以 token 字符串本身为键,缓存传给 decode_contains_at() 后得到的判定结果。
|
|
142
|
+
# 不使用关联数组(bash 4.0+)。
|
|
143
|
+
TOKENS_MATCHED=$'\x01'
|
|
144
|
+
TOKENS_UNMATCHED=$'\x01'
|
|
145
|
+
|
|
146
|
+
# 从每一行添加行中,用正则表达式提取所有 40 个字符以上的 base64 风格 token,
|
|
147
|
+
# 并将每个 token 传给 decode_contains_at() 判定是否为 intra-mart OAuth 客户端密钥格式。
|
|
148
|
+
# 命中则将检测内容追加到 findings 并设置 found=1。
|
|
149
|
+
check_base64_tokens() {
|
|
150
|
+
local file="$1" lineno="$2" content="$3"
|
|
151
|
+
# 不足 40 个字符则不可能存在候选 token,在进入正则循环前直接返回。
|
|
152
|
+
(( ${#content} < 40 )) && return
|
|
153
|
+
local remaining="$content" token is_match
|
|
154
|
+
|
|
155
|
+
# 若按行启动 grep 子进程,在变更文件较多时会显著变慢,
|
|
156
|
+
# 因此仅使用 bash 内置正则匹配提取候选 token。
|
|
157
|
+
while [[ "$remaining" =~ ([A-Za-z0-9+/_-]{40,}={0,2}) ]]; do
|
|
158
|
+
token="${BASH_REMATCH[1]}"
|
|
159
|
+
remaining="${remaining#*"$token"}"
|
|
160
|
+
|
|
161
|
+
# 同一 token 可能出现在多个文件或多行中(例如将同一配置复制到多处),
|
|
162
|
+
# 因此判定结果会复用缓存,但每次出现都会记录到 findings 中。
|
|
163
|
+
if [[ "$TOKENS_MATCHED" == *$'\x01'"$token"$'\x01'* ]]; then
|
|
164
|
+
is_match=0
|
|
165
|
+
elif [[ "$TOKENS_UNMATCHED" == *$'\x01'"$token"$'\x01'* ]]; then
|
|
166
|
+
is_match=1
|
|
167
|
+
elif decode_contains_at "$token"; then
|
|
168
|
+
TOKENS_MATCHED+="$token"$'\x01'
|
|
169
|
+
is_match=0
|
|
170
|
+
else
|
|
171
|
+
TOKENS_UNMATCHED+="$token"$'\x01'
|
|
172
|
+
is_match=1
|
|
173
|
+
fi
|
|
174
|
+
|
|
175
|
+
if (( is_match == 0 )); then
|
|
176
|
+
findings+=("[$file:$lineno] 检测到 intra-mart OAuth 客户端密钥字符串: ${token:0:12}...")
|
|
177
|
+
found=1
|
|
178
|
+
fi
|
|
179
|
+
done
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
# =============================================================================
|
|
183
|
+
# 检查差异中的每一行
|
|
184
|
+
# =============================================================================
|
|
185
|
+
|
|
186
|
+
# 由于检测范围仅限于以下内容,因此仅将下述扩展名作为检测对象。
|
|
187
|
+
# - 记载 intra-mart OAuth 客户端密钥的 mcp 配置文件(.json / .toml)
|
|
188
|
+
INCLUDE_FILE_RE='\.(json|toml)$'
|
|
189
|
+
|
|
190
|
+
# package-lock.json 中包含大量容易被误判为认证密钥的哈希类字符串,
|
|
191
|
+
# 因此从扫描对象中排除。
|
|
192
|
+
EXCLUDE_FILE_RE='(^|/)package-lock\.json$'
|
|
193
|
+
|
|
194
|
+
current_file=""
|
|
195
|
+
current_line=0
|
|
196
|
+
is_binary=0
|
|
197
|
+
skip_file=0
|
|
198
|
+
|
|
199
|
+
while IFS= read -r line; do
|
|
200
|
+
case "$line" in
|
|
201
|
+
"diff --git "*)
|
|
202
|
+
# 例如: diff --git a/path/to/file b/path/to/file
|
|
203
|
+
current_file="${line#diff --git a/}"
|
|
204
|
+
current_file="${current_file%% b/*}"
|
|
205
|
+
is_binary=0
|
|
206
|
+
if [[ "$current_file" =~ $INCLUDE_FILE_RE ]] && [[ ! "$current_file" =~ $EXCLUDE_FILE_RE ]]; then
|
|
207
|
+
skip_file=0
|
|
208
|
+
else
|
|
209
|
+
skip_file=1
|
|
210
|
+
fi
|
|
211
|
+
;;
|
|
212
|
+
"Binary files "*|"GIT binary patch"*)
|
|
213
|
+
is_binary=1
|
|
214
|
+
;;
|
|
215
|
+
"@@ "*)
|
|
216
|
+
# 例如: @@ -12,0 +13,2 @@ ... → 获取新文件一侧的起始行号(13)
|
|
217
|
+
if [[ "$line" =~ \+([0-9]+) ]]; then
|
|
218
|
+
current_line="${BASH_REMATCH[1]}"
|
|
219
|
+
fi
|
|
220
|
+
;;
|
|
221
|
+
"+++ "*|"--- "*)
|
|
222
|
+
: # 忽略 diff 头部行
|
|
223
|
+
;;
|
|
224
|
+
"+"*)
|
|
225
|
+
if (( is_binary == 1 || skip_file == 1 )); then
|
|
226
|
+
continue
|
|
227
|
+
fi
|
|
228
|
+
content="${line#+}"
|
|
229
|
+
check_base64_tokens "$current_file" "$current_line" "$content"
|
|
230
|
+
current_line=$((current_line + 1))
|
|
231
|
+
;;
|
|
232
|
+
*)
|
|
233
|
+
: ;;
|
|
234
|
+
esac
|
|
235
|
+
done <<< "$diff_output"
|
|
236
|
+
|
|
237
|
+
# =============================================================================
|
|
238
|
+
# 显示检测结果
|
|
239
|
+
# =============================================================================
|
|
240
|
+
|
|
241
|
+
if [ "$found" -eq 1 ]; then
|
|
242
|
+
echo "----------------------------------------------------------------------"
|
|
243
|
+
echo "🚫 已阻止提交:检测到疑似敏感信息的字符串"
|
|
244
|
+
echo "----------------------------------------------------------------------"
|
|
245
|
+
for msg in "${findings[@]}"; do
|
|
246
|
+
echo " - $msg"
|
|
247
|
+
done
|
|
248
|
+
echo ""
|
|
249
|
+
echo "以上位置可能包含疑似敏感信息的字符串。"
|
|
250
|
+
echo "请在提交前务必确认内容,并按需删除或替换为其他字符串。"
|
|
251
|
+
echo "如确认检测内容没有问题,可使用 git commit --no-verify 跳过本次检查。"
|
|
252
|
+
exit 1
|
|
253
|
+
fi
|
|
254
|
+
|
|
255
|
+
exit 0
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { listFilesUnder } from "../utils/fs-list.js";
|
|
4
|
+
export const createFsAssetTree = (rootDir) => ({
|
|
5
|
+
listFiles: () => listFilesUnder(rootDir),
|
|
6
|
+
readFile: (relPath) => readFile(join(rootDir, relPath), "utf-8"),
|
|
7
|
+
});
|
package/dist/asset/deployer.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import type { AssetProvider, AccelSettings, HashsumEntry } from "../core/types.js";
|
|
2
|
+
export { getInstallCmds } from "./installer.js";
|
|
2
3
|
export type OverwriteDecision = "overwrite" | "skip";
|
|
3
4
|
export type ExistingFileHandler = (relativePath: string) => Promise<OverwriteDecision>;
|
|
4
5
|
export type ProgressCallbacks = {
|
|
5
6
|
onFetchStart?: () => void;
|
|
7
|
+
onDownloadStart?: () => void;
|
|
6
8
|
onSectionDeploy?: (section: string, files: string[]) => void;
|
|
7
9
|
onInstallStart?: (command: string) => void;
|
|
8
10
|
onBuildStart?: (command: string) => void;
|
|
@@ -22,6 +24,5 @@ export type DeployResult = {
|
|
|
22
24
|
skippedFiles: string[];
|
|
23
25
|
overwrittenFiles: string[];
|
|
24
26
|
};
|
|
25
|
-
export declare const getInstallCmds: (packageManager: string) => string[];
|
|
26
27
|
export declare const ROOT_SECTION = "(root)";
|
|
27
28
|
export declare const deployAssets: (options: DeployOptions) => Promise<DeployResult>;
|
package/dist/asset/deployer.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { writeFile, mkdir, stat } from "node:fs/promises";
|
|
2
2
|
import { join, dirname, resolve, sep } from "node:path";
|
|
3
3
|
import { buildEvalContext } from "../core/eval-context.js";
|
|
4
4
|
import { walkAssetRepo } from "./walker.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import { applyForeachReplacements } from "./foreach-replacement.js";
|
|
5
|
+
import { renderAssetContent } from "./renderer.js";
|
|
6
|
+
import { runInstallBuild } from "./installer.js";
|
|
8
7
|
import { computeSha1 } from "../utils/hash.js";
|
|
9
8
|
import { writeSettings, writeHashsum } from "../utils/settings-io.js";
|
|
10
|
-
|
|
9
|
+
export { getInstallCmds } from "./installer.js";
|
|
11
10
|
const fileExists = async (path) => {
|
|
12
11
|
try {
|
|
13
12
|
await stat(path);
|
|
@@ -25,19 +24,6 @@ const isWithinDir = (parentDir, candidate) => {
|
|
|
25
24
|
const prefix = parent.endsWith(sep) ? parent : parent + sep;
|
|
26
25
|
return target.startsWith(prefix);
|
|
27
26
|
};
|
|
28
|
-
export const getInstallCmds = (packageManager) => {
|
|
29
|
-
switch (packageManager) {
|
|
30
|
-
case "npm":
|
|
31
|
-
return ["npm ci", "npm run build"];
|
|
32
|
-
case "yarn":
|
|
33
|
-
return ["yarn install --frozen-lockfile", "yarn run build"];
|
|
34
|
-
case "pnpm":
|
|
35
|
-
return ["pnpm install --frozen-lockfile", "pnpm run build"];
|
|
36
|
-
case "bun":
|
|
37
|
-
default:
|
|
38
|
-
return ["bun ci", "bun run build"];
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
27
|
const collectReplacements = (entry) => {
|
|
42
28
|
if (!entry.meta?.replacements)
|
|
43
29
|
return [];
|
|
@@ -58,9 +44,28 @@ export const deployAssets = async (options) => {
|
|
|
58
44
|
try {
|
|
59
45
|
progress?.onFetchStart?.();
|
|
60
46
|
const repoDir = await provider.fetch();
|
|
47
|
+
const tree = {
|
|
48
|
+
listFiles: () => provider.listFiles(),
|
|
49
|
+
readFile: (relPath) => provider.readFile(relPath),
|
|
50
|
+
};
|
|
61
51
|
const semver = settings.accelplatformVersion.semver;
|
|
62
52
|
const context = buildEvalContext(settings);
|
|
63
|
-
const
|
|
53
|
+
const allEntries = await walkAssetRepo(tree, semver, context);
|
|
54
|
+
const entries = allEntries.filter((entry) => {
|
|
55
|
+
if (!isWithinDir(projectDir, join(projectDir, entry.relativePath))) {
|
|
56
|
+
console.warn(`Warning: skipping asset with path escaping project directory: ${entry.relativePath}`);
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (!isWithinDir(repoDir, resolve(repoDir, entry.sourceRelPath))) {
|
|
60
|
+
console.warn(`Warning: skipping asset with source path escaping asset repo: ${entry.sourceRelPath}`);
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
return true;
|
|
64
|
+
});
|
|
65
|
+
progress?.onDownloadStart?.();
|
|
66
|
+
await provider.materialize([
|
|
67
|
+
...new Set(entries.map((e) => e.sourceRelPath)),
|
|
68
|
+
]);
|
|
64
69
|
const deployedFiles = [];
|
|
65
70
|
const hashEntries = [];
|
|
66
71
|
const deployedAssets = {};
|
|
@@ -80,14 +85,6 @@ export const deployAssets = async (options) => {
|
|
|
80
85
|
progress.onSectionDeploy(section, sectionFiles);
|
|
81
86
|
}
|
|
82
87
|
}
|
|
83
|
-
if (!isWithinDir(projectDir, destPath)) {
|
|
84
|
-
console.warn(`Warning: skipping asset with path escaping project directory: ${targetPath}`);
|
|
85
|
-
continue;
|
|
86
|
-
}
|
|
87
|
-
if (!isWithinDir(repoDir, entry.sourcePath)) {
|
|
88
|
-
console.warn(`Warning: skipping asset with source path escaping asset repo: ${entry.sourcePath}`);
|
|
89
|
-
continue;
|
|
90
|
-
}
|
|
91
88
|
const exists = await fileExists(destPath);
|
|
92
89
|
if (exists) {
|
|
93
90
|
let decision = "overwrite";
|
|
@@ -103,23 +100,8 @@ export const deployAssets = async (options) => {
|
|
|
103
100
|
}
|
|
104
101
|
overwrittenFiles.push(targetPath);
|
|
105
102
|
}
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
if (replacements.length > 0) {
|
|
109
|
-
const foreachReplacements = replacements.filter((r) => r.type === "foreach");
|
|
110
|
-
if (foreachReplacements.length > 0) {
|
|
111
|
-
content = applyForeachReplacements(content, foreachReplacements, context);
|
|
112
|
-
}
|
|
113
|
-
if (targetPath.endsWith(".md")) {
|
|
114
|
-
content = processMarkdown(content, replacements, context);
|
|
115
|
-
}
|
|
116
|
-
else {
|
|
117
|
-
const regexReplacements = replacements.filter((r) => r.type === "regex");
|
|
118
|
-
if (regexReplacements.length > 0) {
|
|
119
|
-
content = applyRegexReplacements(content, regexReplacements, context);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
103
|
+
const raw = await tree.readFile(entry.sourceRelPath);
|
|
104
|
+
const content = renderAssetContent(raw, targetPath, collectReplacements(entry), context);
|
|
123
105
|
await mkdir(dirname(destPath), { recursive: true });
|
|
124
106
|
await writeFile(destPath, content, "utf-8");
|
|
125
107
|
const sha1 = computeSha1(content);
|
|
@@ -130,26 +112,13 @@ export const deployAssets = async (options) => {
|
|
|
130
112
|
}
|
|
131
113
|
const updatedSettings = {
|
|
132
114
|
...settings,
|
|
115
|
+
assetSource: provider.sourceInfo(),
|
|
133
116
|
deployedAssets,
|
|
134
117
|
};
|
|
135
118
|
await writeSettings(projectDir, updatedSettings);
|
|
136
119
|
await writeHashsum(projectDir, hashEntries);
|
|
137
120
|
if (!noInstall) {
|
|
138
|
-
|
|
139
|
-
progress?.onInstallStart?.(installCmd);
|
|
140
|
-
try {
|
|
141
|
-
execSync(installCmd, { cwd: projectDir, stdio: "pipe" });
|
|
142
|
-
}
|
|
143
|
-
catch {
|
|
144
|
-
console.warn(`Warning: install command failed: ${installCmd} in ${projectDir}`);
|
|
145
|
-
}
|
|
146
|
-
progress?.onBuildStart?.(buildCmd);
|
|
147
|
-
try {
|
|
148
|
-
execSync(buildCmd, { cwd: projectDir, stdio: "pipe" });
|
|
149
|
-
}
|
|
150
|
-
catch {
|
|
151
|
-
console.warn(`Warning: build command failed: ${buildCmd} in ${projectDir}`);
|
|
152
|
-
}
|
|
121
|
+
runInstallBuild(projectDir, settings.packageManager, progress);
|
|
153
122
|
}
|
|
154
123
|
return { deployedFiles, hashEntries, skippedFiles, overwrittenFiles };
|
|
155
124
|
}
|