@jitar-plugins/http 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -42,13 +42,7 @@ export default new CorsMiddleware(origin, headers);
42
42
 
43
43
  import { OriginMiddleware } from '@jitar-plugins/http';
44
44
 
45
- const options = {
46
- path: '/rpc', // the path the cookie is valid for (optional, default: '/')
47
- sameSite: 'Strict' | 'Lax' | 'None', // the SameSite attribute of the cookie (optional, default: 'Strict')
48
- secure: true // the Secure attribute of the cookie (optional, default: true)
49
- };
50
-
51
- export default new OriginMiddleware(options);
45
+ export default new OriginMiddleware();
52
46
  ```
53
47
 
54
48
  ### Step 2 - Activate the middleware
@@ -1,12 +1,5 @@
1
1
  import type { Middleware, NextHandler, Request, Response } from 'jitar';
2
- type Options = {
3
- path?: string;
4
- sameSite?: string;
5
- secure?: boolean;
6
- };
7
2
  export default class OriginMiddleware implements Middleware {
8
3
  #private;
9
- constructor(options?: Options);
10
4
  handle(request: Request, next: NextHandler): Promise<Response>;
11
5
  }
12
- export {};
@@ -1,61 +1,29 @@
1
1
  import { BadRequest } from 'jitar';
2
- const ORIGIN_COOKIE_NAME = 'x-client-origin';
3
2
  export default class OriginMiddleware {
4
- #path;
5
- #sameSite;
6
- #secure;
7
- constructor(options) {
8
- this.#path = options?.path ?? '/rpc';
9
- this.#sameSite = options?.sameSite ?? 'Strict';
10
- this.#secure = options?.secure ?? true;
11
- }
12
3
  async handle(request, next) {
13
- let fromCookie = true;
14
- let origin = this.#getOriginFromCookie(request);
15
- if (origin === undefined) {
16
- fromCookie = false;
17
- origin = this.#getOriginFromHeader(request);
18
- }
4
+ const origin = this.#getOrigin(request);
19
5
  this.#validateOriginValue(origin);
20
6
  // The origin header is validated and set here for use in other middlewares
21
7
  request.setHeader('origin', origin);
22
- const response = await next();
23
- if (fromCookie === false) {
24
- this.#setOriginCookie(response, origin);
25
- }
26
- return response;
8
+ return next();
27
9
  }
28
- #getOriginFromHeader(request) {
29
- return request.getHeader('origin');
10
+ #getOrigin(request) {
11
+ const host = request.getHeader('X-Forwarded-Host') ?? request.getHeader('Host');
12
+ const protocol = request.getHeader('X-Forwarded-Proto');
13
+ this.#validateHeaderValues(protocol, host);
14
+ return `${protocol}://${host}`;
30
15
  }
31
- #getOriginFromCookie(request) {
32
- const header = request.getHeader('cookie');
33
- if (header === undefined) {
34
- return;
16
+ #validateHeaderValues(protocol, host) {
17
+ if (protocol === undefined) {
18
+ throw new BadRequest('Missing protocol header');
35
19
  }
36
- for (const cookie of header.split(';')) {
37
- const [key, value] = cookie.split('=');
38
- if (key.trim() === ORIGIN_COOKIE_NAME) {
39
- return value?.trim();
40
- }
20
+ if (host === undefined) {
21
+ throw new BadRequest('Missing host header');
41
22
  }
42
23
  }
43
24
  #validateOriginValue(value) {
44
- if (value === undefined) {
45
- throw new BadRequest('Invalid origin');
46
- }
47
- try {
48
- new URL(value);
49
- }
50
- catch {
25
+ if (URL.canParse(value) === false) {
51
26
  throw new BadRequest('Invalid origin');
52
27
  }
53
28
  }
54
- #setOriginCookie(response, origin) {
55
- let cookie = `${ORIGIN_COOKIE_NAME}=${origin}; Path=${this.#path}; HttpOnly; SameSite=${this.#sameSite}`;
56
- if (this.#secure) {
57
- cookie += ' Secure';
58
- }
59
- response.setHeader('Set-Cookie', cookie);
60
- }
61
29
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jitar-plugins/http",
3
3
  "private": false,
4
- "version": "0.2.1",
4
+ "version": "0.2.3",
5
5
  "type": "module",
6
6
  "repository": {
7
7
  "url": "git+https://github.com/MaskingTechnology/jitar-plugins.git"
@@ -22,6 +22,6 @@
22
22
  "types": "dist/index.d.ts",
23
23
  "exports": "./dist/index.js",
24
24
  "peerDependencies": {
25
- "jitar": "^0.11.0 || ^0.12.0"
25
+ "jitar": ">0.11 || >0.12 || >0.13"
26
26
  }
27
27
  }