tazuna 0.18.0 → 0.20.0
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/dist/core/mask.js +799 -17
- package/dist/pack/services/repomixExporter.js +8 -1
- package/package.json +1 -1
package/dist/core/mask.js
CHANGED
|
@@ -242,6 +242,147 @@ const ENV_LINE = /^((?:export[ \t]+|[ \t]*-[ \t]+["']?)?)([A-Za-z0-9_.-]+)(=)([^
|
|
|
242
242
|
const DOCKER_ENV = /^((?:ENV|ARG)[ \t]+)((?:[^\r\n]*\\[ \t]*\r?\n)*[^\r\n]*)/gm;
|
|
243
243
|
/** Dockerfile の ENV / ARG の KEY=value(値は空白まで。\ の後の空白は値のうち。引用符で囲んだものは引用符まで) */
|
|
244
244
|
const DOCKER_PAIR = /(?<![^ \t\r\n])([A-Za-z_][A-Za-z0-9_.-]*)(=)("(?:[^"\\\r\n]|\\.)*"|"[^"\r\n]*"|'[^'\r\n]*'|(?:\\[^\r\n]|[^\s"'\\])*)/g;
|
|
245
|
+
/**
|
|
246
|
+
* YAML・.properties・setup.cfg・HOCON などの、引用符で囲まない `キー: 値`(行の頭のキー。字下げ・一覧の `- ` の後も。
|
|
247
|
+
* v0.19.0)。値は行末まで(空白の後の # からはコメント)。引用符で囲んだ値は KEY_LITERAL で読む。
|
|
248
|
+
* コードのファイル(字句解析のない言語)ではオブジェクト・構造体のフィールドと区別できないため使わない(isCodePath)
|
|
249
|
+
*/
|
|
250
|
+
const COLON_PAIR = /^([ \t]*(?:-[ \t]+)?)([A-Za-z_][A-Za-z0-9_.-]*)([ \t]*:[ \t]+)([^\r\n]*)/gm;
|
|
251
|
+
/** YAML の真偽値・空の値 */
|
|
252
|
+
const YAML_NON_VALUE = /^(?:~|null|true|false|yes|no|on|off)$/i;
|
|
253
|
+
/** YAML の `|` / `>` の値(続く字下げの深い行が値) */
|
|
254
|
+
const YAML_BLOCK_SCALAR = /^[|>](?:[-+]?[1-9]?|[1-9][-+])$/;
|
|
255
|
+
/**
|
|
256
|
+
* 型の名前(文書に書いたコードの `password: string` / `api_key: Option<String>` / `password: varchar(255)`)。
|
|
257
|
+
* 値がこれらの名前だけ(型の引数・配列・? を含む)なら値ではない
|
|
258
|
+
*/
|
|
259
|
+
const TYPE_LIKE = /^&?(?:'[a-z]+[ \t]+)?(?:mut[ \t]+)?([A-Za-z_][\w.]*)(?:<[^<>\r\n]*>|\[[^\[\]\r\n]*\]|\([\d, ]*\))?\??$/;
|
|
260
|
+
const TYPE_NAMES = new Set([
|
|
261
|
+
"string",
|
|
262
|
+
"str",
|
|
263
|
+
"number",
|
|
264
|
+
"int",
|
|
265
|
+
"integer",
|
|
266
|
+
"long",
|
|
267
|
+
"short",
|
|
268
|
+
"float",
|
|
269
|
+
"double",
|
|
270
|
+
"decimal",
|
|
271
|
+
"bool",
|
|
272
|
+
"boolean",
|
|
273
|
+
"byte",
|
|
274
|
+
"bytes",
|
|
275
|
+
"char",
|
|
276
|
+
"any",
|
|
277
|
+
"unknown",
|
|
278
|
+
"object",
|
|
279
|
+
"text",
|
|
280
|
+
"varchar",
|
|
281
|
+
"varchar2",
|
|
282
|
+
"nvarchar",
|
|
283
|
+
"nvarchar2",
|
|
284
|
+
"uuid",
|
|
285
|
+
"date",
|
|
286
|
+
"datetime",
|
|
287
|
+
"timestamp",
|
|
288
|
+
"json",
|
|
289
|
+
"secretstr",
|
|
290
|
+
"option",
|
|
291
|
+
"optional",
|
|
292
|
+
"vec",
|
|
293
|
+
"list",
|
|
294
|
+
]);
|
|
295
|
+
/** 値が型の名前だけか(TYPE_LIKE / TYPE_NAMES) */
|
|
296
|
+
function isTypeName(value) {
|
|
297
|
+
const name = TYPE_LIKE.exec(value)?.[1];
|
|
298
|
+
return name !== undefined && TYPE_NAMES.has(name.toLowerCase());
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 字句解析のない言語のソースのパス(.php / .rb / .rs / .kt / .swift / .vue / .c など)と、言語の分かるソース・.vb。
|
|
302
|
+
* これらでは `キー: 値`(COLON_PAIR)を読まない(`password: hashedPassword` のようなオブジェクト・構造体の
|
|
303
|
+
* フィールド・名前付き引数を伏せないように。言語の分かるソースは言語ごとの伏せ方で文字列・コメントの中だけを読む)
|
|
304
|
+
*/
|
|
305
|
+
const CODE_PATH = /\.(?:vb|bas|cls|frm|ctl|php\d?|phtml|inc|rb|rake|gemspec|rs|kt|kts|swift|scala|sc|dart|vue|svelte|astro|c|h|cc|cpp|cxx|hpp|hh|hxx|m|mm|lua|pl|pm|r|ex|exs|erl|hrl|hs|ml|mli|fs|fsx|fsi|clj|cljs|cljc|groovy|gradle|jl|nim|zig|cr|coffee|elm|ps1|psm1|psd1|cshtml|vbhtml|razor|aspx|ascx|jsp|erb|haml|slim|twig)$/i;
|
|
306
|
+
function isCodePath(path) {
|
|
307
|
+
return CODE_PATH.test(path) || sourceLanguageOf(path) !== undefined;
|
|
308
|
+
}
|
|
309
|
+
/** 行の字下げの幅(空白とタブの数) */
|
|
310
|
+
function indentWidth(line) {
|
|
311
|
+
return /^[ \t]*/.exec(line)?.[0].length ?? 0;
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* 引用符で囲まない `キー: 値` の秘密系のキーの値を伏せる(COLON_PAIR)。ユーザー ID 系のキーは伏せない
|
|
315
|
+
* (`user: deploy` / k8s の `runAsUser: 1000` など、値でないものが多いため)。値でないもの: 空(入れ子)・真偽値・
|
|
316
|
+
* 差し込み(${DB_PASSWORD} / {{ x }} / <%= ... %> / @x@)・YAML の別名(*x)・タグ(!vault)・入れ物({ / [)・
|
|
317
|
+
* 型の名前・コードの続き(行末の , ; { ( [ \)・表示の文言(空白・日本語を含む値・キー名そのもの)。
|
|
318
|
+
* `|` / `>` の値は、続く字下げの深い行をまとめて 1 つの伏せ字にする
|
|
319
|
+
*/
|
|
320
|
+
function maskColonPairs(source, findings) {
|
|
321
|
+
const pattern = new RegExp(COLON_PAIR.source, COLON_PAIR.flags);
|
|
322
|
+
let out = "";
|
|
323
|
+
let last = 0;
|
|
324
|
+
for (let m = pattern.exec(source); m !== null; m = pattern.exec(source)) {
|
|
325
|
+
const lead = m[1] ?? "";
|
|
326
|
+
const key = m[2] ?? "";
|
|
327
|
+
const sep = m[3] ?? "";
|
|
328
|
+
const rest = m[4] ?? "";
|
|
329
|
+
if (kindForConfigKey(key) !== KIND_PASSWORD)
|
|
330
|
+
continue;
|
|
331
|
+
// アンカー(password: &pw 値)の後ろが値
|
|
332
|
+
const anchor = /^&[^\s]+[ \t]+/.exec(rest)?.[0] ?? "";
|
|
333
|
+
const body = rest.slice(anchor.length);
|
|
334
|
+
if (/^["']/.test(body))
|
|
335
|
+
continue;
|
|
336
|
+
const value = body.replace(/(?:^|[ \t]+)#.*$/, "").replace(/[ \t]+$/, "");
|
|
337
|
+
const valueStart = m.index + lead.length + key.length + sep.length + anchor.length;
|
|
338
|
+
if (YAML_BLOCK_SCALAR.test(value)) {
|
|
339
|
+
// 続く行のうち、キーの位置より深く字下げした行(間の空行も)が値
|
|
340
|
+
const lineEnd = m.index + m[0].length;
|
|
341
|
+
let cursor = lineEnd;
|
|
342
|
+
let first = -1;
|
|
343
|
+
let end = -1;
|
|
344
|
+
while (cursor < source.length) {
|
|
345
|
+
const next = source.indexOf("\n", cursor + 1);
|
|
346
|
+
const lineStart = cursor + 1;
|
|
347
|
+
const line = source.slice(lineStart, next < 0 ? source.length : next).replace(/\r$/, "");
|
|
348
|
+
if (line.trim() !== "") {
|
|
349
|
+
if (indentWidth(line) <= lead.length)
|
|
350
|
+
break;
|
|
351
|
+
if (first < 0)
|
|
352
|
+
first = lineStart + indentWidth(line);
|
|
353
|
+
end = lineStart + line.replace(/[ \t]+$/, "").length;
|
|
354
|
+
}
|
|
355
|
+
if (next < 0)
|
|
356
|
+
break;
|
|
357
|
+
cursor = next;
|
|
358
|
+
}
|
|
359
|
+
if (first < 0 || source.slice(first, end).includes(MASK))
|
|
360
|
+
continue;
|
|
361
|
+
findings.push({ line: lineOf(source, m.index), kind: KIND_PASSWORD });
|
|
362
|
+
out += `${source.slice(last, first)}${MASK}`;
|
|
363
|
+
last = end;
|
|
364
|
+
pattern.lastIndex = Math.max(pattern.lastIndex, end);
|
|
365
|
+
continue;
|
|
366
|
+
}
|
|
367
|
+
if (value === "" ||
|
|
368
|
+
value.includes(MASK) ||
|
|
369
|
+
YAML_NON_VALUE.test(value) ||
|
|
370
|
+
/^[&*!{[(`|>]/.test(value) ||
|
|
371
|
+
// GitHub Actions の ${{ secrets.X }}・Azure Pipelines の $(X)・Maven の @x@・Symfony の %env(X)% / %x%・
|
|
372
|
+
// ERB の <%= ... %>・説明の書き方の <password>
|
|
373
|
+
/^(?:\$\{\{[^\r\n]*\}\}|\$\([\w.]+\)|@[\w.-]+@|%[\w.():-]+%|<[^<>\r\n]*>)$/.test(value) ||
|
|
374
|
+
/[,;{(\[\\]$/.test(value) ||
|
|
375
|
+
isTypeName(value) ||
|
|
376
|
+
isShellVariable(value) ||
|
|
377
|
+
!isSecretPair(key, value, true)) {
|
|
378
|
+
continue;
|
|
379
|
+
}
|
|
380
|
+
findings.push({ line: lineOf(source, m.index), kind: KIND_PASSWORD });
|
|
381
|
+
out += `${source.slice(last, valueStart)}${MASK}`;
|
|
382
|
+
last = valueStart + value.length;
|
|
383
|
+
}
|
|
384
|
+
return out + source.slice(last);
|
|
385
|
+
}
|
|
245
386
|
/**
|
|
246
387
|
* バッチファイルの set の始まり(set DB_PASSWORD= / set "DB_PASSWORD= / @set / if ... set / ... & set。字下げした行も。
|
|
247
388
|
* C の文字列の set \"K= / VB・C# の set ""K= / MSBuild の Exec などの XML の set "K= も)。値の終わりは batchValueEnd で決める
|
|
@@ -249,10 +390,11 @@ const DOCKER_PAIR = /(?<![^ \t\r\n])([A-Za-z_][A-Za-z0-9_.-]*)(=)("(?:[^"\\\r\n]
|
|
|
249
390
|
const BATCH_SET = /(?<=^|[ \t@(&|;"'>])([Ss][Ee][Tt][ \t]+)(\\"|""|"|")?([A-Za-z0-9_.-]+)(=)/gm;
|
|
250
391
|
/**
|
|
251
392
|
* パスワードを引数で渡すコマンドの名前(sqlcmd / osql / isql / bcp の -P、mysql 系の -p... / --password=、
|
|
252
|
-
* net use の共有の後の引数、schtasks の /RP・/P
|
|
393
|
+
* net use の共有の後の引数、schtasks の /RP・/P。v0.20.0 から curl の -u ユーザー:パスワード・PowerShell の
|
|
394
|
+
* Invoke-Sqlcmd の -Password・sqlcmd の /P も)。パスの途中の語・別の拡張子のファイル名
|
|
253
395
|
* (/var/lib/mysql/・mysql.py・MySql.Data)は除く。MSBuild の "...\SQLCMD.EXE" と %SQLCMD% も読む
|
|
254
396
|
*/
|
|
255
|
-
const CLI_COMMAND = /(?<![\w.-])(sqlcmd|osql|isql|bcp|mysql|mysqldump|mysqladmin|mysqlimport|net[ \t]+use|schtasks)(?:\.exe)?(?=["'\s,\]%!]|&(?:quot|apos);|$)/gi;
|
|
397
|
+
const CLI_COMMAND = /(?<![\w.-])(sqlcmd|osql|isql|bcp|mysql|mysqldump|mysqladmin|mysqlimport|net(?:\.exe)?[ \t]+use|schtasks|sqlplus|exp|imp|expdp|impdp|sqlldr|dgmgrl|rman|curl|invoke-sqlcmd)(?:\.exe)?(?=["'\s,\]%!]|&(?:quot|apos);|$)/gi;
|
|
256
398
|
/** 引数の区切りの空白(行の継続(バッチの ^ / シェルの \ の後の改行)も) */
|
|
257
399
|
const CLI_SP = String.raw `(?:[ \t]|\^\r?\n|(?<=[ \t])\\\r?\n)+`;
|
|
258
400
|
/** 引用符で囲まない値の文字(行の継続の ^ / \ は値に含めない) */
|
|
@@ -260,6 +402,15 @@ const CLI_BARE = (excluded) => String.raw `(?:[^\s"'` + "`" + String.raw `^\\${e
|
|
|
260
402
|
/** 引用符で囲んだ値("..." / '...' / MSBuild の "...") */
|
|
261
403
|
const CLI_QUOTED = String.raw `"(?:[^"\\\r\n]|\\.)*"|'[^'\r\n]*'|"(?:(?!")[^\r\n])*"`;
|
|
262
404
|
const CLI_VALUE = `${CLI_QUOTED}|${CLI_BARE("")}`;
|
|
405
|
+
/**
|
|
406
|
+
* Oracle のログオン(ユーザー/パスワード[@接続先])の前の部分: 引数の区切り(空白・VB の Process.Start("sqlplus.exe",
|
|
407
|
+
* "...") の "," )と、ログオンを囲む引用符(sqlplus "sys/pw@db as sysdba")。ユーザー名は英字で始まるもの(変数の
|
|
408
|
+
* $DB_USER / %DB_USER% / &1 と、OS 認証の / は除く)か引用符で囲んだもの
|
|
409
|
+
*/
|
|
410
|
+
const ORACLE_GAP = String.raw `["']?(?:${CLI_SP}|[ \t]*,[ \t]*)["']?`;
|
|
411
|
+
const ORACLE_USER = String.raw `(?:[A-Za-z_][\w$#.]*|"[^"\r\n]+")`;
|
|
412
|
+
/** Oracle のログオンのパスワード(引用符で囲んだものか、@ / 空白・引用符・/ の前まで) */
|
|
413
|
+
const ORACLE_PASSWORD = String.raw `"[^"\r\n]*"|[^\s"'@\/]+`;
|
|
263
414
|
/** コマンドの種類ごとの、パスワードの引数(lead = 値の前、value = 値) */
|
|
264
415
|
const CLI_ARGS = [
|
|
265
416
|
[
|
|
@@ -272,6 +423,8 @@ const CLI_ARGS = [
|
|
|
272
423
|
/((?<=[\s'\[,])-P'[ \t]*,[ \t]*')([^'\r\n]*)/g,
|
|
273
424
|
new RegExp(String.raw `((?<=[\s;])-P)(${CLI_QUOTED})`, "g"),
|
|
274
425
|
new RegExp(String.raw `((?<=[\s"';])-P)(${CLI_BARE(",\\]")})`, "g"),
|
|
426
|
+
// Windows の / の形(sqlcmd /U sa /P value。v0.20.0)
|
|
427
|
+
new RegExp(String.raw `((?<=[\s"'])\/P${CLI_SP})(${CLI_VALUE})`, "g"),
|
|
275
428
|
],
|
|
276
429
|
],
|
|
277
430
|
[
|
|
@@ -284,15 +437,37 @@ const CLI_ARGS = [
|
|
|
284
437
|
// mysqladmin password 新しいパスワード
|
|
285
438
|
[/^mysqladmin$/i, [new RegExp(String.raw `((?<=\s)password${CLI_SP})(${CLI_VALUE})`, "g")]],
|
|
286
439
|
[
|
|
287
|
-
/^net[ \t]+use$/i,
|
|
440
|
+
/^net(?:\.exe)?[ \t]+use$/i,
|
|
288
441
|
[
|
|
289
|
-
// net use [Z: | *] \\srv\share [/user:x ...] パスワード
|
|
290
|
-
new RegExp(String.raw `(^net[ \t]+use(?:${CLI_SP}(?:[A-Za-z]:|\*))?${CLI_SP}(?:"\\\\[^"\r\n]*"|"\\\\(?:(?!")[^\r\n])*"|\\\\[^\s"` +
|
|
442
|
+
// net use [Z: | *] \\srv\share [/user:x ...] パスワード(net.exe use も)
|
|
443
|
+
new RegExp(String.raw `(^net(?:\.exe)?[ \t]+use(?:${CLI_SP}(?:[A-Za-z]:|\*))?${CLI_SP}(?:"\\\\[^"\r\n]*"|"\\\\(?:(?!")[^\r\n])*"|\\\\[^\s"` +
|
|
291
444
|
"`" +
|
|
292
445
|
String.raw `]+)(?:${CLI_SP}\/[A-Za-z]+(?::(?:"[^"\r\n]*"|[^\s"]*))?)*${CLI_SP})(?![\/*])(${CLI_VALUE})`, "i"),
|
|
293
446
|
],
|
|
294
447
|
],
|
|
295
448
|
[/^schtasks$/i, [new RegExp(String.raw `((?<=\s)\/(?:rp|p)${CLI_SP})(?!\/)(${CLI_VALUE})`, "gi")]],
|
|
449
|
+
// Oracle のログオン(v0.19.0): sqlplus / exp / imp / expdp / impdp / sqlldr / dgmgrl の最初の引数(-S などの
|
|
450
|
+
// オプションの後)の ユーザー/パスワード[@接続先] と userid=...、rman の target / catalog / auxiliary の後
|
|
451
|
+
[
|
|
452
|
+
/^(?:sqlplus|exp|imp|expdp|impdp|sqlldr|dgmgrl)$/i,
|
|
453
|
+
[
|
|
454
|
+
new RegExp(String.raw `(^[A-Za-z]+(?:\.exe)?${ORACLE_GAP}(?:-[A-Za-z]+${ORACLE_GAP})*["']?${ORACLE_USER}\/)(${ORACLE_PASSWORD})`, "i"),
|
|
455
|
+
new RegExp(String.raw `((?<=[\s"',])userid=["']?${ORACLE_USER}\/)(${ORACLE_PASSWORD})`, "gi"),
|
|
456
|
+
],
|
|
457
|
+
],
|
|
458
|
+
[
|
|
459
|
+
/^rman$/i,
|
|
460
|
+
[
|
|
461
|
+
new RegExp(String.raw `((?<=\s)(?:target|catalog|auxiliary)(?:${CLI_SP}|=)["']?${ORACLE_USER}\/)(${ORACLE_PASSWORD})`, "gi"),
|
|
462
|
+
],
|
|
463
|
+
],
|
|
464
|
+
// curl の -u / --user / --proxy-user / -U の ユーザー:パスワード(v0.20.0。パスワードの無い -u user は除く)
|
|
465
|
+
[
|
|
466
|
+
/^curl$/i,
|
|
467
|
+
[new RegExp(String.raw `((?<=[\s"'])(?:-u|-U|--user|--proxy-user)(?:${CLI_SP}|=)?["']?[^\s:"']*:)([^\s"']+)`, "g")],
|
|
468
|
+
],
|
|
469
|
+
// PowerShell の Invoke-Sqlcmd -Password 値(v0.20.0。$ で始まる変数・式は除く)
|
|
470
|
+
[/^invoke-sqlcmd$/i, [new RegExp(String.raw `((?<=\s)-Password(?:${CLI_SP}|:)(?![$(@]))(${CLI_VALUE})`, "gi")]],
|
|
296
471
|
];
|
|
297
472
|
/** コマンド名のない引数の文字列(ProcessStartInfo.Arguments = "-S srv -U sa -P ...")の -U ユーザー -P パスワード */
|
|
298
473
|
const CLI_USER_PASSWORD = new RegExp(String.raw `((?<=[\s"'])-U[ \t]*(?:${CLI_QUOTED}|[^\s"'` + "`" + String.raw `]+)[ \t]+-P[ \t]*)(${CLI_VALUE})`, "g");
|
|
@@ -391,7 +566,7 @@ function maskCliArgs(source, findings) {
|
|
|
391
566
|
}
|
|
392
567
|
for (const [kind, at] of firsts) {
|
|
393
568
|
const tail = segment.slice(at);
|
|
394
|
-
if (kind === 0 && !/(?<=[\s"'\[,])-U(?!-)/.test(tail)) {
|
|
569
|
+
if (kind === 0 && !/(?<=[\s"'\[,])-U(?!-)|(?<=[\s"'])\/U(?=[ \t])/.test(tail)) {
|
|
395
570
|
continue;
|
|
396
571
|
}
|
|
397
572
|
for (const pattern of CLI_ARGS[kind]?.[1] ?? []) {
|
|
@@ -454,6 +629,77 @@ function maskCliArgs(source, findings) {
|
|
|
454
629
|
}
|
|
455
630
|
return out + source.slice(last);
|
|
456
631
|
}
|
|
632
|
+
/** PowerShell の ConvertTo-SecureString(v0.20.0) */
|
|
633
|
+
const SECURE_STRING = /(?<![\w-])ConvertTo-SecureString(?![\w-])/gi;
|
|
634
|
+
/** その引数(-名前[:値] / 文字列 / 引用符で囲まない語 / 閉じ括弧) */
|
|
635
|
+
const PS_TOKEN = /(-[A-Za-z]+)(?::(\S+))?|("(?:[^"`\r\n]|`.)*"|'(?:[^'\r\n]|'')*'|[^\s"'()]+)|(\))/g;
|
|
636
|
+
/** パイプで渡す文字列("..." | ConvertTo-SecureString -AsPlainText) */
|
|
637
|
+
const SECURE_STRING_PIPED = /("(?:[^"`\r\n]|`.)*"|'(?:[^'\r\n]|'')*')([ \t]*\|[ \t]*)(?=ConvertTo-SecureString\b)/gi;
|
|
638
|
+
/**
|
|
639
|
+
* PowerShell の ConvertTo-SecureString -AsPlainText の平文(-String の値か最初の位置の引数、パイプで渡す前の文字列)を
|
|
640
|
+
* 伏せる。-AsPlainText の無いもの(暗号化した文字列を戻す使い方)と、変数・式($pw / $(...) / (Read-Host))は伏せない
|
|
641
|
+
*/
|
|
642
|
+
function maskSecureStrings(source, findings) {
|
|
643
|
+
const spans = [];
|
|
644
|
+
const add = (value, at) => {
|
|
645
|
+
const quote = /^["']/.test(value) && value.length >= 2 ? value.charAt(0) : "";
|
|
646
|
+
const inner = quote === "" ? value : value.slice(1, -1);
|
|
647
|
+
// 引用符で囲まない英字だけの語は説明の文章(ConvertTo-SecureString with -AsPlainText ...)のことが多いので除く
|
|
648
|
+
if (inner.trim() === "" || inner === MASK || /^[$(@]/.test(inner) || isCliNonSecret(value) || (quote === "" && /^[A-Za-z]+$/.test(inner)))
|
|
649
|
+
return;
|
|
650
|
+
spans.push({ start: at + quote.length, end: at + quote.length + inner.length });
|
|
651
|
+
};
|
|
652
|
+
for (const m of source.matchAll(SECURE_STRING)) {
|
|
653
|
+
const from = (m.index ?? 0) + m[0].length;
|
|
654
|
+
const end = cliCommandEnd(source, from);
|
|
655
|
+
const args = source.slice(from, end);
|
|
656
|
+
if (!/(?<![\w-])-AsPlainText(?![\w-])/i.test(args))
|
|
657
|
+
continue;
|
|
658
|
+
let skip = false;
|
|
659
|
+
for (const t of args.matchAll(PS_TOKEN)) {
|
|
660
|
+
const at = from + (t.index ?? 0);
|
|
661
|
+
if (t[4] !== undefined)
|
|
662
|
+
break;
|
|
663
|
+
if (t[1] !== undefined) {
|
|
664
|
+
const name = t[1].toLowerCase();
|
|
665
|
+
if (name === "-string") {
|
|
666
|
+
if (t[2] !== undefined) {
|
|
667
|
+
add(t[2], at + t[1].length + 1);
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
else if (name === "-key" || name === "-securekey") {
|
|
672
|
+
skip = t[2] === undefined;
|
|
673
|
+
}
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (skip) {
|
|
677
|
+
skip = false;
|
|
678
|
+
continue;
|
|
679
|
+
}
|
|
680
|
+
// -String の値か、最初の位置の引数
|
|
681
|
+
add(t[3] ?? "", at);
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
for (const m of source.matchAll(SECURE_STRING_PIPED)) {
|
|
686
|
+
const after = source.slice((m.index ?? 0) + m[0].length, cliCommandEnd(source, (m.index ?? 0) + m[0].length));
|
|
687
|
+
if (/(?<![\w-])-AsPlainText(?![\w-])/i.test(after))
|
|
688
|
+
add(m[1] ?? "", m.index ?? 0);
|
|
689
|
+
}
|
|
690
|
+
if (spans.length === 0)
|
|
691
|
+
return source;
|
|
692
|
+
let out = "";
|
|
693
|
+
let last = 0;
|
|
694
|
+
for (const span of spans.sort((x, y) => x.start - y.start)) {
|
|
695
|
+
if (span.start < last)
|
|
696
|
+
continue;
|
|
697
|
+
findings.push({ line: lineOf(source, span.start), kind: KIND_PASSWORD });
|
|
698
|
+
out += source.slice(last, span.start) + MASK;
|
|
699
|
+
last = span.end;
|
|
700
|
+
}
|
|
701
|
+
return out + source.slice(last);
|
|
702
|
+
}
|
|
457
703
|
/**
|
|
458
704
|
* SQL の SET col=... の値がバインド変数(`:LOGIN_PASSWORD` / `@p1` / `$1` / `?` / `%s`)か。名前が列名と同じもの、
|
|
459
705
|
* 後ろに , / WHERE が続くもの、前の行に UPDATE があるものだけ(バッチの set ADMIN_PASSWORD=@admin は値として伏せる)
|
|
@@ -475,8 +721,13 @@ function isBindMarkerFor(key, after, before) {
|
|
|
475
721
|
}
|
|
476
722
|
/** コマンドの引数の環境変数(docker run -e DB_PASSWORD=... / --env "DB_PASSWORD=...") */
|
|
477
723
|
const SHELL_ENV_ARG = /(?<=[ \t])((?:-e|--env)(?:[ \t]+|=))("(?:[^"\\\r\n]|\\.)*"|"[^"\r\n]*"|'[^'\r\n]*'|[A-Za-z_][A-Za-z0-9_]*=(?:"(?:[^"\\\r\n]|\\.)*"|"[^"\r\n]*"|'[^'\r\n]*'|(?:\\[^\r\n]|[^\s"'\\])*))/g;
|
|
478
|
-
/**
|
|
479
|
-
|
|
724
|
+
/**
|
|
725
|
+
* キーの値の文字列(C# の @"..." / $"..." / $@"..."、Python の u"..." / r'...' / f"..." / b"..." なども)。
|
|
726
|
+
* JS のテンプレート文字列・Go の生の文字列(`...`)は差し込み(${...})のない 1 行のものだけ(v0.20.0)
|
|
727
|
+
*/
|
|
728
|
+
const KEY_VALUE = String.raw `((?:\$?@|@\$)"(?:[^"\r\n]|"")*"|(?:\$|[rRuUbBfF]{1,2})?"(?:[^"\\\r\n]|\\.)*"|(?:[rRuUbBfF]{1,2})?'(?:[^'\\\r\n]|\\.)*'|` + "`" + String.raw `(?:[^` + "`" + String.raw `\\\r\n$]|\\.|\$(?!\{))*` + "`" + ")";
|
|
729
|
+
/** KEY_VALUE の文字列の前の接頭辞(@ / $ / r / u / b / f)。引用符は含まない */
|
|
730
|
+
const LITERAL_PREFIX = /^[^"'`]*/;
|
|
480
731
|
/** 型の注釈の型(string / Optional[str] / Map<K, V> / string | null) */
|
|
481
732
|
const KEY_TYPE = String.raw `[A-Za-z_$][\w$.]*(?:<[^<>\r\n]*>|\[[^\[\]\r\n]*\])*\??`;
|
|
482
733
|
/**
|
|
@@ -489,8 +740,19 @@ const KEY_TYPE = String.raw `[A-Za-z_$][\w$.]*(?:<[^<>\r\n]*>|\[[^\[\]\r\n]*\])*
|
|
|
489
740
|
const KEY_LITERAL = new RegExp(String.raw `((?<![A-Za-z0-9_$.])[A-Za-z_$][A-Za-z0-9_$.]*|"[^"\r\n]*"|'[^'\r\n]*')` +
|
|
490
741
|
String.raw `(\s*(?:(?<=[ \t])[Aa][Ss][ \t]+[A-Za-z_][\w.]*(?:\(\))?[ \t]*=(?!=)` +
|
|
491
742
|
String.raw `|\{[ \t]*get;[ \t]*(?:(?:(?:public|private|protected|internal)[ \t]+)*(?:set|init);[ \t]*)?\}[ \t]*=(?!=)` +
|
|
492
|
-
String.raw
|
|
743
|
+
String.raw `|=>|:=|:|=(?!=))\s*)` +
|
|
493
744
|
KEY_VALUE, "g");
|
|
745
|
+
/**
|
|
746
|
+
* 添字への代入(v0.19.0): $config['db']['password'] = '...' / os.environ["DB_PASSWORD"] = "..." / cfg["apiKey"] = '...'。
|
|
747
|
+
* 添字の文字列をキーとして KEY_LITERAL と同じく判定する
|
|
748
|
+
*/
|
|
749
|
+
const SUBSCRIPT_LITERAL = new RegExp(String.raw `(\[[ \t]*)("[^"\r\n]*"|'[^'\r\n]*')([ \t]*\][ \t]*=(?![=>])[ \t]*)` + KEY_VALUE, "g");
|
|
750
|
+
/**
|
|
751
|
+
* 秘密の名前を最初の引数に渡す関数の 2 番目の文字列(v0.19.0): define('DB_PASSWORD', '...') /
|
|
752
|
+
* env('DB_PASSWORD', '既定値') / os.environ.get("DB_PASSWORD", "既定値") / System.getProperty("db.password", "既定値")。
|
|
753
|
+
* 最初の引数はキーの形の文字列(空白を含まない)だけ
|
|
754
|
+
*/
|
|
755
|
+
const NAMED_ARG_LITERAL = new RegExp(String.raw `(\([ \t]*)("[A-Za-z_][\w.-]*"|'[A-Za-z_][\w.-]*')([ \t]*,[ \t]*)` + KEY_VALUE + String.raw `(?=[ \t]*[,)])`, "g");
|
|
494
756
|
/**
|
|
495
757
|
* 型の注釈つきのキーと文字列の値(TS / Python の password: string = "..." / password:str="...")。KEY_LITERAL の後に
|
|
496
758
|
* 別に探す(同じ式に入れると case "prod": dbPassword = "..." の dbPassword を型と読み、その代入を探し直さないため)。
|
|
@@ -500,6 +762,381 @@ const KEY_LITERAL = new RegExp(String.raw `((?<![A-Za-z0-9_$.])[A-Za-z_$][A-Za-z
|
|
|
500
762
|
const KEY_LITERAL_TYPED = new RegExp(String.raw `((?<![A-Za-z0-9_$.])[A-Za-z_$][A-Za-z0-9_$.]*)` +
|
|
501
763
|
String.raw `(\s*:[ \t]*${KEY_TYPE}(?:[ \t]*\|[ \t]*${KEY_TYPE})*[ \t]*=(?!=)\s*)` +
|
|
502
764
|
KEY_VALUE, "g");
|
|
765
|
+
// ---------------------------------------------------------------------------
|
|
766
|
+
// コードの形 (v0.20.0)。VB は maskVbSource が代入・比較・秘密の名前の関数の引数を読む。ほかの言語にも同じ形を
|
|
767
|
+
// 汎用テキストの規則として足す (言語の分かるソースは maskLanguageSource が文字列の中の部分だけを残す)
|
|
768
|
+
// ---------------------------------------------------------------------------
|
|
769
|
+
/**
|
|
770
|
+
* 秘密の名前のメソッドに文字列を 1 つだけ渡す呼び出し(setter・builder: ds.setPassword("...") / .password("...") /
|
|
771
|
+
* SetPassword("...") / withPassword("...") / user.set_password("...") / setApiKey("..."))。名前の判定は secretSetterKey
|
|
772
|
+
*/
|
|
773
|
+
const SECRET_SETTER = new RegExp(String.raw `((?<![A-Za-z0-9_$])([A-Za-z_$][A-Za-z0-9_$]*)[ \t]*\([ \t]*)` + KEY_VALUE + String.raw `(?=[ \t]*\))`, "g");
|
|
774
|
+
/** setter の名前の前の動詞(React のフックの use... は名前を受け取ることが多いので入れない) */
|
|
775
|
+
const SETTER_VERBS = new Set(["set", "with"]);
|
|
776
|
+
/**
|
|
777
|
+
* setter・builder の名前から値のキーを返す(秘密系でなければ undefined)。set / with の後に秘密系の名前が続くもの
|
|
778
|
+
* (setPassword / withApiKey / set_db_password)と、名前そのものが秘密系の語のもの(password / apiKey / secret / token)
|
|
779
|
+
* だけ。validatePassword / hashPassword / getPassword のような動作の名前と、テストの t.pass("...") は除く
|
|
780
|
+
*/
|
|
781
|
+
function secretSetterKey(callee) {
|
|
782
|
+
const words = keyWords(callee);
|
|
783
|
+
const first = words[0] ?? "";
|
|
784
|
+
if (SETTER_VERBS.has(first) && words.length >= 2) {
|
|
785
|
+
const key = words.slice(1).join("_");
|
|
786
|
+
return kindForConfigKey(key) === KIND_PASSWORD ? key : undefined;
|
|
787
|
+
}
|
|
788
|
+
const whole = words.join("");
|
|
789
|
+
const named = words.length === 1 ? whole !== "pass" && SECRET_KEY_WORDS.has(whole) : words.length === 2 && SECRET_KEY_WORDS.has(whole);
|
|
790
|
+
return named ? callee : undefined;
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* 既定値の演算子の後の文字列(環境変数が無いときの既定値: process.env.DB_PASSWORD || "..." / ?? "..." /
|
|
794
|
+
* Python の os.getenv("DB_PASSWORD") or "..." / Kotlin・PHP の ?: "...")。演算子の前の値か代入の先の名前で判定する
|
|
795
|
+
* (defaultValueKeys)。?: は前に空白のあるものだけ(TS の省略可能なプロパティ password?: "a" | "b" を除く)
|
|
796
|
+
*/
|
|
797
|
+
const DEFAULT_LITERAL = new RegExp(String.raw `(\|\||\?\?|(?<=[ \t])\?:|(?<![\w$.])or(?![\w$]))([ \t]*)` + KEY_VALUE, "g");
|
|
798
|
+
/** 既定値の式の代入の先・プロパティのキー(password = / const password: string = / password := / password: / 'password' =>) */
|
|
799
|
+
const DEFAULT_TARGET = /(?:^|[^\w$.])([A-Za-z_$][\w$.]*|"[^"\r\n]*"|'[^'\r\n]*')[ \t]*(?:(?::[ \t]*[\w$.<>\[\]|? ]{1,80})?[ \t]*:?=(?!=)|:(?!:)|=>)[ \t]*(?:await[ \t]+)?$/;
|
|
800
|
+
/** close の位置の閉じ括弧に対応する開き括弧の位置(文字列の中の括弧は数えない)。無ければ -1 */
|
|
801
|
+
function openingBracket(text, close) {
|
|
802
|
+
let depth = 0;
|
|
803
|
+
for (let i = close; i >= 0; i--) {
|
|
804
|
+
const ch = text.charAt(i);
|
|
805
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
806
|
+
// 文字列の始まりの引用符まで戻る(\ の後の引用符は文字列の中)
|
|
807
|
+
let j = i - 1;
|
|
808
|
+
while (j >= 0 && !(text.charAt(j) === ch && text.charAt(j - 1) !== "\\"))
|
|
809
|
+
j -= 1;
|
|
810
|
+
if (j < 0)
|
|
811
|
+
return -1;
|
|
812
|
+
i = j;
|
|
813
|
+
}
|
|
814
|
+
else if (ch === ")" || ch === "]") {
|
|
815
|
+
depth += 1;
|
|
816
|
+
}
|
|
817
|
+
else if (ch === "(" || ch === "[") {
|
|
818
|
+
depth -= 1;
|
|
819
|
+
if (depth === 0)
|
|
820
|
+
return i;
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return -1;
|
|
824
|
+
}
|
|
825
|
+
/** text の end の前で終わる値(識別子・メンバーの参照・呼び出し・添字の並び。os.getenv("X") / process.env.X)の始まり */
|
|
826
|
+
function operandStart(text, end) {
|
|
827
|
+
let i = end;
|
|
828
|
+
// TS の非 null 表明(process.env.X!)
|
|
829
|
+
if (text.charAt(i - 1) === "!")
|
|
830
|
+
i -= 1;
|
|
831
|
+
for (;;) {
|
|
832
|
+
const ch = text.charAt(i - 1);
|
|
833
|
+
if (ch === ")" || ch === "]") {
|
|
834
|
+
const open = openingBracket(text, i - 1);
|
|
835
|
+
if (open < 0)
|
|
836
|
+
break;
|
|
837
|
+
i = open;
|
|
838
|
+
}
|
|
839
|
+
else if (/[\w$]/.test(ch)) {
|
|
840
|
+
while (i > 0 && /[\w$]/.test(text.charAt(i - 1)))
|
|
841
|
+
i -= 1;
|
|
842
|
+
}
|
|
843
|
+
else {
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
// メンバーの参照(. / ?. / :: / ->)が前にあれば続けて読む
|
|
847
|
+
const access = /(?:\?\.|\.|::|->)$/.exec(text.slice(Math.max(0, i - 2), i));
|
|
848
|
+
if (access !== null && /[\w$)\]]/.test(text.charAt(i - access[0].length - 1))) {
|
|
849
|
+
i -= access[0].length;
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
// 呼び出し・添字の括弧の前の名前(getenv(...) / ENV[...])
|
|
853
|
+
if ((text.charAt(i) === "(" || text.charAt(i) === "[") && /[\w$)\]]/.test(text.charAt(i - 1)))
|
|
854
|
+
continue;
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
return i;
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* 値の式から、値のキーの候補を返す: 最後の名前(process.env.DB_PASSWORD / config.password)か、最後の括弧の中の
|
|
861
|
+
* キーの形の文字列(getenv("DB_PASSWORD") / ENV["DB_PASSWORD"] / config.get("db.password"))と呼び出しの名前(getPassword())
|
|
862
|
+
*/
|
|
863
|
+
function operandKeys(operand) {
|
|
864
|
+
const text = operand.replace(/!$/, "");
|
|
865
|
+
const last = /([A-Za-z_$][\w$]*)$/.exec(text);
|
|
866
|
+
if (last !== null)
|
|
867
|
+
return [last[1] ?? ""];
|
|
868
|
+
if (!/[)\]]$/.test(text))
|
|
869
|
+
return [];
|
|
870
|
+
const open = openingBracket(text, text.length - 1);
|
|
871
|
+
if (open < 0)
|
|
872
|
+
return [];
|
|
873
|
+
const keys = [...text.slice(open + 1, -1).matchAll(/["']([A-Za-z_][\w.:-]*)["']/g)].map((m) => m[1] ?? "");
|
|
874
|
+
const callee = /([A-Za-z_$][\w$]*)$/.exec(text.slice(0, open));
|
|
875
|
+
if (callee !== null)
|
|
876
|
+
keys.push(callee[1] ?? "");
|
|
877
|
+
return keys;
|
|
878
|
+
}
|
|
879
|
+
/** 既定値の演算子の前の値(続けて書いた演算子の前の値も)と代入の先から、値のキーの候補を集める */
|
|
880
|
+
function defaultValueKeys(line) {
|
|
881
|
+
const keys = [];
|
|
882
|
+
let end = line.length;
|
|
883
|
+
for (let guard = 0; guard < 8; guard++) {
|
|
884
|
+
while (end > 0 && /[ \t]/.test(line.charAt(end - 1)))
|
|
885
|
+
end -= 1;
|
|
886
|
+
const start = operandStart(line, end);
|
|
887
|
+
if (start === end)
|
|
888
|
+
break;
|
|
889
|
+
keys.push(...operandKeys(line.slice(start, end)));
|
|
890
|
+
end = start;
|
|
891
|
+
while (end > 0 && /[ \t]/.test(line.charAt(end - 1)))
|
|
892
|
+
end -= 1;
|
|
893
|
+
const op = /(?:\|\||\?\?|\?:|(?<![\w$.])or)$/.exec(line.slice(Math.max(0, end - 3), end));
|
|
894
|
+
if (op === null || (op[0] === "or" && /[\w$.]/.test(line.charAt(end - 3))))
|
|
895
|
+
break;
|
|
896
|
+
end -= op[0].length;
|
|
897
|
+
}
|
|
898
|
+
const target = DEFAULT_TARGET.exec(line.slice(0, end));
|
|
899
|
+
if (target !== null)
|
|
900
|
+
keys.push((target[1] ?? "").replace(/^["']|["']$/g, ""));
|
|
901
|
+
return keys;
|
|
902
|
+
}
|
|
903
|
+
/**
|
|
904
|
+
* 秘密の名前の値と文字列の比較(txtPassword.Text == "..." / password === '...' / "..." != pwd /
|
|
905
|
+
* password.equals("...") / "...".equals(password))。名前の後ろの .Text / .value / .getText() は除いて判定する(compareKey)
|
|
906
|
+
*/
|
|
907
|
+
const COMPARE_CHAIN = String.raw `[A-Za-z_$][\w$]*(?:(?:\?\.|\.)[A-Za-z_$][\w$]*(?:\(\))?)*`;
|
|
908
|
+
const COMPARE_LITERAL = new RegExp(String.raw `((?<![\w$.])(${COMPARE_CHAIN})[ \t]*(?:===?|!==?)[ \t]*)` + KEY_VALUE, "g");
|
|
909
|
+
const COMPARE_LITERAL_LEFT = new RegExp(KEY_VALUE + String.raw `([ \t]*(?:===?|!==?)[ \t]*(${COMPARE_CHAIN}))(?![\w$(.])`, "g");
|
|
910
|
+
const EQUALS_LITERAL = new RegExp(String.raw `((?<![\w$.])(${COMPARE_CHAIN})\.(?:equals|Equals|equalsIgnoreCase|contentEquals)\([ \t]*)` + KEY_VALUE + String.raw `(?=[ \t]*\))`, "g");
|
|
911
|
+
const EQUALS_LITERAL_LEFT = new RegExp(KEY_VALUE + String.raw `(\.(?:equals|Equals|equalsIgnoreCase)\([ \t]*(${COMPARE_CHAIN})[ \t]*\))`, "g");
|
|
912
|
+
/** 比べる値の名前(画面の部品の .Text / .value / .getText() などは除く) */
|
|
913
|
+
function compareKey(chain) {
|
|
914
|
+
const segments = chain.split(/\?\.|\./).map((segment) => segment.replace(/\(\)$/, ""));
|
|
915
|
+
while (segments.length > 1 && /^(?:text|value|gettext|getvalue|tostring|trim|strip)$/i.test(segments[segments.length - 1] ?? "")) {
|
|
916
|
+
segments.pop();
|
|
917
|
+
}
|
|
918
|
+
return segments.join(".");
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* パスワードを位置で渡す定番の関数(v0.20.0): [名前(修飾を含めた末尾)、引数の数からパスワードの引数の位置(0 始まり)]
|
|
922
|
+
*/
|
|
923
|
+
const POSITIONAL_SECRET_CALLS = [
|
|
924
|
+
// .NET: new NetworkCredential("user", "pw"[, "domain"]) / MailKit の client.Authenticate("user", "pw")
|
|
925
|
+
[/(?:^|\.)NetworkCredential$/, (n) => (n === 2 || n === 3 ? 1 : undefined)],
|
|
926
|
+
[/\.Authenticate(?:Async)?$/, (n) => (n === 2 ? 1 : undefined)],
|
|
927
|
+
// Java: DriverManager.getConnection(url, "user", "pw") / DataSource の getConnection("user", "pw") /
|
|
928
|
+
// JavaMail の new PasswordAuthentication("user", "pw".toCharArray()) / Spring Security・HttpClient の
|
|
929
|
+
// new UsernamePasswordAuthenticationToken("user", "pw") / new UsernamePasswordCredentials("user", "pw") /
|
|
930
|
+
// OkHttp の Credentials.basic("user", "pw")
|
|
931
|
+
[/(?:^|\.)getConnection$/, (n) => (n === 3 ? 2 : n === 2 ? 1 : undefined)],
|
|
932
|
+
[/(?:^|\.)PasswordAuthentication$/, (n) => (n === 2 ? 1 : undefined)],
|
|
933
|
+
[/(?:^|\.)UsernamePassword(?:AuthenticationToken|Credentials)$/, (n) => (n === 2 || n === 3 ? 1 : undefined)],
|
|
934
|
+
[/(?:^|\.)Credentials\.basic$/, (n) => (n === 2 || n === 3 ? 1 : undefined)],
|
|
935
|
+
// PHP: mysqli_connect("host", "user", "pw", "db") / new mysqli(...) / mysql_connect(...) / new PDO($dsn, "user", "pw")
|
|
936
|
+
[/^(?:mysqli_connect|mysql_connect|mysqli|PDO)$/, (n) => (n >= 3 ? 2 : undefined)],
|
|
937
|
+
// Python: MySQLdb.connect("host", "user", "pw", "db") / pymysql.connect(...)。Node: new Sequelize("db", "user", "pw", {...})
|
|
938
|
+
[/(?:^|\.)(?:MySQLdb|pymysql)\.connect$/, (n) => (n >= 3 ? 2 : undefined)],
|
|
939
|
+
[/(?:^|\.)Sequelize$/, (n) => (n === 3 || n === 4 ? 2 : undefined)],
|
|
940
|
+
];
|
|
941
|
+
const POSITIONAL_CALL_NAME = /(?<![\w$])(NetworkCredential|Authenticate(?:Async)?|getConnection|PasswordAuthentication|UsernamePassword(?:AuthenticationToken|Credentials)|basic|mysqli_connect|mysql_connect|mysqli|PDO|connect|Sequelize)[ \t]*\(/g;
|
|
942
|
+
/** 引数が文字列だけ(Java の "...".toCharArray() も)か */
|
|
943
|
+
const ARG_LITERAL = new RegExp(String.raw `^\s*` + KEY_VALUE + String.raw `(?:[ \t]*\.[ \t]*(?:toCharArray|ToCharArray)\(\))?\s*$`);
|
|
944
|
+
/** open の位置の開き括弧からの引数の範囲(閉じ括弧が見つからなければ undefined) */
|
|
945
|
+
function callArguments(source, open) {
|
|
946
|
+
const args = [];
|
|
947
|
+
const limit = Math.min(source.length, open + 2000);
|
|
948
|
+
let depth = 0;
|
|
949
|
+
let argStart = open + 1;
|
|
950
|
+
for (let i = open + 1; i < limit; i++) {
|
|
951
|
+
const ch = source.charAt(i);
|
|
952
|
+
if (ch === '"' || ch === "'" || ch === "`") {
|
|
953
|
+
// C# の逐語的文字列(@"...")は "" が引用符、ほかは \ の後の文字が文字列の中
|
|
954
|
+
const verbatim = ch === '"' && source.charAt(i - 1) === "@";
|
|
955
|
+
let j = i + 1;
|
|
956
|
+
for (; j < limit; j++) {
|
|
957
|
+
const c = source.charAt(j);
|
|
958
|
+
if ((c === "\n" || c === "\r") && ch !== "`")
|
|
959
|
+
return undefined;
|
|
960
|
+
if (c === "\\" && !verbatim) {
|
|
961
|
+
j += 1;
|
|
962
|
+
}
|
|
963
|
+
else if (c === ch) {
|
|
964
|
+
if (verbatim && source.charAt(j + 1) === ch) {
|
|
965
|
+
j += 1;
|
|
966
|
+
}
|
|
967
|
+
else {
|
|
968
|
+
break;
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
i = j;
|
|
973
|
+
}
|
|
974
|
+
else if (ch === "(" || ch === "[" || ch === "{") {
|
|
975
|
+
depth += 1;
|
|
976
|
+
}
|
|
977
|
+
else if (ch === ")" || ch === "]" || ch === "}") {
|
|
978
|
+
if (depth === 0) {
|
|
979
|
+
if (ch !== ")")
|
|
980
|
+
return undefined;
|
|
981
|
+
args.push({ start: argStart, end: i });
|
|
982
|
+
return args.length === 1 && source.slice(argStart, i).trim() === "" ? [] : args;
|
|
983
|
+
}
|
|
984
|
+
depth -= 1;
|
|
985
|
+
}
|
|
986
|
+
else if (ch === "," && depth === 0) {
|
|
987
|
+
args.push({ start: argStart, end: i });
|
|
988
|
+
argStart = i + 1;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return undefined;
|
|
992
|
+
}
|
|
993
|
+
/** パスワードを位置で渡す定番の関数の、その位置の文字列を伏せる */
|
|
994
|
+
function maskPositionalCalls(source, findings) {
|
|
995
|
+
const spans = [];
|
|
996
|
+
for (const m of source.matchAll(POSITIONAL_CALL_NAME)) {
|
|
997
|
+
const at = m.index ?? 0;
|
|
998
|
+
const qualifier = /(?:[A-Za-z_$][\w$]*\.)*$/.exec(source.slice(Math.max(0, at - 120), at))?.[0] ?? "";
|
|
999
|
+
const name = `${qualifier}${m[1] ?? ""}`;
|
|
1000
|
+
const entry = POSITIONAL_SECRET_CALLS.find(([pattern]) => pattern.test(name));
|
|
1001
|
+
if (entry === undefined)
|
|
1002
|
+
continue;
|
|
1003
|
+
const args = callArguments(source, at + m[0].length - 1);
|
|
1004
|
+
const index = args === undefined ? undefined : entry[1](args.length);
|
|
1005
|
+
const arg = index === undefined ? undefined : args?.[index];
|
|
1006
|
+
if (arg === undefined)
|
|
1007
|
+
continue;
|
|
1008
|
+
const text = source.slice(arg.start, arg.end);
|
|
1009
|
+
const literal = ARG_LITERAL.exec(text);
|
|
1010
|
+
if (literal === null)
|
|
1011
|
+
continue;
|
|
1012
|
+
const value = literal[1] ?? "";
|
|
1013
|
+
const prefix = LITERAL_PREFIX.exec(value)?.[0] ?? "";
|
|
1014
|
+
const inner = value.slice(prefix.length + 1, -1);
|
|
1015
|
+
if (inner.trim() === "" || inner === MASK || isNonSecretValue(inner) || isCliNonSecret(inner))
|
|
1016
|
+
continue;
|
|
1017
|
+
const start = arg.start + text.indexOf(value) + prefix.length + 1;
|
|
1018
|
+
spans.push({ start, end: start + inner.length });
|
|
1019
|
+
}
|
|
1020
|
+
if (spans.length === 0)
|
|
1021
|
+
return source;
|
|
1022
|
+
let out = "";
|
|
1023
|
+
let last = 0;
|
|
1024
|
+
for (const span of spans.sort((x, y) => x.start - y.start)) {
|
|
1025
|
+
if (span.start < last)
|
|
1026
|
+
continue;
|
|
1027
|
+
findings.push({ line: lineOf(source, span.start), kind: KIND_PASSWORD });
|
|
1028
|
+
out += source.slice(last, span.start) + MASK;
|
|
1029
|
+
last = span.end;
|
|
1030
|
+
}
|
|
1031
|
+
return out + source.slice(last);
|
|
1032
|
+
}
|
|
1033
|
+
/**
|
|
1034
|
+
* 括弧を省いた呼び出しの文字列だけの文(Gradle の credentials { password '...' } / username "..." の行)。
|
|
1035
|
+
* 行の頭(前に { で開いたブロックの名前があってもよい)の名前と文字列だけのもの
|
|
1036
|
+
*/
|
|
1037
|
+
const COMMAND_CALL_LITERAL = new RegExp(String.raw `(^[ \t]*(?:[A-Za-z_][\w.]*[ \t]*\{[ \t]*)*)([A-Za-z_]\w*)([ \t]+)` + KEY_VALUE + String.raw `(?=[ \t]*(?:[;}]|\/\/|\r?$))`, "gm");
|
|
1038
|
+
/**
|
|
1039
|
+
* 名前と値を属性に書く要素(Java の <param name="password" value="..."/>・Spring の <property name="password" value="..."/> /
|
|
1040
|
+
* <entry key="password" value="..."/> / <constructor-arg name="password" value="..."/>)。.NET の <add key value> は CONFIG_ADD
|
|
1041
|
+
*/
|
|
1042
|
+
const NAMED_VALUE_TAG = new RegExp(`<(?:param|property|parameter|entry|constructor-arg|arg)\\b${TAG_BODY}>`, "gi");
|
|
1043
|
+
const NAMED_VALUE_ATTR = /(?<![\w-])(name|key|value)(\s*=\s*)("[^"]*"|'[^']*')/gi;
|
|
1044
|
+
/**
|
|
1045
|
+
* 設定の既定値の属性(C# の Settings.Designer.cs の [DefaultSettingValueAttribute("...")] と、汎用テキストとして伏せる
|
|
1046
|
+
* .vb の <DefaultSettingValueAttribute("...")>)。名前は続くプロパティで決める(VB のソースは maskVbSource が読む)
|
|
1047
|
+
*/
|
|
1048
|
+
const DEFAULT_SETTING_LITERAL = new RegExp(String.raw `(DefaultSettingValue(?:Attribute)?[ \t]*\([ \t]*)` + KEY_VALUE + String.raw `(?=[ \t]*\)[ \t]*[\]>])`, "g");
|
|
1049
|
+
const DEFAULT_SETTING_TARGET = /^[ \t]*\)[ \t]*[\]>][\s\S]{0,400}?(?:\bProperty[ \t]+\[?([A-Za-z_]\w*)|[\w.<>?]+[ \t]+([A-Za-z_]\w*)[ \t]*(?:\r?\n[ \t]*)?\{)/;
|
|
1050
|
+
/** KEY_VALUE の文字列の接頭辞・引用符・中身 */
|
|
1051
|
+
function literalParts(literal) {
|
|
1052
|
+
const prefix = LITERAL_PREFIX.exec(literal)?.[0] ?? "";
|
|
1053
|
+
return { prefix, quote: literal.charAt(prefix.length), inner: literal.slice(prefix.length + 1, -1) };
|
|
1054
|
+
}
|
|
1055
|
+
/** 文字列の中身を伏せた形(接頭辞と引用符は残す) */
|
|
1056
|
+
function maskedLiteral(literal) {
|
|
1057
|
+
const { prefix, quote } = literalParts(literal);
|
|
1058
|
+
return `${prefix}${quote}${MASK}${quote}`;
|
|
1059
|
+
}
|
|
1060
|
+
/** コードの形の値を伏せる(setter・builder、既定値、比較、位置で渡す定番の関数、括弧を省いた呼び出し、名前と値の属性、設定の既定値) */
|
|
1061
|
+
function maskCodeShapes(source, findings) {
|
|
1062
|
+
let result = source;
|
|
1063
|
+
const secret = (key, inner) => isSecretPair(key, inner) && !isTypeName(inner);
|
|
1064
|
+
result = result.replace(SECRET_SETTER, (whole, lead, callee, literal, offset) => {
|
|
1065
|
+
const key = secretSetterKey(callee);
|
|
1066
|
+
if (key === undefined || !secret(key, literalParts(literal).inner))
|
|
1067
|
+
return whole;
|
|
1068
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
1069
|
+
return `${lead}${maskedLiteral(literal)}`;
|
|
1070
|
+
});
|
|
1071
|
+
{
|
|
1072
|
+
const before = result;
|
|
1073
|
+
result = before.replace(DEFAULT_LITERAL, (whole, op, gap, literal, offset) => {
|
|
1074
|
+
const { inner } = literalParts(literal);
|
|
1075
|
+
if (inner === "" || inner === MASK)
|
|
1076
|
+
return whole;
|
|
1077
|
+
// 行の頭から(長い行は直前の 300 文字。1 行の大きいファイルで行の頭まで探し直さないよう、先に切り出す)
|
|
1078
|
+
const near = before.slice(Math.max(0, offset - 300), offset);
|
|
1079
|
+
if (!defaultValueKeys(near.slice(near.lastIndexOf("\n") + 1)).some((key) => secret(key, inner)))
|
|
1080
|
+
return whole;
|
|
1081
|
+
findings.push({ line: lineOf(before, offset), kind: KIND_PASSWORD });
|
|
1082
|
+
return `${op}${gap}${maskedLiteral(literal)}`;
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1085
|
+
for (const pattern of [COMPARE_LITERAL, EQUALS_LITERAL]) {
|
|
1086
|
+
result = result.replace(pattern, (whole, lead, chain, literal, offset) => {
|
|
1087
|
+
if (!secret(compareKey(chain), literalParts(literal).inner))
|
|
1088
|
+
return whole;
|
|
1089
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
1090
|
+
return `${lead}${maskedLiteral(literal)}`;
|
|
1091
|
+
});
|
|
1092
|
+
}
|
|
1093
|
+
for (const pattern of [COMPARE_LITERAL_LEFT, EQUALS_LITERAL_LEFT]) {
|
|
1094
|
+
result = result.replace(pattern, (whole, literal, tail, chain, offset) => {
|
|
1095
|
+
if (!secret(compareKey(chain), literalParts(literal).inner))
|
|
1096
|
+
return whole;
|
|
1097
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
1098
|
+
return `${maskedLiteral(literal)}${tail}`;
|
|
1099
|
+
});
|
|
1100
|
+
}
|
|
1101
|
+
result = maskPositionalCalls(result, findings);
|
|
1102
|
+
result = result.replace(COMMAND_CALL_LITERAL, (whole, lead, key, gap, literal, offset) => {
|
|
1103
|
+
if (!secret(key, literalParts(literal).inner))
|
|
1104
|
+
return whole;
|
|
1105
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
1106
|
+
return `${lead}${key}${gap}${maskedLiteral(literal)}`;
|
|
1107
|
+
});
|
|
1108
|
+
result = result.replace(NAMED_VALUE_TAG, (tag, offset) => {
|
|
1109
|
+
let key;
|
|
1110
|
+
let value;
|
|
1111
|
+
for (const attr of tag.matchAll(NAMED_VALUE_ATTR)) {
|
|
1112
|
+
const inner = (attr[3] ?? "").slice(1, -1);
|
|
1113
|
+
if (attr[1]?.toLowerCase() === "value")
|
|
1114
|
+
value ??= inner.trim();
|
|
1115
|
+
else
|
|
1116
|
+
key ??= inner;
|
|
1117
|
+
}
|
|
1118
|
+
// 設定の値なので token / credentials のキーも値によらず伏せる(appSettings と同じ)
|
|
1119
|
+
if (key === undefined || value === undefined || kindForConfigKey(key) !== KIND_PASSWORD)
|
|
1120
|
+
return tag;
|
|
1121
|
+
if (value === "" || value === MASK || isNonSecretValue(value))
|
|
1122
|
+
return tag;
|
|
1123
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
1124
|
+
return tag.replace(NAMED_VALUE_ATTR, (attr, name, eq, quoted) => name.toLowerCase() === "value" ? `${name}${eq}${quoted.charAt(0)}${MASK}${quoted.charAt(0)}` : attr);
|
|
1125
|
+
});
|
|
1126
|
+
{
|
|
1127
|
+
const before = result;
|
|
1128
|
+
result = before.replace(DEFAULT_SETTING_LITERAL, (whole, lead, literal, offset) => {
|
|
1129
|
+
const { inner } = literalParts(literal);
|
|
1130
|
+
const target = DEFAULT_SETTING_TARGET.exec(before.slice(offset + whole.length, offset + whole.length + 500));
|
|
1131
|
+
const kind = target === null ? undefined : kindForConfigKey(target[1] ?? target[2] ?? "");
|
|
1132
|
+
if (kind === undefined || inner.trim() === "" || inner === MASK || isNonSecretValue(inner))
|
|
1133
|
+
return whole;
|
|
1134
|
+
findings.push({ line: lineOf(before, offset), kind });
|
|
1135
|
+
return `${lead}${maskedLiteral(literal)}`;
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
return result;
|
|
1139
|
+
}
|
|
503
1140
|
/** 設定のキー名の最後の語(と最後の 2 語を続けたもの)がこれらなら秘密系 / ユーザー ID 系 */
|
|
504
1141
|
const SECRET_KEY_WORDS = new Set([
|
|
505
1142
|
"password",
|
|
@@ -522,7 +1159,23 @@ const USER_KEY_WORDS = new Set(["userid", "username", "uid", "user"]);
|
|
|
522
1159
|
const WEAK_KEY_WORDS = new Set(["token", "credential", "credentials"]);
|
|
523
1160
|
/** 秘密ではない値: 真偽値(SavePassword = True / RequireToken = "true" などの設定)と書式の差し込みだけの値 */
|
|
524
1161
|
function isNonSecretValue(value) {
|
|
525
|
-
return /^(?:true|false)$/i.test(value.trim()) ||
|
|
1162
|
+
return (/^(?:true|false)$/i.test(value.trim()) ||
|
|
1163
|
+
isPlaceholderOnly(value) ||
|
|
1164
|
+
isValidationRules(value) ||
|
|
1165
|
+
// Laravel のモデルの型変換('password' => 'hashed')
|
|
1166
|
+
/^(?:hashed|encrypted)$/.test(value));
|
|
1167
|
+
}
|
|
1168
|
+
/** 入力の検証の規則の名前(Laravel など) */
|
|
1169
|
+
const VALIDATION_RULE = /^(?:bail|required|nullable|sometimes|present|filled|confirmed|string|integer|numeric|alpha|alpha_num|alpha_dash|ascii|email|boolean|uuid|min|max|between|size|digits|digits_between|regex|not_regex|same|different|current_password|password|letters|mixed_case|numbers|symbols|uncompromised|required_with|required_if|required_unless|exclude|prohibited)(?::[^|\r\n]*)?$/;
|
|
1170
|
+
/**
|
|
1171
|
+
* 入力の検証の規則だけでできた値(v0.19.0。Laravel などの 'password' => 'required|string|min:8|confirmed')。秘密の値では
|
|
1172
|
+
* ない。規則が 1 つだけの値は、パスワードにはまず使わない名前(required / nullable / sometimes / confirmed)だけ
|
|
1173
|
+
*/
|
|
1174
|
+
function isValidationRules(value) {
|
|
1175
|
+
const rules = value.split("|");
|
|
1176
|
+
return rules.length > 1
|
|
1177
|
+
? rules.every((rule) => VALIDATION_RULE.test(rule))
|
|
1178
|
+
: /^(?:required|nullable|sometimes|confirmed)$/.test(value);
|
|
526
1179
|
}
|
|
527
1180
|
/**
|
|
528
1181
|
* 書式の差し込み(%s / {} / {0} / {1,-8:D} / {:.2f} / ${x})だけでできた値('%s:%s' など)。秘密ではないため伏せない。
|
|
@@ -721,6 +1374,65 @@ function isPartlyMasked(token) {
|
|
|
721
1374
|
}
|
|
722
1375
|
/** PRIVATE KEY ブロック(BEGIN/END 行は残し中身をマスク) */
|
|
723
1376
|
const PRIVATE_KEY_BLOCK = /(-----BEGIN [A-Z ]*PRIVATE KEY-----)[\s\S]+?(-----END [A-Z ]*PRIVATE KEY-----)/g;
|
|
1377
|
+
/**
|
|
1378
|
+
* SQL*Plus / RMAN のスクリプトの CONNECT 行(v0.19.0): CONNECT ユーザー/パスワード[@接続先] [AS SYSDBA] / CONN ... /
|
|
1379
|
+
* CONNECT TARGET ...。行がその文だけのもの(「Connect to/from the server」のような文章を読まない)
|
|
1380
|
+
*/
|
|
1381
|
+
const ORACLE_CONNECT = new RegExp(String.raw `^([ \t]*conn(?:ect)?[ \t]+(?:(?:target|catalog|auxiliary)[ \t]+)?["']?${ORACLE_USER}\/)` +
|
|
1382
|
+
String.raw `("[^"\r\n]*"|[^\s"'@\/;]+)` +
|
|
1383
|
+
String.raw `(?=(?:@[^\s;"']+)?["']?(?:[ \t]+as[ \t]+sys\w+)?[ \t]*;?[ \t]*(?:--[^\r\n]*)?\r?$)`, "gim");
|
|
1384
|
+
/**
|
|
1385
|
+
* SQL のパスワードの句(v0.19.0): Oracle / MySQL の IDENTIFIED BY 値(IDENTIFIED WITH 方式 BY 値・
|
|
1386
|
+
* IDENTIFIED BY VALUES 'ハッシュ' も)と、PostgreSQL の [WITH | LOGIN | USER 名 | ROLE 名] [ENCRYPTED] PASSWORD '値'。
|
|
1387
|
+
* VB / Java などの文字列の中の SQL も読む(値は文字列を閉じる引用符の前まで)
|
|
1388
|
+
*/
|
|
1389
|
+
const SQL_IDENTIFIED_BY = /\b(IDENTIFIED(?:[ \t]+WITH[ \t]+[\w$]+)?[ \t]+BY[ \t]+(?:VALUES[ \t]+)?)('(?:[^'\r\n]|'')*'|"[^"\r\n]*"|[^\s;'",)]+)/gi;
|
|
1390
|
+
/**
|
|
1391
|
+
* IDENTIFIED BY の前のユーザー・ロールの名前(CREATE USER 名 / ALTER ROLE 名 / GRANT ... TO 名 / CONNECT TO 名 /
|
|
1392
|
+
* MySQL の 'u'@'h')。小文字の identified by はこれが前にあるときだけ SQL とみなす(文章の「identified by a single
|
|
1393
|
+
* character」を読まない)。大文字の IDENTIFIED BY は前を問わない(VB の "CREATE USER " & u & " IDENTIFIED BY ..." も)
|
|
1394
|
+
*/
|
|
1395
|
+
const SQL_GRANTEE_BEFORE = /\b(?:user|role|to)[ \t]+(?:if[ \t]+(?:not[ \t]+)?exists[ \t]+)?(?:"[^"\r\n]+"|'[^'\r\n]+'(?:@'[^'\r\n]*')?|`[^`\r\n]+`(?:@`[^`\r\n]*`)?|[\w$#.]+(?:@[\w%.]+)?)[ \t]+$/i;
|
|
1396
|
+
const SQL_PASSWORD = /\b((?:WITH|LOGIN|USER[ \t]+[\w$."`]+|ROLE[ \t]+[\w$."`]+)(?:[ \t]+(?:LOGIN|NOLOGIN|SUPERUSER|NOSUPERUSER|CREATEDB|CREATEROLE|INHERIT|NOINHERIT|REPLICATION))*[ \t]+(?:ENCRYPTED[ \t]+)?PASSWORD[ \t]+)('(?:[^'\r\n]|'')*')/gi;
|
|
1397
|
+
/**
|
|
1398
|
+
* SQL のパスワードの句の値のうち伏せないもの: 変数・差し込み・バインド変数(&pw / :pw / ? / %s / {0} / ${x})・
|
|
1399
|
+
* 説明の書き方(<password> / [password] / password)・コードの連結(' + pw + ' / " & pw & ")
|
|
1400
|
+
*/
|
|
1401
|
+
function isSqlNonSecret(value) {
|
|
1402
|
+
const quote = /^['"]/.test(value) && value.length >= 2 && value.endsWith(value.charAt(0)) ? value.charAt(0) : "";
|
|
1403
|
+
const inner = quote === "" ? value : value.slice(1, -1);
|
|
1404
|
+
return (isCliNonSecret(value) ||
|
|
1405
|
+
/^(?::[A-Za-z_]\w*|\?|&\w+\.?|@\w+)$/.test(inner) ||
|
|
1406
|
+
/^(?:password|passwd|pwd|pass|new_password|newpassword)$/i.test(inner) ||
|
|
1407
|
+
(quote !== "" && /["'`]/.test(inner)));
|
|
1408
|
+
}
|
|
1409
|
+
/** Oracle の CONNECT 行と SQL のパスワードの句の値を伏せる(VB のソースと汎用テキストの両方) */
|
|
1410
|
+
function maskSqlCredentials(source, findings) {
|
|
1411
|
+
let result = source.replace(ORACLE_CONNECT, (whole, lead, value, offset) => {
|
|
1412
|
+
if (isCliNonSecret(value) || /^&/.test(value))
|
|
1413
|
+
return whole;
|
|
1414
|
+
findings.push({ line: lineOf(source, offset), kind: KIND_PASSWORD });
|
|
1415
|
+
const quote = /^"/.test(value) ? '"' : "";
|
|
1416
|
+
return `${lead}${quote}${MASK}${quote}`;
|
|
1417
|
+
});
|
|
1418
|
+
for (const pattern of [SQL_IDENTIFIED_BY, SQL_PASSWORD]) {
|
|
1419
|
+
const before = result;
|
|
1420
|
+
result = before.replace(pattern, (whole, lead, value, offset) => {
|
|
1421
|
+
if (value.includes(MASK) || isSqlNonSecret(value))
|
|
1422
|
+
return whole;
|
|
1423
|
+
if (pattern === SQL_IDENTIFIED_BY && !lead.startsWith("IDENTIFIED")) {
|
|
1424
|
+
// 行の頭から(長い行は直前の 300 文字)。行の頭は lastIndexOf で探す(文章に多いときに遅くならないように)
|
|
1425
|
+
const lineStart = Math.max(before.lastIndexOf("\n", offset - 1) + 1, offset - 300, 0);
|
|
1426
|
+
if (!SQL_GRANTEE_BEFORE.test(before.slice(lineStart, offset)))
|
|
1427
|
+
return whole;
|
|
1428
|
+
}
|
|
1429
|
+
findings.push({ line: lineOf(before, offset), kind: KIND_PASSWORD });
|
|
1430
|
+
const quote = /^['"]/.test(value) && value.length >= 2 ? value.charAt(0) : "";
|
|
1431
|
+
return `${lead}${quote}${MASK}${quote}`;
|
|
1432
|
+
});
|
|
1433
|
+
}
|
|
1434
|
+
return result;
|
|
1435
|
+
}
|
|
724
1436
|
/**
|
|
725
1437
|
* 高エントロピー文字列の候補(英数と base64/URL-safe 記号の 20 文字以上)。
|
|
726
1438
|
* 判定は looksHighEntropy で絞る
|
|
@@ -880,6 +1592,47 @@ function callArgKind(tail) {
|
|
|
880
1592
|
}
|
|
881
1593
|
return undefined;
|
|
882
1594
|
}
|
|
1595
|
+
/**
|
|
1596
|
+
* VB の呼び出しの引数の位置で決まる種別(v0.20.0)。tail は文字列の直前までのコード、after は文字列の後ろ。
|
|
1597
|
+
* - If(式, "既定値") の既定値: 式の中の名前・キーの形の文字列か、If の結果の代入の先が秘密系なら
|
|
1598
|
+
* (If(Environment.GetEnvironmentVariable("DB_PASSWORD"), "...") / Dim password = If(x, "..."))。3 つの引数の If は除く
|
|
1599
|
+
* - パスワードを位置で渡す定番の関数(client.Authenticate("user", "pw") / AuthenticateAsync)の 2 番目の引数
|
|
1600
|
+
*/
|
|
1601
|
+
function vbCallPositionKind(tail, after) {
|
|
1602
|
+
const line = tail.slice(tail.lastIndexOf("\n") + 1);
|
|
1603
|
+
// 文字列の中の括弧・カンマを数えないよう、同じ長さの _ に替える
|
|
1604
|
+
const plain = line.replace(/"(?:[^"\r\n]|"")*"/g, (s) => `"${"_".repeat(s.length - 2)}"`);
|
|
1605
|
+
let depth = 0;
|
|
1606
|
+
let commas = 0;
|
|
1607
|
+
let firstComma = -1;
|
|
1608
|
+
for (let i = plain.length - 1; i >= 0; i--) {
|
|
1609
|
+
const ch = plain.charAt(i);
|
|
1610
|
+
if (ch === ")") {
|
|
1611
|
+
depth += 1;
|
|
1612
|
+
}
|
|
1613
|
+
else if (ch === "(") {
|
|
1614
|
+
if (depth > 0) {
|
|
1615
|
+
depth -= 1;
|
|
1616
|
+
continue;
|
|
1617
|
+
}
|
|
1618
|
+
const found = /((?:[A-Za-z_]\w*\.)*[A-Za-z_]\w*)\s*$/.exec(plain.slice(0, i));
|
|
1619
|
+
const callee = found?.[1] ?? "";
|
|
1620
|
+
if (/^if$/i.test(callee) && commas === 1 && /^\s*\)/.test(after)) {
|
|
1621
|
+
const keys = [
|
|
1622
|
+
...[...line.slice(i + 1, firstComma).matchAll(/"([A-Za-z_][\w.:-]*)"|([A-Za-z_]\w*)\s*(?:\(\s*\))?\s*$/g)].map((m) => m[1] ?? m[2] ?? ""),
|
|
1623
|
+
ASSIGNMENT_TAIL.exec(line.slice(0, found?.index ?? i))?.[1] ?? "",
|
|
1624
|
+
];
|
|
1625
|
+
return keys.some((key) => kindForIdentifier(key) === KIND_PASSWORD) ? KIND_PASSWORD : undefined;
|
|
1626
|
+
}
|
|
1627
|
+
return /\.authenticate(?:async)?$/i.test(callee) && commas === 1 ? KIND_PASSWORD : undefined;
|
|
1628
|
+
}
|
|
1629
|
+
else if (ch === "," && depth === 0) {
|
|
1630
|
+
commas += 1;
|
|
1631
|
+
firstComma = i;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
return undefined;
|
|
1635
|
+
}
|
|
883
1636
|
/** コメント内のマスク(コメントアウトされた認証情報コードも対象にする) */
|
|
884
1637
|
function maskCommentText(comment, findings, line, strict) {
|
|
885
1638
|
let result = comment.replace(COMMENT_ASSIGN, (whole, identifier, assign, _value, close) => {
|
|
@@ -972,6 +1725,10 @@ function maskVbSource(content, findings, strict) {
|
|
|
972
1725
|
? kindForIdentifier(namedArg[1] ?? "")
|
|
973
1726
|
: callArgKind(tail);
|
|
974
1727
|
}
|
|
1728
|
+
// If(環境変数, "既定値") の既定値と、パスワードを位置で渡す定番の関数(client.Authenticate("user", "pw"))
|
|
1729
|
+
if (kind === undefined) {
|
|
1730
|
+
kind = vbCallPositionKind(tail, content.slice(j + 1, j + 80));
|
|
1731
|
+
}
|
|
975
1732
|
// My.Settings の既定値(続くプロパティの名前で決める)
|
|
976
1733
|
if (kind === undefined && DEFAULT_SETTING_CALLEE.test(tail)) {
|
|
977
1734
|
const property = DEFAULT_SETTING_PROPERTY.exec(content.slice(j + 1, j + 400));
|
|
@@ -1105,7 +1862,7 @@ function maskElementText(value) {
|
|
|
1105
1862
|
return `${lead}${MASK}${value.slice(lead.length + inner.length)}`;
|
|
1106
1863
|
}
|
|
1107
1864
|
/** 非 VB ファイル(config / XML / テキスト等)のマスク */
|
|
1108
|
-
function maskGenericText(content, findings, strict) {
|
|
1865
|
+
function maskGenericText(content, findings, strict, colonPairs = true) {
|
|
1109
1866
|
// .NET の設定ファイルの appSettings / My.Settings(キー名と値が別の属性・要素にある)
|
|
1110
1867
|
let result = maskConfigAdds(content, findings, (index) => lineOf(content, index));
|
|
1111
1868
|
result = result.replace(SETTING_VALUE, (whole, open, middle, value, close, offset) => {
|
|
@@ -1192,6 +1949,10 @@ function maskGenericText(content, findings, strict) {
|
|
|
1192
1949
|
const shown = quoted ? `${quote}${MASK}${quote}` : MASK;
|
|
1193
1950
|
return `${lead}${key}${eq}${rest.slice(0, at)}${shown}${rest.slice(at + value.length)}${closing}`;
|
|
1194
1951
|
});
|
|
1952
|
+
// YAML・.properties・setup.cfg などの引用符で囲まない `キー: 値`(コードのファイルでは読まない)
|
|
1953
|
+
if (colonPairs) {
|
|
1954
|
+
result = maskColonPairs(result, findings);
|
|
1955
|
+
}
|
|
1195
1956
|
// バッチファイルの set(値の空白も値のうちなので表示の文言とはみなさない)。SQL の SET を飛ばしたときと、文字列・
|
|
1196
1957
|
// 括弧の中で値を途中で終えたときは、その後ろから続けて探す(同じ行の後ろの set も読む)
|
|
1197
1958
|
{
|
|
@@ -1245,8 +2006,10 @@ function maskGenericText(content, findings, strict) {
|
|
|
1245
2006
|
findings.push({ line: lineOf(result, offset), kind: kindForConfigKey(key) ?? KIND_PASSWORD });
|
|
1246
2007
|
return `${flag}${quote}${key}=${shown}${quote}`;
|
|
1247
2008
|
});
|
|
1248
|
-
// パスワードを引数で渡すコマンド(sqlcmd -P ... / mysql -p... / net use \\srv\share ... / schtasks /RP ...)
|
|
2009
|
+
// パスワードを引数で渡すコマンド(sqlcmd -P ... / mysql -p... / net use \\srv\share ... / schtasks /RP ... / curl -u ...)
|
|
1249
2010
|
result = maskCliArgs(result, findings);
|
|
2011
|
+
// PowerShell の ConvertTo-SecureString "..." -AsPlainText
|
|
2012
|
+
result = maskSecureStrings(result, findings);
|
|
1250
2013
|
// XML 属性形式(値が引用符で囲まれるため接続文字列パターンでは拾えない)
|
|
1251
2014
|
for (const regex of [ATTR_SECRET, ATTR_SECRET_SINGLE]) {
|
|
1252
2015
|
result = result.replace(regex, (whole, key, eq, value, closeQuote, offset) => {
|
|
@@ -1297,7 +2060,7 @@ function maskGenericText(content, findings, strict) {
|
|
|
1297
2060
|
return whole;
|
|
1298
2061
|
}
|
|
1299
2062
|
}
|
|
1300
|
-
const prefix =
|
|
2063
|
+
const prefix = LITERAL_PREFIX.exec(literal)?.[0] ?? "";
|
|
1301
2064
|
const quote = literal.charAt(prefix.length);
|
|
1302
2065
|
if (!isSecretPair(name, literal.slice(prefix.length + 1, -1))) {
|
|
1303
2066
|
return whole;
|
|
@@ -1307,10 +2070,25 @@ function maskGenericText(content, findings, strict) {
|
|
|
1307
2070
|
};
|
|
1308
2071
|
// キーと文字列の値("password": "..." / dbPassword = "..." / { apiKey: '...' } / password: string = "...")
|
|
1309
2072
|
result = result.replace(KEY_LITERAL, maskKeyLiteral);
|
|
2073
|
+
// 添字への代入と、秘密の名前を最初の引数に渡す関数の 2 番目の文字列(型の名前・表示の文言は除く)
|
|
2074
|
+
for (const pattern of [SUBSCRIPT_LITERAL, NAMED_ARG_LITERAL]) {
|
|
2075
|
+
result = result.replace(pattern, (whole, open, key, sep, literal, offset) => {
|
|
2076
|
+
const prefix = LITERAL_PREFIX.exec(literal)?.[0] ?? "";
|
|
2077
|
+
const quote = literal.charAt(prefix.length);
|
|
2078
|
+
const inner = literal.slice(prefix.length + 1, -1);
|
|
2079
|
+
if (!isSecretPair(key.slice(1, -1), inner) || isTypeName(inner)) {
|
|
2080
|
+
return whole;
|
|
2081
|
+
}
|
|
2082
|
+
findings.push({ line: lineOf(result, offset), kind: KIND_PASSWORD });
|
|
2083
|
+
return `${open}${key}${sep}${prefix}${quote}${MASK}${quote}`;
|
|
2084
|
+
});
|
|
2085
|
+
}
|
|
1310
2086
|
// : の後に空白のない注釈は、token などのキーでは型が文字列のときだけ(last_synced_token:label="..." は書式指定)
|
|
1311
2087
|
result = result.replace(KEY_LITERAL_TYPED, (whole, key, sep, literal, offset) => /:[^ \t]/.test(sep) && !hasStrongSecretWord(key) && !/:(?:str|string|String|bytes|SecretStr|Optional\b)/.test(sep)
|
|
1312
2088
|
? whole
|
|
1313
2089
|
: maskKeyLiteral(whole, key, sep, literal, offset));
|
|
2090
|
+
// コードの形(v0.20.0。setter・builder・既定値・比較・位置で渡す定番の関数・Gradle・<param>・設定の既定値)
|
|
2091
|
+
result = maskCodeShapes(result, findings);
|
|
1314
2092
|
result = maskConnstrText(result, findings, (index) => lineOf(result, index));
|
|
1315
2093
|
if (strict) {
|
|
1316
2094
|
result = maskEntropyText(result, findings, (index) => lineOf(result, index));
|
|
@@ -1328,11 +2106,13 @@ export function maskCredentials(content, options) {
|
|
|
1328
2106
|
const strict = options.strict ?? true;
|
|
1329
2107
|
let result = options.vbSource
|
|
1330
2108
|
? maskVbSource(content, findings, strict)
|
|
1331
|
-
: maskGenericText(content, findings, strict);
|
|
2109
|
+
: maskGenericText(content, findings, strict, options.path === undefined || !isCodePath(options.path));
|
|
1332
2110
|
if (options.vbSource) {
|
|
1333
2111
|
// VB の文字列の中のコマンド(Shell("net use ...") / Process.Start("sqlcmd.exe", "-S srv -P ..."))
|
|
1334
2112
|
result = maskCliArgs(result, findings);
|
|
1335
2113
|
}
|
|
2114
|
+
// SQL*Plus の CONNECT 行・SQL の IDENTIFIED BY / PASSWORD の句(スクリプトとコードの文字列の中)
|
|
2115
|
+
result = maskSqlCredentials(result, findings);
|
|
1336
2116
|
// ファイル種別によらない汎用シークレット形式
|
|
1337
2117
|
result = result.replace(PRIVATE_KEY_BLOCK, (whole, begin, end, offset) => {
|
|
1338
2118
|
findings.push({ line: lineOf(result, offset), kind: KIND_PRIVATE_KEY });
|
|
@@ -1479,11 +2259,11 @@ export function hasMaskToken(line) {
|
|
|
1479
2259
|
*/
|
|
1480
2260
|
function maskModes(path, stricts) {
|
|
1481
2261
|
const vbs = /\.vb$/i.test(path) ? [true, false] : [false];
|
|
1482
|
-
const modes = vbs.flatMap((vbSource) => stricts.map((strict) => ({ vbSource, strict })));
|
|
2262
|
+
const modes = vbs.flatMap((vbSource) => stricts.map((strict) => ({ vbSource, strict, path })));
|
|
1483
2263
|
// 言語の分かるソースは、言語ごとの伏せ方 (tazuna pack) と汎用テキストの伏せ方 (旧 slnmix のパック) の両方
|
|
1484
2264
|
const language = sourceLanguageOf(path);
|
|
1485
2265
|
if (language !== undefined) {
|
|
1486
|
-
modes.push(...stricts.map((strict) => ({ vbSource: false, strict, language })));
|
|
2266
|
+
modes.push(...stricts.map((strict) => ({ vbSource: false, strict, language, path })));
|
|
1487
2267
|
}
|
|
1488
2268
|
return modes;
|
|
1489
2269
|
}
|
|
@@ -1598,7 +2378,9 @@ function displaySpans(text, options) {
|
|
|
1598
2378
|
// strict のときは両方の範囲を伏せる
|
|
1599
2379
|
const stricts = (options.strict ?? true) ? [true, false] : [false];
|
|
1600
2380
|
const vbSource = options.vbSource;
|
|
1601
|
-
const modes = vbSource === undefined
|
|
2381
|
+
const modes = vbSource === undefined
|
|
2382
|
+
? maskModes(options.path, stricts)
|
|
2383
|
+
: stricts.map((strict) => ({ vbSource, strict, path: options.path }));
|
|
1602
2384
|
for (const mode of modes) {
|
|
1603
2385
|
const spans = maskedSpans(text, mode);
|
|
1604
2386
|
if (spans === null)
|
|
@@ -165,7 +165,12 @@ function buildUiSummaryEntry(node, displayPath, deps, maskEnabled, strictMask) {
|
|
|
165
165
|
// C# の Designer.cs はパックの .cs と同じく、文字列・コメントの中だけを伏せる
|
|
166
166
|
const masked = language === "vb"
|
|
167
167
|
? maskCredentials(source, { vbSource: true, strict: strictMask })
|
|
168
|
-
: maskCredentials(source, {
|
|
168
|
+
: maskCredentials(source, {
|
|
169
|
+
vbSource: false,
|
|
170
|
+
language: sourceLanguageOf(fileName),
|
|
171
|
+
strict: strictMask,
|
|
172
|
+
path: fileName,
|
|
173
|
+
});
|
|
169
174
|
source = masked.content;
|
|
170
175
|
findings = masked.findings;
|
|
171
176
|
}
|
|
@@ -387,6 +392,7 @@ export function buildRepomixOutput(title, sources, deps, options) {
|
|
|
387
392
|
vbSource: /\.vb$/i.test(displayPath),
|
|
388
393
|
language: sourceLanguageOf(displayPath),
|
|
389
394
|
strict: strictMask,
|
|
395
|
+
path: displayPath,
|
|
390
396
|
});
|
|
391
397
|
content = masked.content;
|
|
392
398
|
if (masked.findings.length > 0) {
|
|
@@ -524,6 +530,7 @@ export function buildRepomixOutput(title, sources, deps, options) {
|
|
|
524
530
|
vbSource: false,
|
|
525
531
|
language: sourceLanguageOf(file.relativePath),
|
|
526
532
|
strict: strictMask,
|
|
533
|
+
path: file.relativePath,
|
|
527
534
|
});
|
|
528
535
|
content = masked.content;
|
|
529
536
|
if (masked.findings.length > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tazuna",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.0",
|
|
4
4
|
"description": "Build with AI chat, without handing over the reins: pack your codebase for the chat, then verify and apply its patches locally (zero dependencies; formerly petari and slnmix)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|