Last active
June 6, 2017 07:38
-
-
Save whizzter/fe37aa03bb2e9579e304c16cc8e12db2 to your computer and use it in GitHub Desktop.
automatically exporting a module with koa (possibly insecure)
This file contains hidden or 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
exports.bah=function(count,name){ | |
let out=""; | |
while(count--) | |
out+=name+" "; | |
return out; | |
}; |
This file contains hidden or 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
// Install requirements with: npm install --save co co-body koa-better-router koa@2 | |
const app=new (require("koa"))(); | |
const router=require("koa-better-router")().loadMethods(); | |
const co_body=require("co-body"); | |
const mymod=require("./mymod"); | |
// Test the below with: curl -d "[3,\"Hello\"]" http://localhost:8080/mymod/bah | |
router.post("/mymod/:myfun",function*() { | |
const fun=this.route.params.myfun; | |
const args=yield co_body.json(this,{limit:"10kb"}); | |
const mod=mymod; | |
// Only allow invoking direct properties on the object (ie restrict the prototype chain methods from being invoked) | |
if (Object.prototype.hasOwnProperty.call(mod,fun)) { | |
this.body=mod[fun].apply(null,args); | |
} else { | |
this.body="No such method "+fun; | |
} | |
}); | |
app.use(router.middleware()); | |
app.listen(8080); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment