Skip to content

Commit d148f9e

Browse files
authored
非セキュアコンテクスト下でのMath:gen_rngのデフォルトアルゴリズムを調整 (#960)
1 parent a4a0b33 commit d148f9e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/interpreter/lib/std.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,8 @@ export const std: Record<string, Value> = {
459459

460460
'Math:gen_rng': FN_NATIVE(async ([seed, options]) => {
461461
expectAny(seed);
462-
let algo = 'chacha20';
462+
const isSecureContext = 'subtle' in crypto;
463+
let algo = isSecureContext ? 'chacha20' : 'rc4_legacy';
463464
if (options?.type === 'obj') {
464465
const v = options.value.get('algorithm');
465466
if (v?.type !== 'str') throw new AiScriptRuntimeError('`options.algorithm` must be string.');
@@ -472,10 +473,14 @@ export const std: Record<string, Value> = {
472473
switch (algo) {
473474
case 'rc4_legacy':
474475
return GenerateLegacyRandom(seed);
475-
case 'rc4':
476+
case 'rc4': {
477+
if (!isSecureContext) throw new AiScriptRuntimeError(`The random algorithm ${algo} cannot be used because \`crypto.subtle\` is not available. Maybe in non-secure context?`);
476478
return GenerateRC4Random(seed);
477-
case 'chacha20':
479+
}
480+
case 'chacha20': {
481+
if (!isSecureContext) throw new AiScriptRuntimeError(`The random algorithm ${algo} cannot be used because \`crypto.subtle\` is not available. Maybe in non-secure context?`);
478482
return await GenerateChaCha20Random(seed);
483+
}
479484
default:
480485
throw new AiScriptRuntimeError('`options.algorithm` must be one of these: `chacha20`, `rc4`, or `rc4_legacy`.');
481486
}

unreleased/fix-gen_rng-default.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix: `Math:gen_rng`のアルゴリズム`chacha20`および`rc4`が非セキュアコンテクスト下では動作しないため、そのような環境下では`options.algorithm`のデフォルトを`rc4_legacy`に変更

0 commit comments

Comments
 (0)