Skip to content

Add request ip to request object #4265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions spec/CloudCode.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
it('should have request headers', (done) => {
Parse.Cloud.beforeSave('MyObject', (req, res) => {
Expand All @@ -1250,6 +1261,17 @@ describe('beforeSave hooks', () => {
const myObject = new MyObject();
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', () => {
Expand All @@ -1263,6 +1285,17 @@ describe('afterSave hooks', () => {
myObject.save()
.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', () => {
Expand All @@ -1278,6 +1311,19 @@ describe('beforeDelete hooks', () => {
.then(myObj => myObj.destroy())
.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', () => {
Expand All @@ -1292,6 +1338,18 @@ describe('afterDelete hooks', () => {
.then(myObj => myObj.destroy())
.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', () => {
Expand Down Expand Up @@ -1448,6 +1506,26 @@ describe('beforeFind hooks', () => {
})
.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', () => {
Expand Down Expand Up @@ -1667,6 +1745,27 @@ describe('afterFind hooks', () => {
.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', () => {
expect(() => {
Parse.Cloud.beforeSave('_Session', () => {});
Expand Down
6 changes: 4 additions & 2 deletions src/Routers/FunctionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export class FunctionsRouter extends PromiseRouter {
const request = {
params: params,
log: req.config.loggerController,
headers: req.headers,
headers: req.config.headers,
ip: req.config.ip,
jobName
};
const status = {
Expand Down Expand Up @@ -111,7 +112,8 @@ export class FunctionsRouter extends PromiseRouter {
user: req.auth && req.auth.user,
installationId: req.info.installationId,
log: req.config.loggerController,
headers: req.headers,
headers: req.config.headers,
ip: req.config.ip,
functionName
};

Expand Down
5 changes: 4 additions & 1 deletion src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,15 @@ export function handleParseHeaders(req, res, next) {
req.body = new Buffer(base64, 'base64');
}

const clientIp = getClientIp(req);

info.app = AppCache.get(info.appId);
req.config = new Config(info.appId, mount);
req.config.headers = req.headers || {};
req.config.ip = clientIp;
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);
}

Expand Down
2 changes: 2 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export function getRequestObject(triggerType, auth, parseObject, originalParseOb
master: false,
log: config.loggerController,
headers: config.headers,
ip: config.ip,
};

if (originalParseObject) {
Expand Down Expand Up @@ -176,6 +177,7 @@ export function getRequestQueryObject(triggerType, auth, query, count, config, i
log: config.loggerController,
isGet,
headers: config.headers,
ip: config.ip,
};

if (!auth) {
Expand Down