Skip to content

Commit

Permalink
Fix null check in paraphrase wrappers (#126)
Browse files Browse the repository at this point in the history
* Fix wrappers null check

* Add integration test

* Pin version

* Add check for prop existence

* Add unit test
  • Loading branch information
styfle authored Jun 16, 2020
1 parent 6a06f2d commit 6d3fc90
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"mysql": "^2.17.1",
"npm": "^6.13.4",
"oracledb": "^4.2.0",
"paraphrase": "1.8.0",
"passport": "^0.4.0",
"passport-google-oauth": "^2.0.0",
"passport-trakt": "^1.0.4",
Expand Down
7 changes: 4 additions & 3 deletions src/utils/wrappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const { walk } = require('estree-walker');
function handleWrappers (ast) {
// UglifyJS will convert function wrappers into !function(){}
let wrapper;

if (ast.body.length === 1 &&
ast.body[0].type === 'ExpressionStatement' &&
ast.body[0].expression.type === 'UnaryExpression' &&
Expand Down Expand Up @@ -319,12 +318,14 @@ function handleWrappers (ast) {
wrapper.callee.body.body[1].type === 'FunctionDeclaration' &&
wrapper.callee.body.body[1].params.length === 1 &&
wrapper.callee.body.body[1].body.body.length >= 3 && (
wrapper.arguments[0] &&
wrapper.arguments[0].type === 'ArrayExpression' &&
wrapper.arguments[0].elements.length > 0 &&
wrapper.arguments[0].elements.every(el => el.type === 'FunctionExpression') ||
wrapper.arguments[0].elements.every(el => el && el.type === 'FunctionExpression') ||
wrapper.arguments[0].type === 'ObjectExpression' &&
wrapper.arguments[0].properties &&
wrapper.arguments[0].properties.length > 0 &&
wrapper.arguments[0].properties.every(prop => prop.key.type === 'Literal' && prop.value.type === 'FunctionExpression')
wrapper.arguments[0].properties.every(prop => prop && prop.key && prop.key.type === 'Literal' && prop.value && prop.value.type === 'FunctionExpression')
)) {
const externalMap = new Map();
let modules;
Expand Down
1 change: 1 addition & 0 deletions test/integration/paraphrase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const paraphrase = require('paraphrase/percent')
1 change: 1 addition & 0 deletions test/unit/webpack-wrapper-null/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./mangled')
117 changes: 117 additions & 0 deletions test/unit/webpack-wrapper-null/mangled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
module.exports = function(t) {
var e = {};

function r(n) {
if (e[n]) return e[n].exports;
var o = e[n] = {
i: n,
l: !1,
exports: {}
};
return t[n].call(o.exports, o, o.exports, r), o.l = !0, o.exports
}
return r.m = t, r.c = e, r.d = function(t, e, n) {
r.o(t, e) || Object.defineProperty(t, e, {
enumerable: !0,
get: n
})
}, r.r = function(t) {
"undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(t, Symbol.toStringTag, {
value: "Module"
}), Object.defineProperty(t, "__esModule", {
value: !0
})
}, r.t = function(t, e) {
if (1 & e && (t = r(t)), 8 & e) return t;
if (4 & e && "object" == typeof t && t && t.__esModule) return t;
var n = Object.create(null);
if (r.r(n), Object.defineProperty(n, "default", {
enumerable: !0,
value: t
}), 2 & e && "string" != typeof t)
for (var o in t) r.d(n, o, function(e) {
return t[e]
}.bind(null, o));
return n
}, r.n = function(t) {
var e = t && t.__esModule ? function() {
return t.
default
} : function() {
return t
};
return r.d(e, "a", e), e
}, r.o = function(t, e) {
return Object.prototype.hasOwnProperty.call(t, e)
}, r.p = "", r(r.s = 5)
}([
function(t, e, r) {
function n(t) {
return (n = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
return typeof t
} : function(t) {
return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
})(t)
}
var o = r(1),
u = Object.seal(["string", "number"]);
t.exports = function() {
for (var t = arguments.length, e = new Array(t), r = 0; r < t; r++) e[r] = arguments[r];
var i = {
recursive: !0,
resolve: !0,
clean: !1
};

function f() {
var t = arguments.length > 0 && void 0 !== arguments[0] ? arguments[0] : "",
r = arguments.length > 1 ? arguments[1] : void 0;
if ("string" != typeof t) throw new TypeError("paraphrase expects first argument to be a string, got a ".concat(n(t), " (").concat(t, ")"));
if (!r) return t;
for (var c = arguments.length, l = new Array(c > 2 ? c - 2 : 0), y = 2; y < c; y++) l[y - 2] = arguments[y];

function a(t, e) {
var c = i.resolve ? o(r, e.trim()) : r[e.trim()];
return u.includes(n(c)) ? c : i.clean ? "" : t
}
u.includes(n(r)) && (r = [r].concat(l));
var p = e.reduce((function(t, e) {
return t.replace(e, a)
}), t);
return i.recursive && t !== p ? f.apply(void 0, [p, r].concat(l)) : p
}
return e.length && c(e[e.length - 1]) && Object.assign(i, e.pop()), f.patterns = e, f
};
var c = function(t) {
return "[object Object]" === "".concat(t)
}
},
function(t, e, r) {
"use strict";

function n(t) {
return (n = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
return typeof t
} : function(t) {
return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t
})(t)
}

function o(t) {
return (o = "function" == typeof Symbol && "symbol" === n(Symbol.iterator) ? function(t) {
return n(t)
} : function(t) {
return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : n(t)
})(t)
}
t.exports = function(t) {
var e = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : "";
return e.split(".").reduce((function(t, e) {
return "object" === o(t) ? t[e] : t
}), t)
}
}, , , ,
function(t, e, r) {
t.exports = r(0)(/%{([^{}]*)}/gm)
}
]);
4 changes: 4 additions & 0 deletions test/unit/webpack-wrapper-null/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
"test/unit/webpack-wrapper-null/input.js",
"test/unit/webpack-wrapper-null/mangled.js"
]
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9461,6 +9461,11 @@ [email protected]:
query-string "^5.0.1"
sort-keys "^2.0.0"

notate@^1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/notate/-/notate-1.1.2.tgz#29447d80c3789a4c9f196bdc64b4068618dde9f6"
integrity sha512-87c1SbFP7TvHk1gdc1ZRTk+0wr+1VbKC6Nm3cW7xqHjEyU0eY27fOHjJnWmxFkgSkQF/kaLj9PfvtUh13GjHMQ==

npm-audit-report@^1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/npm-audit-report/-/npm-audit-report-1.3.2.tgz#303bc78cd9e4c226415076a4f7e528c89fc77018"
Expand Down Expand Up @@ -10151,6 +10156,13 @@ param-case@^1.1.0:
dependencies:
sentence-case "^1.1.2"

[email protected]:
version "1.8.0"
resolved "https://registry.yarnpkg.com/paraphrase/-/paraphrase-1.8.0.tgz#7ba038d176f4585674162a4bd825932b8b6ac22c"
integrity sha512-447jeY7a82JcPtJht+rEEnlSeKFmaZezMpkeQTtWB88n8PtJew/HIGHhmvjxi2QfSiRECeIJ6XkZ9U8w0neCZg==
dependencies:
notate "^1.1.2"

parents@^1.0.0, parents@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/parents/-/parents-1.0.1.tgz#fedd4d2bf193a77745fe71e371d73c3307d9c751"
Expand Down

0 comments on commit 6d3fc90

Please sign in to comment.