Skip to content

Commit

Permalink
URL Validation (Protection against inserting an invalid URL)
Browse files Browse the repository at this point in the history
  • Loading branch information
CubesterYT committed Jan 17, 2025
1 parent 4f86f92 commit 7effd94
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion extensions/CubesterYT/Webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
setupStorage();
Scratch.vm.runtime.on("PROJECT_LOADED", setupStorage);

function isURLValid(string) {
try {
const url = new URL(string);
return url.protocol === "http:" || url.protocol === "https:";
} catch (error) {
return false;
}
}

function toForm(data) {
const parts = [];
const process = (name, value) => {
Expand Down Expand Up @@ -147,7 +156,20 @@
);
}
} while (Object.keys(webhooks).includes(name));
let URL = prompt(Scratch.translate("Enter Webhook URL:"));
let URL;
do {
URL = prompt(Scratch.translate("Enter Webhook URL:"));
if (URL === null) {
return;
}
if (!isURLValid(URL)) {
alert(
Scratch.translate(
"Invalid URL! Please make sure you provide a valid URL."
)
);
}
} while (!isURLValid(URL));
webhooks[name] = { URL, DATA: {}, TYPE: "application/json" };
hideFromPalette = false;
Scratch.vm.extensionManager.refreshBlocks();
Expand Down

0 comments on commit 7effd94

Please sign in to comment.