Skip to content

Commit

Permalink
Handle relative urls in image upload sanatize
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Best-Codes committed Dec 2, 2024
1 parent d5d7207 commit edecf8e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions public/pages/index/modules/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,16 @@ export class UploadManager {

sanitizeUrl(url) {
try {
// Create a URL object to validate the URL
// Handle relative URLs
if (url.startsWith('/')) {
return encodeURI(url);
}

// For absolute URLs, validate the protocol
const parsedUrl = new URL(url);
// Only allow specific protocols
if (!['http:', 'https:'].includes(parsedUrl.protocol)) {
throw new Error('Invalid URL protocol');
}
// Encode the URL components to prevent XSS
return encodeURI(url);
} catch (e) {
console.error('Invalid URL:', e);
Expand Down

0 comments on commit edecf8e

Please sign in to comment.