Skip to content

Commit 39b5685

Browse files
committed
1.10.0
Expanded decode handler to support more encoding methods. See issue #100 for more information. Currently supported: hex, base64, url and url2. url2 is a special case used in issue #72
1 parent 1055bfc commit 39b5685

File tree

9 files changed

+106
-8
lines changed

9 files changed

+106
-8
lines changed

data/tidy.user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name Tidy URL
33
// @namespace https://ksir.pw
4-
// @version 1.9.0
4+
// @version 1.10.0
55
// @description Cleans/removes garbage or tracking parameters from URLs
66
// @author Kain (ksir.pw)
77
// @include *

lib/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export declare class TidyCleaner {
3535
hasParams(url: string): boolean;
3636
private isJSON;
3737
private getDiff;
38+
private decode;
3839
/**
3940
* Clean a URL
4041
* @param _url Any URL

lib/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from) {
66
};
77
Object.defineProperty(exports, "__esModule", { value: true });
88
exports.clean = exports.TidyURL = exports.TidyCleaner = void 0;
9+
var interface_1 = require("./interface");
910
var $github = 'https://github.com/DrKain/tidy-url';
1011
var TidyCleaner = /** @class */ (function () {
1112
function TidyCleaner() {
@@ -111,6 +112,33 @@ var TidyCleaner = /** @class */ (function () {
111112
reduction: +(100 - (data.url.length / url.length) * 100).toFixed(2)
112113
};
113114
};
115+
TidyCleaner.prototype.decode = function (str, encoding) {
116+
if (encoding === void 0) { encoding = interface_1.EEncoding.base64; }
117+
var decoded = str;
118+
// Simple base64 decoding
119+
if (encoding === interface_1.EEncoding.base64) {
120+
decoded = this.atob(str);
121+
}
122+
// Decode uri when used in URL parameters
123+
if (encoding === interface_1.EEncoding.url) {
124+
decoded = decodeURI(str);
125+
}
126+
// This is more of a special case but it may help other rules. See issue #72
127+
if (encoding === interface_1.EEncoding.url2) {
128+
decoded = decodeURIComponent(str.replace(/-/g, '%')).replace(/_/g, '/').replace(/%2F/g, '/');
129+
}
130+
// hex decode, not the best method but it works.
131+
// Open a PR if you want to improve it
132+
if (encoding === interface_1.EEncoding.hex) {
133+
var hex = str.toString();
134+
var out = '';
135+
for (var i = 0; i < hex.length; i += 2) {
136+
out += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
137+
}
138+
decoded = out;
139+
}
140+
return decoded;
141+
};
114142
/**
115143
* Clean a URL
116144
* @param _url Any URL
@@ -260,13 +288,15 @@ var TidyCleaner = /** @class */ (function () {
260288
try {
261289
if (!rule.decode)
262290
continue;
291+
// Make sure the target parameter exists
263292
if (!cleaner.has(rule.decode.param))
264293
continue;
265294
// These will always be clickjacking links, so use the allow_redirects rule
266295
if (!this.allow_redirects)
267296
continue;
268-
// Decode the base64 string
269-
var decoded = this.atob(cleaner.get(rule.decode.param));
297+
// Decode the string using selected encoding
298+
var encoding = rule.decode.encoding || 'base64';
299+
var decoded = this.decode(cleaner.get(rule.decode.param), encoding);
270300
var target = '';
271301
// If the response is JSON, decode and look for a key
272302
if (this.isJSON(decoded)) {

lib/interface.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ export interface IData {
6262
full_clean: boolean;
6363
};
6464
}
65+
export declare enum EEncoding {
66+
base64 = "b64",
67+
base32 = "b32",
68+
base45 = "b45",
69+
url = "url",
70+
url2 = "url2",
71+
binary = "binary",
72+
hex = "hex"
73+
}

lib/interface.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3+
exports.EEncoding = void 0;
4+
var EEncoding;
5+
(function (EEncoding) {
6+
EEncoding["base64"] = "b64";
7+
EEncoding["base32"] = "b32";
8+
EEncoding["base45"] = "b45";
9+
EEncoding["url"] = "url";
10+
EEncoding["url2"] = "url2";
11+
EEncoding["binary"] = "binary";
12+
EEncoding["hex"] = "hex";
13+
})(EEncoding = exports.EEncoding || (exports.EEncoding = {}));

lib/tidyurl.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tidy-url",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"description": "Cleans/removes tracking or garbage parameters from URLs",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/index.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IRule, IData } from './interface';
1+
import { IRule, IData, EEncoding } from './interface';
22

33
const $github = 'https://github.com/DrKain/tidy-url';
44

@@ -113,6 +113,40 @@ export class TidyCleaner {
113113
};
114114
}
115115

116+
private decode(str: string, encoding: EEncoding = EEncoding.base64): string {
117+
let decoded = str;
118+
119+
// Simple base64 decoding
120+
if (encoding === EEncoding.base64) {
121+
decoded = this.atob(str);
122+
}
123+
124+
// Decode uri when used in URL parameters
125+
if (encoding === EEncoding.url) {
126+
decoded = decodeURI(str);
127+
}
128+
129+
// This is more of a special case but it may help other rules. See issue #72
130+
if (encoding === EEncoding.url2) {
131+
decoded = decodeURIComponent(str.replace(/-/g, '%')).replace(/_/g, '/').replace(/%2F/g, '/');
132+
}
133+
134+
// hex decode, not the best method but it works.
135+
// Open a PR if you want to improve it
136+
if (encoding === EEncoding.hex) {
137+
let hex = str.toString();
138+
let out = '';
139+
140+
for (var i = 0; i < hex.length; i += 2) {
141+
out += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
142+
}
143+
144+
decoded = out;
145+
}
146+
147+
return decoded;
148+
}
149+
116150
/**
117151
* Clean a URL
118152
* @param _url Any URL
@@ -261,11 +295,14 @@ export class TidyCleaner {
261295
for (const rule of data.info.match) {
262296
try {
263297
if (!rule.decode) continue;
298+
// Make sure the target parameter exists
264299
if (!cleaner.has(rule.decode.param)) continue;
265300
// These will always be clickjacking links, so use the allow_redirects rule
266301
if (!this.allow_redirects) continue;
267-
// Decode the base64 string
268-
const decoded = this.atob(cleaner.get(rule.decode.param) as string);
302+
// Decode the string using selected encoding
303+
304+
const encoding = rule.decode.encoding || 'base64';
305+
const decoded = this.decode(cleaner.get(rule.decode.param) as string, encoding);
269306
let target = '';
270307

271308
// If the response is JSON, decode and look for a key

src/interface.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,13 @@ export interface IData {
5858
full_clean: boolean;
5959
};
6060
}
61+
62+
export enum EEncoding {
63+
base64 = 'b64',
64+
base32 = 'b32',
65+
base45 = 'b45',
66+
url = 'url',
67+
url2 = 'url2',
68+
binary = 'binary',
69+
hex = 'hex'
70+
}

0 commit comments

Comments
 (0)