You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(binding-http): Make Access-Control-Allow-Origin header configurable
Add allowedOrigins option to HttpConfig interface to let users configure
the Access-Control-Allow-Origin header value. Defaults to '*' for backward
compatibility. Secured things still echo the request origin with credentials
regardless of this setting.
Fixes#941
Signed-off-by: jona42-ui <jonathanthembo123@gmail.com>
Copy file name to clipboardExpand all lines: packages/binding-http/README.md
+16Lines changed: 16 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -180,6 +180,7 @@ The protocol binding can be configured using his constructor or trough servient
180
180
baseUri?:string// A Base URI to be used in the TD in cases where the client will access a different URL than the actual machine serving the thing. [See Using BaseUri below]
181
181
urlRewrite?:Record<string, string>// A record to allow for other URLs pointing to existing endpoints, e.g., { "/myroot/myUrl": "/test/properties/test" }
182
182
middleware?:MiddlewareRequestHandler; // the MiddlewareRequestHandler function. See [Adding a middleware] section below.
183
+
allowedOrigins?:string; // Configures the Access-Control-Allow-Origin header. Defaults to "*" (any origin). See [Configuring CORS] section below.
183
184
}
184
185
```
185
186
@@ -305,6 +306,21 @@ The exposed thing on the internal server will product form URLs such as:
305
306
306
307
> `address` tells the HttpServer a specific local network interface to bind its TCP listener.
307
308
309
+
### Configuring CORS
310
+
311
+
By default, the HTTP binding sets the `Access-Control-Allow-Origin` header to `"*"`, allowing any origin to access exposed Things. You can restrict this to a specific origin using the `allowedOrigins` configuration option:
312
+
313
+
```js
314
+
servient.addServer(
315
+
new HttpServer({
316
+
port: 8080,
317
+
allowedOrigins: "https://my-app.example.com",
318
+
})
319
+
);
320
+
```
321
+
322
+
When a security scheme (e.g. `basic`, `bearer`) is configured, the server echoes the request's `Origin` header and sets `Access-Control-Allow-Credentials: true`, regardless of the `allowedOrigins` value. This is required for browsers to send credentials in cross-origin requests.
323
+
308
324
### Adding a middleware
309
325
310
326
HttpServer supports the addition of **middleware** to handle the raw HTTP requests before they hit the Servient. In the middleware function, you can run some logic to filter and eventually reject HTTP requests (e.g. based on some custom headers).
0 commit comments