Skip to content

Commit e22b73d

Browse files
authored
feat: Write log entry when request with master key is rejected as outside of masterKeyIps (#8350)
1 parent fded5be commit e22b73d

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

spec/Middlewares.spec.js

+16
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ describe('middlewares', () => {
135135
});
136136
});
137137

138+
it('should not succeed and log if the ip does not belong to masterKeyIps list', async () => {
139+
const logger = require('../lib/logger').logger;
140+
spyOn(logger, 'error').and.callFake(() => {});
141+
AppCache.put(fakeReq.body._ApplicationId, {
142+
masterKey: 'masterKey',
143+
masterKeyIps: ['10.0.0.1'],
144+
});
145+
fakeReq.ip = '127.0.0.1';
146+
fakeReq.headers['x-parse-master-key'] = 'masterKey';
147+
await new Promise(resolve => middlewares.handleParseHeaders(fakeReq, fakeRes, resolve));
148+
expect(fakeReq.auth.isMaster).toBe(false);
149+
expect(logger.error).toHaveBeenCalledWith(
150+
`Request using master key rejected as the request IP address '127.0.0.1' is not set in Parse Server option 'masterKeyIps'.`
151+
);
152+
});
153+
138154
it('should not succeed if the ip does not belong to masterKeyIps list', async () => {
139155
AppCache.put(fakeReq.body._ApplicationId, {
140156
masterKey: 'masterKey',

src/middlewares.js

+4
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ export function handleParseHeaders(req, res, next) {
167167

168168
let isMaster = info.masterKey === req.config.masterKey;
169169
if (isMaster && !ipRangeCheck(clientIp, req.config.masterKeyIps || [])) {
170+
const log = req.config?.loggerController || defaultLogger;
171+
log.error(
172+
`Request using master key rejected as the request IP address '${clientIp}' is not set in Parse Server option 'masterKeyIps'.`
173+
);
170174
isMaster = false;
171175
}
172176

0 commit comments

Comments
 (0)