Skip to content
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
22 changes: 20 additions & 2 deletions pages/api/music/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,36 @@
if (!req.session.userid) return res.status(401).end();

const url = req.query.url as string;
if (!url || !url.startsWith('https://audio-ssl.itunes.apple.com/')) {
if (!url) {
return res.status(400).end('Invalid URL');
}

const upstream = await axios.get(url, {
let parsedUrl: URL;
try {
parsedUrl = new URL(url);
} catch {
return res.status(400).end('Invalid URL');
}

if (
parsedUrl.protocol !== 'https:' ||
parsedUrl.hostname !== 'audio-ssl.itunes.apple.com' ||
parsedUrl.username ||
parsedUrl.password ||
(parsedUrl.port !== '' && parsedUrl.port !== '443')
) {
return res.status(400).end('Invalid URL');
}

const upstream = await axios.get(parsedUrl.toString(), {
responseType: 'stream',
headers: {
'User-Agent': 'Mozilla/5.0',
'Accept': 'audio/*,*/*',
},
timeout: 15000,
maxRedirects: 0,
});

Check failure

Code scanning / CodeQL

Server-side request forgery Critical

The
URL
of this request depends on a
user-provided value
.

res.setHeader('Content-Type', upstream.headers['content-type'] || 'audio/mp4');
res.setHeader('Cache-Control', 'public, max-age=3600');
Expand Down
Loading