Skip to content

Commit ab67a0a

Browse files
committed
Pass API execution context to sandbox
Closes: #114 PR-URL: #112
1 parent 80baa5f commit ab67a0a

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

lib/application.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Application extends events.EventEmitter {
3434

3535
createSandbox() {
3636
const introspection = async () => [...this.api.keys()];
37-
const application = { security, api: { introspection } };
37+
const context = Object.freeze({});
38+
const application = { security, api: { introspection }, context };
3839
for (const name of this.namespaces) application[name] = this[name];
3940
const sandbox = { console: this.logger, application, Buffer, api };
4041
sandbox.global = sandbox;
@@ -51,7 +52,7 @@ class Application extends events.EventEmitter {
5152
const data = await fsp.readFile(fileName, 'utf8');
5253
const code = data.startsWith('({') ? data :
5354
`({ access: 'logged', method: ${data.trim().slice(0, -1)} });`;
54-
const src = `'use strict';\ncontext => ${code}`;
55+
const src = `'use strict';\n${code}`;
5556
const options = { filename: fileName, lineOffset: -1 };
5657
try {
5758
return new vm.Script(src, options);
@@ -61,7 +62,8 @@ class Application extends events.EventEmitter {
6162
}
6263
}
6364

64-
runScript(methodName, sandbox = this.sandbox) {
65+
runScript(methodName, session) {
66+
const sandbox = session ? session.sandbox : this.sandbox;
6567
const script = this.api.get(methodName);
6668
if (!script) return null;
6769
return script.runInContext(sandbox, SCRIPT_OPTIONS);

lib/auth.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ module.exports = application => {
9090
this.token = token;
9191
this.sandbox = sandbox;
9292
this.data = contextData;
93-
this.context = new Proxy(contextData, contextHandler);
93+
sandbox.context = new Proxy(contextData, contextHandler);
9494
}
9595
}
9696

lib/server.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,11 @@ class Client {
9292
}
9393
try {
9494
const session = await application.auth.restore(this);
95-
const sandbox = session ? session.sandbox : undefined;
96-
const context = session ? session.context : {};
97-
const exp = application.runScript(method, sandbox);
98-
if (!exp) {
95+
const proc = application.runScript(method, session);
96+
if (!proc) {
9997
this.error(404);
10098
return;
10199
}
102-
const proc = exp(context);
103100
if (!session && proc.access !== 'public') {
104101
this.error(403, new Error(`Forbidden: /api/${method}`));
105102
return;

0 commit comments

Comments
 (0)