-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Add http2 support for Kibana server #183465
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
364ee52
2205584
d893a06
a9f73a1
7ba90c9
8b7f775
c250d41
9c2f9aa
3bc8265
1eb5d7f
0a06093
447548d
f43ee8f
ffbab26
537a365
e7819fb
12b4a1c
55c0e10
ec6e95c
84009a5
1ae9eb2
41b7951
0799cba
fd5b5a8
5be430e
943acfa
8bd0bc7
1d579cd
2dcdc59
ca2fb97
da6d7d9
69f81f2
9fa7428
e61e45f
279b49c
c257870
ecebcbd
d8ed82f
d374a63
37e54cf
7092c3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1627,6 +1627,8 @@ | |
| "html": "1.0.0", | ||
| "html-loader": "^1.3.2", | ||
| "http-proxy": "^1.18.1", | ||
| "http2-proxy": "^5.0.53", | ||
| "http2-wrapper": "^2.2.1", | ||
|
Comment on lines
+1630
to
+1631
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dev deps - only used by the new http2 basepathproxy server in dev setup |
||
| "ignore": "^5.3.0", | ||
| "jest": "^29.6.1", | ||
| "jest-canvas-mock": "^2.5.2", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,7 @@ import { HapiResponseAdapter } from './response_adapter'; | |
| import { wrapErrors } from './error_wrapper'; | ||
| import { Method } from './versioned_router/types'; | ||
| import { prepareRouteConfigValidation } from './util'; | ||
| import { stripIllegalHttp2Headers } from './strip_illegal_http2_headers'; | ||
|
|
||
| export type ContextEnhancer< | ||
| P, | ||
|
|
@@ -265,6 +266,14 @@ export class Router<Context extends RequestHandlerContextBase = RequestHandlerCo | |
|
|
||
| try { | ||
| const kibanaResponse = await handler(kibanaRequest, kibanaResponseFactory); | ||
| if (kibanaRequest.protocol === 'http2' && kibanaResponse.options.headers) { | ||
| kibanaResponse.options.headers = stripIllegalHttp2Headers({ | ||
| headers: kibanaResponse.options.headers, | ||
| isDev: this.options.isDev ?? false, | ||
| logger: this.log, | ||
| requestContext: `${request.route.method} ${request.route.path}`, | ||
| }); | ||
| } | ||
|
Comment on lines
+269
to
+276
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Illegal http2 headers (see That's why we're removing it in the handler wrapper, and we log a warning in development mode so that developers can be informed and remove it from their handler. |
||
| return hapiResponseAdapter.handle(kibanaResponse); | ||
| } catch (error) { | ||
| // capture error | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { stripIllegalHttp2Headers } from './strip_illegal_http2_headers'; | ||
| import { loggerMock, MockedLogger } from '@kbn/logging-mocks'; | ||
|
|
||
| describe('stripIllegalHttp2Headers', () => { | ||
| let logger: MockedLogger; | ||
|
|
||
| beforeEach(() => { | ||
| logger = loggerMock.create(); | ||
| }); | ||
|
|
||
| it('removes illegal http2 headers', () => { | ||
| const headers = { | ||
| 'x-foo': 'bar', | ||
| 'x-hello': 'dolly', | ||
| connection: 'keep-alive', | ||
| 'proxy-connection': 'keep-alive', | ||
| 'keep-alive': 'true', | ||
| upgrade: 'probably', | ||
| 'transfer-encoding': 'chunked', | ||
| 'http2-settings': 'yeah', | ||
| }; | ||
| const output = stripIllegalHttp2Headers({ | ||
| headers, | ||
| isDev: false, | ||
| logger, | ||
| requestContext: 'requestContext', | ||
| }); | ||
|
|
||
| expect(output).toEqual({ | ||
| 'x-foo': 'bar', | ||
| 'x-hello': 'dolly', | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores case when detecting headers', () => { | ||
| const headers = { | ||
| 'x-foo': 'bar', | ||
| 'x-hello': 'dolly', | ||
| Connection: 'keep-alive', | ||
| 'Proxy-Connection': 'keep-alive', | ||
| 'kEeP-AlIvE': 'true', | ||
| }; | ||
| const output = stripIllegalHttp2Headers({ | ||
| headers, | ||
| isDev: false, | ||
| logger, | ||
| requestContext: 'requestContext', | ||
| }); | ||
|
|
||
| expect(output).toEqual({ | ||
| 'x-foo': 'bar', | ||
| 'x-hello': 'dolly', | ||
| }); | ||
| }); | ||
|
|
||
| it('logs a warning about the illegal header when in dev mode', () => { | ||
| const headers = { | ||
| 'x-foo': 'bar', | ||
| Connection: 'keep-alive', | ||
| }; | ||
| stripIllegalHttp2Headers({ | ||
| headers, | ||
| isDev: true, | ||
| logger, | ||
| requestContext: 'requestContext', | ||
| }); | ||
|
|
||
| expect(logger.warn).toHaveBeenCalledTimes(1); | ||
| expect(logger.warn).toHaveBeenCalledWith( | ||
| `Handler for "requestContext" returned an illegal http2 header: Connection. Please check "request.protocol" in handlers before assigning connection headers` | ||
| ); | ||
| }); | ||
|
|
||
| it('does not log a warning about the illegal header when not in dev mode', () => { | ||
| const headers = { | ||
| 'x-foo': 'bar', | ||
| Connection: 'keep-alive', | ||
| }; | ||
| stripIllegalHttp2Headers({ | ||
| headers, | ||
| isDev: false, | ||
| logger, | ||
| requestContext: 'requestContext', | ||
| }); | ||
|
|
||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('does not mutate the original headers', () => { | ||
| const headers = { | ||
| 'x-foo': 'bar', | ||
| Connection: 'keep-alive', | ||
| }; | ||
| stripIllegalHttp2Headers({ | ||
| headers, | ||
| isDev: true, | ||
| logger, | ||
| requestContext: 'requestContext', | ||
| }); | ||
|
|
||
| expect(headers).toEqual({ | ||
| 'x-foo': 'bar', | ||
| Connection: 'keep-alive', | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
| * in compliance with, at your election, the Elastic License 2.0 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import type { Logger } from '@kbn/logging'; | ||
| import type { ResponseHeaders } from '@kbn/core-http-server'; | ||
|
|
||
| // from https://github.com/nodejs/node/blob/v22.2.0/lib/internal/http2/util.js#L557 | ||
| const ILLEGAL_HTTP2_CONNECTION_HEADERS = new Set([ | ||
| 'connection', | ||
| 'proxy-connection', | ||
| 'keep-alive', | ||
| 'upgrade', | ||
| 'transfer-encoding', | ||
| 'http2-settings', | ||
| ]); | ||
|
|
||
| /** | ||
| * Return a new version of the provided headers, with all illegal http2 headers removed. | ||
| * If `isDev` is `true`, will also log a warning if such header is encountered. | ||
| */ | ||
| export const stripIllegalHttp2Headers = ({ | ||
| headers, | ||
| isDev, | ||
| logger, | ||
| requestContext, | ||
| }: { | ||
| headers: ResponseHeaders; | ||
| isDev: boolean; | ||
| logger: Logger; | ||
| requestContext: string; | ||
| }): ResponseHeaders => { | ||
| return Object.entries(headers).reduce((output, [headerName, headerValue]) => { | ||
| if (ILLEGAL_HTTP2_CONNECTION_HEADERS.has(headerName.toLowerCase())) { | ||
| if (isDev) { | ||
| logger.warn( | ||
| `Handler for "${requestContext}" returned an illegal http2 header: ${headerName}. Please check "request.protocol" in handlers before assigning connection headers` | ||
| ); | ||
| } | ||
| } else { | ||
| output[headerName as keyof ResponseHeaders] = headerValue; | ||
| } | ||
| return output; | ||
| }, {} as ResponseHeaders); | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.