Skip to content

Commit 6bb181d

Browse files
simplesmilerGuillaume Chau
authored and
Guillaume Chau
committed
Use prototype toString to prevent bad serialization (#569)
* Edge case of incorrectly serialized function Should fix #568 * Function.toString hijacking example * Use prototype toString
1 parent d5b8a75 commit 6bb181d

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

shells/dev/target/NativeTypes.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@
3333
import { mapState, mapGetters, mapMutations } from 'vuex'
3434
import CompDef from './Other.vue'
3535
36+
function setToString (func, string) {
37+
return Object.defineProperty(func, 'toString', {
38+
configurable: true,
39+
enumerable: false,
40+
value: () => string,
41+
writable: true
42+
})
43+
}
44+
45+
const aWeirdFunction = setToString(function weird (a, b, c) {}, 'foo')
46+
3647
export default {
3748
components: {
3849
TestComponent: {
@@ -53,6 +64,7 @@ export default {
5364
hello: function foo (a, b, c) {},
5465
hey: function empty () {},
5566
anon: function (foo, bar) {},
67+
aWeirdFunction,
5668
arrow: (a, b) => {},
5769
def: CompDef,
5870
def2: {

src/util.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ function replacer (key) {
117117
return encodeCache.cache(val, () => getCustomSetDetails(val))
118118
} else if (val instanceof RegExp) {
119119
// special handling of native type
120-
return `[native RegExp ${val.toString()}]`
120+
return `[native RegExp ${RegExp.prototype.toString.call(val)}]`
121121
} else if (val instanceof Date) {
122-
return `[native Date ${val.toString()}]`
122+
return `[native Date ${Date.prototype.toString.call(val)}]`
123123
} else if (val.state && val._vm) {
124124
return encodeCache.cache(val, () => getCustomStoreDetails(val))
125125
} else if (val.constructor && val.constructor.name === 'VueRouter') {
@@ -230,7 +230,9 @@ export function getCustomComponentDefinitionDetails (def) {
230230
}
231231

232232
export function getCustomFunctionDetails (func) {
233-
const args = func.toString().match(/\(.*\)/)[0]
233+
const string = Function.prototype.toString.call(func) || ''
234+
const matches = string.match(/\(.*\)/)
235+
const args = matches ? matches[0] : '(?)'
234236
return {
235237
_custom: {
236238
type: 'function',

0 commit comments

Comments
 (0)