Created
December 2, 2015 18:44
-
-
Save ermouth/953691b6b62c9cab4ff6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(doc){ | |
// Validates ddoc map functions to prevent them | |
// from calling send(), getRow() and so on. | |
// Ddoc with this validate_doc_update must have | |
// property .Uglify with UglifyJS lib (tested for 2.4.15). | |
require("Uglify"); | |
var mapInvalid = "start send getRow eval".split(" "), | |
v, invalid, | |
u = UglifyJS, | |
globals = {}, | |
errors = []; | |
if (/^_design\//.test(doc._id)) { | |
v = doc.views; | |
if (typeof v != "object") return; | |
Object.keys(v).forEach(function(idx){ | |
_parse (v[idx].map, idx+"/map"); | |
}); | |
mapInvalid.forEach(function(i){ | |
if (globals[i]) errors.push("Ddoc has view/map that uses "+i+"()"); | |
}); | |
if (errors.length) throw ({ forbidden: errors.join(" \n") }); | |
} | |
//- - - - - - - - - - - | |
function _parse(fn, name){ | |
if (!fn) return; | |
var ast, gl; | |
try{ | |
ast = u.parse("("+fn+")"); | |
} catch (e) { | |
errors.push (name+": "+e.message+" @"+e.line+":"+e.col) | |
} | |
if (ast) { | |
ast.figure_out_scope(); | |
if (typeof(gl = ast.globals) == "object" && gl._size) { | |
Object.keys(gl._values) | |
.forEach(function(i){ | |
globals[gl._values[i].name] = true; | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UglifyJS 2.4.15 can be taken from http://cdn.cloudwall.me/1.1/uglify.js