Skip to content

Commit

Permalink
URL aggressive encoding fix
Browse files Browse the repository at this point in the history
Basically I didn't account for UTF-8 characters, I did Windows-1252 instead which isn't what browsers use.
  • Loading branch information
ghluka committed Apr 27, 2024
1 parent 2769c42 commit b9b72a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
24 changes: 19 additions & 5 deletions techdemos/encode/js/encoders/url.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
const encodeURIAggressive = text => "%" + text.split("").map(c => c.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase();
const decodeURIAggressive = text => text.split("%").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 16))).join("");
function encodeURIAggressive(text) {
var encoded_text = "";

const characters = encodeURI(text).split(/(%\w\w)/g).filter(t => t !== "");
for (const i in characters) {
let c = characters[i]
if (c.startsWith("%")) {
encoded_text += c;
}
else {
encoded_text += "%" + c.split("").map(t => t.charCodeAt(0).toString(16).padStart(2, "0")).join("%").toUpperCase();
}
}

return encoded_text;
}

function encodeURINonDestructive(text) {
var encoded_text = encodeURIAggressive(text);

if (encoded_text.includes("%3A%2F%2F")) {
encoded_text = encoded_text.replace("%3A%2F%2F", "://");
const protocol = decodeURIAggressive(encoded_text.split("://")[0])
const protocol = decodeURI(encoded_text.split("://")[0])
encoded_text = protocol + "://" + encoded_text.split("://")[1];
}

Expand All @@ -23,7 +37,7 @@ function decodeURINonDestructive(text) {

text = text.replaceAll("/", "%2F").replaceAll("?", "%3F").replaceAll("&", "%26").replaceAll("+", "%2B").replaceAll("=", "%3D");

return decodeURIAggressive(text);
return decodeURIComponent(decodeURI(text));
}

export {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive}
export {encodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive}
12 changes: 6 additions & 6 deletions techdemos/encode/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {encodeURIAggressive, decodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} from "./encoders/url.js";
import {encodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} from "./encoders/url.js";

const mode = document.getElementById("mode");
const encode = document.getElementById("encode");
Expand All @@ -25,16 +25,16 @@ const codes = {
decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 2))).join("")
},
"URL": {
encode: text => encodeURI(text),
decode: text => decodeURI(text)
encode: encodeURI,
decode: decodeURI
},
"URL (component)": {
encode: text => encodeURIComponent(text),
decode: text => decodeURIComponent(text)
encode: encodeURIComponent,
decode: decodeURIComponent
},
"URL (aggressive)": {
encode: encodeURIAggressive,
decode: decodeURIAggressive
decode: text => decodeURIComponent(decodeURI(text))
},
"URL (aggressive, non destructive)": {
encode: encodeURINonDestructive,
Expand Down

0 comments on commit b9b72a6

Please sign in to comment.