Skip to content

Commit

Permalink
Fix code scanning alert no. 27: DOM text reinterpreted as HTML (#801)
Browse files Browse the repository at this point in the history
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 443335c commit ee135b7
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/frontend/src/scripts/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import { RateLimiter } from '@/scripts/rate-limiter.js';
let ctx: AudioContext;
const cache = new Map<string, AudioBuffer>();

function isValidUrl(url: string): boolean {
try {
new URL(url);
return true;
} catch (_) {
return false;
}
}

export const soundsTypes = [
// 音声なし
null,
Expand Down Expand Up @@ -260,8 +269,12 @@ export function createSourceNode(buffer: AudioBuffer, opts: {
*/
export async function getSoundDuration(file: string): Promise<number> {
const audioEl = document.createElement('audio');
audioEl.src = file;
return new Promise((resolve) => {
audioEl.src = isValidUrl(file) ? file : '';
return new Promise((resolve, reject) => {
if (!audioEl.src) {
reject(new Error('Invalid URL'));
return;
}
const si = setInterval(() => {
if (audioEl.readyState > 0) {
resolve(audioEl.duration * 1000);
Expand Down

0 comments on commit ee135b7

Please sign in to comment.