Skip to content

Commit ee135b7

Browse files
Fix code scanning alert no. 27: DOM text reinterpreted as HTML (#801)
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 443335c commit ee135b7

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

packages/frontend/src/scripts/sound.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import { RateLimiter } from '@/scripts/rate-limiter.js';
1111
let ctx: AudioContext;
1212
const cache = new Map<string, AudioBuffer>();
1313

14+
function isValidUrl(url: string): boolean {
15+
try {
16+
new URL(url);
17+
return true;
18+
} catch (_) {
19+
return false;
20+
}
21+
}
22+
1423
export const soundsTypes = [
1524
// 音声なし
1625
null,
@@ -260,8 +269,12 @@ export function createSourceNode(buffer: AudioBuffer, opts: {
260269
*/
261270
export async function getSoundDuration(file: string): Promise<number> {
262271
const audioEl = document.createElement('audio');
263-
audioEl.src = file;
264-
return new Promise((resolve) => {
272+
audioEl.src = isValidUrl(file) ? file : '';
273+
return new Promise((resolve, reject) => {
274+
if (!audioEl.src) {
275+
reject(new Error('Invalid URL'));
276+
return;
277+
}
265278
const si = setInterval(() => {
266279
if (audioEl.readyState > 0) {
267280
resolve(audioEl.duration * 1000);

0 commit comments

Comments
 (0)