Skip to content

Commit bc59433

Browse files
f-nief_niebugwelleDavid-Kunz
authored
fix: Adds webhook size limit parameter (#42)
Fixes #40 --------- Co-authored-by: f_nie <frank_nietzold@sap.com> Co-authored-by: Andre Meyering <info@andremeyering.de> Co-authored-by: Dr. David A. Kunz <david.kunz@sap.com>
1 parent d35a343 commit bc59433

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

README.md

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
[![REUSE status](https://api.reuse.software/badge/github.com/cap-js/event-broker)](https://api.reuse.software/info/github.com/cap-js/event-broker)
44

5-
6-
75
## About this project
86

97
CDS plugin providing integration with SAP Cloud Application Event Hub (technical name: `event-broker`).
108

11-
12-
139
## Table of Contents
1410

1511
- [About this project](#about-this-project)
@@ -19,15 +15,11 @@ CDS plugin providing integration with SAP Cloud Application Event Hub (technical
1915
- [Code of Conduct](#code-of-conduct)
2016
- [Licensing](#licensing)
2117

22-
23-
2418
## Requirements
2519

2620
See [Getting Started](https://cap.cloud.sap/docs/get-started/in-a-nutshell) on how to jumpstart your development and grow as you go with SAP Cloud Application Programming Model (CAP).
2721
To learn about messaging in CAP, please consult the guide on [Events & Messaging](https://cap.cloud.sap/docs/guides/messaging/).
2822

29-
30-
3123
## Setup
3224

3325
Install the plugin via:
@@ -67,26 +59,37 @@ If you are not using [IAS-based Authentication](https://cap.cloud.sap/docs/node.
6759

6860
For more information, please see [SAP Cloud Application Event Hub](https://help.sap.com/docs/sap-cloud-application-event-hub) in SAP Help Portal.
6961

62+
## Parameters
7063

64+
### webhookSizeLimit
7165

72-
## Support, Feedback, Contributing
66+
To set a size limit for events accepted by the webhook, set the ``webhookSizeLimit``parameter in the ``package.json`` file in the root folder of your app, e.g.
7367

74-
This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/event-broker/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
68+
```jsonc
69+
"cds": {
70+
"requires": {
71+
"messaging": {
72+
"kind": "event-broker",
73+
"webhookSizeLimit": "1mb"
74+
}
75+
}
76+
}
77+
```
78+
79+
If the parameter is not set, the [global request body size limit](https://pages.github.tools.sap/cap/docs/node.js/cds-server#maximum-request-body-size) ``cds.env.server.body_parser.limit`` is taken into account. If this parameter is not set either, the default value of ``1mb``is used.
7580

81+
## Support, Feedback, Contributing
7682

83+
This project is open to feature requests/suggestions, bug reports etc. via [GitHub issues](https://github.com/cap-js/event-broker/issues). Contribution and feedback are encouraged and always welcome. For more information about how to contribute, the project structure, as well as additional contribution information, see our [Contribution Guidelines](CONTRIBUTING.md).
7784

7885
## Security / Disclosure
7986

8087
If you find any bug that may be a security problem, please follow our instructions at [in our security policy](https://github.com/cap-js/event-broker/security/policy) on how to report it. Please do not create GitHub issues for security-related doubts or problems.
8188

82-
83-
8489
## Code of Conduct
8590

8691
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone. By participating in this project, you agree to abide by its [Code of Conduct](https://github.com/cap-js/.github/blob/main/CODE_OF_CONDUCT.md) at all times.
8792

88-
89-
9093
## Licensing
9194

9295
Copyright 2024 SAP SE or an SAP affiliate company and event-broker contributors. Please see our [LICENSE](LICENSE) for copyright and license information. Detailed information including third-party components and their licensing/copyright information is available [via the REUSE tool](https://api.reuse.software/info/github.com/cap-js/event-broker).

cds-plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,9 @@ class EventBroker extends cds.MessagingService {
311311
} else {
312312
cds.app.post(webhookBasePath, _validateCertificate.bind(this))
313313
}
314-
cds.app.post(webhookBasePath, express.json())
314+
315+
const limit = this.options.webhookSizeLimit ?? cds.env.server.body_parser?.limit ?? "1mb"
316+
cds.app.post(webhookBasePath, express.json({ limit }))
315317
cds.app.post(webhookBasePath, this.onEventReceived.bind(this))
316318
}
317319

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"format": "cloudevents",
3636
"outbox": true,
3737
"webhookPath": "/-/cds/event-broker/webhook",
38+
"webhookSizeLimit": "1mb",
3839
"vcap": {
3940
"label": "event-broker"
4041
}

test/event-broker-error-handling/event-broker-errors.test.js renamed to test/event-broker-unit-tests/event-broker-errors.test.js

File renamed without changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
describe('event broker webhook endpoint registration', () => {
2+
test.each([
3+
{
4+
name: 'webhookSizeLimit correctly passed to express.json',
5+
webhookPath: '/webhook-test',
6+
webhookSizeLimit: '5mb',
7+
bodyParserLimit: undefined,
8+
expectedLimit: '5mb'
9+
},
10+
{
11+
name: 'body_parser.limit overrides webhookSizeLimit',
12+
webhookPath: '/webhook-test-2',
13+
webhookSizeLimit: undefined,
14+
bodyParserLimit: '42mb',
15+
expectedLimit: '42mb'
16+
}
17+
])('$name', async ({ webhookPath, webhookSizeLimit, bodyParserLimit, expectedLimit }) => {
18+
// Arrange
19+
const express = require('express')
20+
const originalJson = express.json
21+
let calledLimit
22+
express.json = opts => {
23+
calledLimit = opts.limit
24+
return (req, res, next) => next()
25+
}
26+
27+
const cds = require('@sap/cds')
28+
const originalServer = cds.env.server
29+
if (bodyParserLimit) {
30+
cds.env.server = { body_parser: { limit: bodyParserLimit } }
31+
}
32+
33+
const EventBroker = require('../../cds-plugin.js')
34+
const eb = new EventBroker('event-broker')
35+
eb.options = {
36+
webhookPath,
37+
webhookSizeLimit,
38+
credentials: {
39+
ias: { clientId: 'clientId' },
40+
}
41+
}
42+
eb.auth = { kind: 'ias', ias: { credentials: { certificate: 'cert', key: 'key' }, clientId: 'clientId' } }
43+
44+
const useMock = jest.fn()
45+
const postMock = jest.fn()
46+
cds.app = { use: useMock, post: postMock }
47+
48+
// Act
49+
eb.registerWebhookEndpoints()
50+
51+
// Assert
52+
expect(calledLimit).toBe(expectedLimit)
53+
54+
// Cleanup
55+
express.json = originalJson
56+
cds.server = originalServer
57+
})
58+
59+
})

0 commit comments

Comments
 (0)