Skip to content

Commit e2595cf

Browse files
committed
fix: nested batch sub-requests cause unclear error
1 parent 82edbd7 commit e2595cf

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

spec/batch.spec.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,4 +852,61 @@ describe('batch', () => {
852852
expect(result.data).toEqual(jasmine.any(Array));
853853
});
854854
});
855+
856+
describe('nested batch requests', () => {
857+
it('rejects sub-request that targets the batch endpoint', async () => {
858+
await expectAsync(
859+
request({
860+
method: 'POST',
861+
url: 'http://localhost:8378/1/batch',
862+
headers,
863+
body: JSON.stringify({
864+
requests: [
865+
{
866+
method: 'POST',
867+
path: '/1/batch',
868+
body: {
869+
requests: [{ method: 'GET', path: '/1/classes/TestClass' }],
870+
},
871+
},
872+
],
873+
}),
874+
})
875+
).toBeRejectedWith(
876+
jasmine.objectContaining({
877+
status: 400,
878+
data: jasmine.objectContaining({
879+
error: 'nested batch requests are not allowed',
880+
}),
881+
})
882+
);
883+
});
884+
885+
it('rejects when any sub-request among valid ones targets the batch endpoint', async () => {
886+
await expectAsync(
887+
request({
888+
method: 'POST',
889+
url: 'http://localhost:8378/1/batch',
890+
headers,
891+
body: JSON.stringify({
892+
requests: [
893+
{ method: 'GET', path: '/1/classes/TestClass' },
894+
{
895+
method: 'POST',
896+
path: '/1/batch',
897+
body: { requests: [{ method: 'GET', path: '/1/classes/TestClass' }] },
898+
},
899+
],
900+
}),
901+
})
902+
).toBeRejectedWith(
903+
jasmine.objectContaining({
904+
status: 400,
905+
data: jasmine.objectContaining({
906+
error: 'nested batch requests are not allowed',
907+
}),
908+
})
909+
);
910+
});
911+
});
855912
});

src/batch.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ async function handleBatch(router, req) {
7878
if (!restRequest || typeof restRequest !== 'object' || typeof restRequest.path !== 'string') {
7979
throw new Parse.Error(Parse.Error.INVALID_JSON, 'batch request path must be a string');
8080
}
81+
if (restRequest.method === 'POST' && restRequest.path.endsWith(batchPath)) {
82+
throw new Parse.Error(Parse.Error.INVALID_JSON, 'nested batch requests are not allowed');
83+
}
8184
}
8285

8386
// The batch paths are all from the root of our domain.

0 commit comments

Comments
 (0)