Skip to content

Instantly share code, notes, and snippets.

@ermouth
Created December 2, 2015 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ermouth/953691b6b62c9cab4ff6 to your computer and use it in GitHub Desktop.
Save ermouth/953691b6b62c9cab4ff6 to your computer and use it in GitHub Desktop.
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;
});
}
}
}
}
@ermouth
Copy link
Author

ermouth commented Dec 2, 2015

UglifyJS 2.4.15 can be taken from http://cdn.cloudwall.me/1.1/uglify.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment