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

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
あわわわとーにゅ 2024-11-08 15:54:42 +09:00 committed by GitHub
parent 443335c662
commit ee135b7e3c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,6 +11,15 @@ import { RateLimiter } from '@/scripts/rate-limiter.js';
let ctx: AudioContext; let ctx: AudioContext;
const cache = new Map<string, AudioBuffer>(); const cache = new Map<string, AudioBuffer>();
function isValidUrl(url: string): boolean {
try {
new URL(url);
return true;
} catch (_) {
return false;
}
}
export const soundsTypes = [ export const soundsTypes = [
// 音声なし // 音声なし
null, null,
@ -260,8 +269,12 @@ export function createSourceNode(buffer: AudioBuffer, opts: {
*/ */
export async function getSoundDuration(file: string): Promise<number> { export async function getSoundDuration(file: string): Promise<number> {
const audioEl = document.createElement('audio'); const audioEl = document.createElement('audio');
audioEl.src = file; audioEl.src = isValidUrl(file) ? file : '';
return new Promise((resolve) => { return new Promise((resolve, reject) => {
if (!audioEl.src) {
reject(new Error('Invalid URL'));
return;
}
const si = setInterval(() => { const si = setInterval(() => {
if (audioEl.readyState > 0) { if (audioEl.readyState > 0) {
resolve(audioEl.duration * 1000); resolve(audioEl.duration * 1000);