Skip to content

Commit 4679f8f

Browse files
committed
✨ Add internal option for Cloud.define
1 parent 2dcf720 commit 4679f8f

6 files changed

Lines changed: 37 additions & 0 deletions

File tree

API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ AV.Cloud.define(name: string, options: object, func: function)
5353
`options` 的属性包括:
5454

5555
* `fetchUser: boolean`:是否自动抓取客户端的用户信息,默认为 `true`,若设置为 `false` 则 `request` 上将不会有 user 属性。
56+
* `internal: boolean`:只允许在云引擎内(使用 `AV.Cloud.run` 且未开启 `remote` 选项)或 masterKey 调用(使用 `AV.Cloud.run` 时传入 `useMasterKey`),不允许客户端直接调用,默认 `false`。
5657

5758
`Request` 上的属性包括:
5859

leanengine.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export namespace Insight {
5959
export namespace Cloud {
6060
interface DefineOptions {
6161
fetchUser?: boolean
62+
internal?: boolean
6263
}
6364

6465
interface RunOptions {

lib/cloud.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ Cloud.define = function(name, options, func) {
2121
func.fetchUser = false;
2222
}
2323

24+
if (options && options.internal) {
25+
func.internal = true;
26+
}
27+
2428
if (Cloud.functions[name]) {
2529
throw new Error(`LeanEngine: ${name} already defined`);
2630
} else {

lib/leanengine.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ function callCloudFunction(req, funcName, options) {
155155
throw new Cloud.Error(`No such cloud function '${funcName}'`, {status: 404, printToLog: true, printFullStack: false});
156156
}
157157

158+
if (cloudFunction.internal) {
159+
checkInternal(req);
160+
}
161+
158162
if (_.contains(_.values(utils.realtimeHookMapping), funcName)) {
159163
checkHookKey(req);
160164
}
@@ -342,6 +346,14 @@ function checkHookKey(req) {
342346
}
343347
}
344348

349+
function checkInternal(req) {
350+
if (req.headers['x-lc-hook-key'] !== AV.hookKey && !req.AV.authMasterKey) {
351+
throw new Cloud.Error(`Internal cloud function, request from ${utils.getRemoteAddress(req)}`, {
352+
status: 401, code: 401, printToLog: true, printFullStack: false
353+
});
354+
}
355+
}
356+
345357
function decodeUser(requestUser) {
346358
if (requestUser) {
347359
const user = new AV.User();

test/authorization-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,19 @@ describe('authorization', function() {
118118
.expect({code: 401, error: 'Unauthorized.'}, done);
119119
});
120120

121+
it('internal function', done => {
122+
request(app)
123+
.post('/1/functions/internalFunction')
124+
.set('X-LC-Id', appId)
125+
.set('X-LC-Key', appKey)
126+
.expect(401, done);
127+
})
128+
129+
it('internal function, master key', done => {
130+
request(app)
131+
.post('/1/functions/internalFunction')
132+
.set('X-LC-Id', appId)
133+
.set('X-LC-Key', `${masterKey},master`)
134+
.expect(200, done);
135+
})
121136
});

test/fixtures/functions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ AV.Cloud.define('dontFetchUser', {fetchUser: false}, function(req, res) {
134134
res.success();
135135
});
136136

137+
AV.Cloud.define('internalFunction', {internal: true}, () => {
138+
139+
});
140+
137141
AV.Cloud.define('testRun', function(request, response) {
138142
if (request.params.shouldRemote && process.env.NODE_ENV != 'production') {
139143
return response.error('Should be run on remote');

0 commit comments

Comments
 (0)