Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix code scanning alert no. 27: DOM text reinterpreted as HTML #801

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading