Add request ip to request object (#4265)

* add the client ip to the request config object

* add the config ip to the trigger request object

* add the config ip to the functions request object

* add tests

* remove log

* remove log
This commit is contained in:
Gustav Ahlberg
2017-10-18 14:13:09 +02:00
committed by Florent Vilmart
parent 9376b4d04a
commit 70ad9e9ffd
4 changed files with 109 additions and 3 deletions

View File

@@ -1239,6 +1239,17 @@ describe('Cloud Code', () => {
}); });
}); });
describe('cloud functions', () => {
it('Should have request ip', (done) => {
Parse.Cloud.define('myFunction', (req, res) => {
expect(req.ip).toBeDefined();
res.success("success");
});
Parse.Cloud.run('myFunction', {}).then(() => done());
});
});
describe('beforeSave hooks', () => { describe('beforeSave hooks', () => {
it('should have request headers', (done) => { it('should have request headers', (done) => {
Parse.Cloud.beforeSave('MyObject', (req, res) => { Parse.Cloud.beforeSave('MyObject', (req, res) => {
@@ -1250,6 +1261,17 @@ describe('beforeSave hooks', () => {
const myObject = new MyObject(); const myObject = new MyObject();
myObject.save().then(() => done()); myObject.save().then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.beforeSave('MyObject', (req, res) => {
expect(req.ip).toBeDefined();
res.success();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save().then(() => done());
});
}); });
describe('afterSave hooks', () => { describe('afterSave hooks', () => {
@@ -1263,6 +1285,17 @@ describe('afterSave hooks', () => {
myObject.save() myObject.save()
.then(() => done()); .then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.afterSave('MyObject', (req, res) => {
expect(req.ip).toBeDefined();
res.success();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save().then(() => done());
});
}); });
describe('beforeDelete hooks', () => { describe('beforeDelete hooks', () => {
@@ -1278,6 +1311,19 @@ describe('beforeDelete hooks', () => {
.then(myObj => myObj.destroy()) .then(myObj => myObj.destroy())
.then(() => done()); .then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.beforeDelete('MyObject', (req, res) => {
expect(req.ip).toBeDefined();
res.success();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save()
.then(myObj => myObj.destroy())
.then(() => done());
});
}); });
describe('afterDelete hooks', () => { describe('afterDelete hooks', () => {
@@ -1292,6 +1338,18 @@ describe('afterDelete hooks', () => {
.then(myObj => myObj.destroy()) .then(myObj => myObj.destroy())
.then(() => done()); .then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.afterDelete('MyObject', (req) => {
expect(req.ip).toBeDefined();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save()
.then(myObj => myObj.destroy())
.then(() => done());
});
}); });
describe('beforeFind hooks', () => { describe('beforeFind hooks', () => {
@@ -1448,6 +1506,26 @@ describe('beforeFind hooks', () => {
}) })
.then(() => done()); .then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.beforeFind('MyObject', (req) => {
expect(req.ip).toBeDefined();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save()
.then((myObj) => {
const query = new Parse.Query('MyObject');
query.equalTo('objectId', myObj.id);
return Promise.all([
query.get(myObj.id),
query.first(),
query.find(),
]);
})
.then(() => done());
});
}); });
describe('afterFind hooks', () => { describe('afterFind hooks', () => {
@@ -1667,6 +1745,27 @@ describe('afterFind hooks', () => {
.then(() => done()); .then(() => done());
}); });
it('should have request ip', (done) => {
Parse.Cloud.afterFind('MyObject', (req, res) => {
expect(req.ip).toBeDefined();
res.success();
});
const MyObject = Parse.Object.extend('MyObject');
const myObject = new MyObject();
myObject.save()
.then((myObj) => {
const query = new Parse.Query('MyObject');
query.equalTo('objectId', myObj.id);
return Promise.all([
query.get(myObj.id),
query.first(),
query.find(),
]);
})
.then(() => done());
});
it('should validate triggers correctly', () => { it('should validate triggers correctly', () => {
expect(() => { expect(() => {
Parse.Cloud.beforeSave('_Session', () => {}); Parse.Cloud.beforeSave('_Session', () => {});

View File

@@ -54,7 +54,8 @@ export class FunctionsRouter extends PromiseRouter {
const request = { const request = {
params: params, params: params,
log: req.config.loggerController, log: req.config.loggerController,
headers: req.headers, headers: req.config.headers,
ip: req.config.ip,
jobName jobName
}; };
const status = { const status = {
@@ -111,7 +112,8 @@ export class FunctionsRouter extends PromiseRouter {
user: req.auth && req.auth.user, user: req.auth && req.auth.user,
installationId: req.info.installationId, installationId: req.info.installationId,
log: req.config.loggerController, log: req.config.loggerController,
headers: req.headers, headers: req.config.headers,
ip: req.config.ip,
functionName functionName
}; };

View File

@@ -106,12 +106,15 @@ export function handleParseHeaders(req, res, next) {
req.body = new Buffer(base64, 'base64'); req.body = new Buffer(base64, 'base64');
} }
const clientIp = getClientIp(req);
info.app = AppCache.get(info.appId); info.app = AppCache.get(info.appId);
req.config = new Config(info.appId, mount); req.config = new Config(info.appId, mount);
req.config.headers = req.headers || {}; req.config.headers = req.headers || {};
req.config.ip = clientIp;
req.info = info; req.info = info;
if (info.masterKey && req.config.masterKeyIps && req.config.masterKeyIps.length !== 0 && req.config.masterKeyIps.indexOf(getClientIp(req)) === -1) { if (info.masterKey && req.config.masterKeyIps && req.config.masterKeyIps.length !== 0 && req.config.masterKeyIps.indexOf(clientIp) === -1) {
return invalidRequest(req, res); return invalidRequest(req, res);
} }

View File

@@ -144,6 +144,7 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
master: false, master: false,
log: config.loggerController, log: config.loggerController,
headers: config.headers, headers: config.headers,
ip: config.ip,
}; };
if (originalParseObject) { if (originalParseObject) {
@@ -176,6 +177,7 @@ export function getRequestQueryObject(triggerType, auth, query, count, config, i
log: config.loggerController, log: config.loggerController,
isGet, isGet,
headers: config.headers, headers: config.headers,
ip: config.ip,
}; };
if (!auth) { if (!auth) {