Skip to content

Commit

Permalink
Merge pull request #21 from withinJoel/alert-autofix-2
Browse files Browse the repository at this point in the history
Fix code scanning alert no. 2: DOM text reinterpreted as HTML
  • Loading branch information
withinJoel authored Dec 23, 2024
2 parents f773a11 + fb14c99 commit abefb88
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions Modules/Detect File Type.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ function checkFileExistence(url, callback) {
img.src = url;
}

// Function to sanitize data by encoding special characters
function sanitizeData(data) {
return data.replace(/[&<>"'`=\/]/g, function (s) {
return "&#" + s.charCodeAt(0) + ";";
});
}

// Detect image type function
function detectImageType(data) {
const existingElement = document.querySelector('[data-role="dynamic-image"]');
if (existingElement) {
existingElement.remove();
}

const imageurl = imagedir + data;
const sanitizedData = sanitizeData(data);
const imageurl = imagedir + sanitizedData;

checkFileExistence(imageurl, function (exists) {
if (exists) {
Expand All @@ -31,15 +39,15 @@ function detectImageType(data) {
img.setAttribute('data-role', 'dynamic-image');
document.body.appendChild(img);

if (data.includes('.png')) {
if (sanitizedData.includes('.png')) {
echo("The file is in PNG format.");
} else if (data.includes('.jpg')) {
} else if (sanitizedData.includes('.jpg')) {
echo("The file is in JPG format.");
} else if (data.includes('.jpeg')) {
} else if (sanitizedData.includes('.jpeg')) {
echo("The file is in JPEG format.");
} else if (data.includes('.webp')) {
} else if (sanitizedData.includes('.webp')) {
echo("The file is in WEBP format.");
} else if (data.includes('.gif')) {
} else if (sanitizedData.includes('.gif')) {
echo("The file is in GIF format.");
} else {
echo("The file format is unknown.");
Expand Down

0 comments on commit abefb88

Please sign in to comment.