Skip to content

Commit

Permalink
Remove support for setting server.host to '0' (breaking)
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Jan 4, 2021
1 parent 8d2e51d commit ee6752c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
9 changes: 9 additions & 0 deletions src/core/server/http/__snapshots__/http_config.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 9 additions & 10 deletions src/core/server/http/http_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import { config, HttpConfig } from './http_config';
import { CspConfig } from '../csp';
import { ExternalUrlConfig } from '../external_url';

const validHostnames = ['www.example.com', '8.8.8.8', '::1', 'localhost'];
const invalidHostname = 'asdf$%^';
const validHostnames = ['www.example.com', '8.8.8.8', '::1', 'localhost', '0.0.0.0'];
const invalidHostnames = ['asdf$%^', '0'];

jest.mock('os', () => {
const original = jest.requireActual('os');
Expand All @@ -48,11 +48,10 @@ test('accepts valid hostnames', () => {
});

test('throws if invalid hostname', () => {
const httpSchema = config.schema;
const obj = {
host: invalidHostname,
};
expect(() => httpSchema.validate(obj)).toThrowErrorMatchingSnapshot();
for (const host of invalidHostnames) {
const httpSchema = config.schema;
expect(() => httpSchema.validate({ host })).toThrowErrorMatchingSnapshot();
}
});

describe('requestId', () => {
Expand Down Expand Up @@ -304,17 +303,17 @@ describe('with compression', () => {

test('throws if invalid referrer whitelist', () => {
const httpSchema = config.schema;
const invalidHostnames = {
const nonEmptyArray = {
compression: {
referrerWhitelist: [invalidHostname],
referrerWhitelist: invalidHostnames,
},
};
const emptyArray = {
compression: {
referrerWhitelist: [],
},
};
expect(() => httpSchema.validate(invalidHostnames)).toThrowErrorMatchingSnapshot();
expect(() => httpSchema.validate(nonEmptyArray)).toThrowErrorMatchingSnapshot();
expect(() => httpSchema.validate(emptyArray)).toThrowErrorMatchingSnapshot();
});

Expand Down
13 changes: 6 additions & 7 deletions src/core/server/http/http_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export const config = {
host: schema.string({
defaultValue: 'localhost',
hostname: true,
validate(value) {
if (value === '0') {
return 'value 0 is not a valid hostname';
}
},
}),
maxPayload: schema.byteSize({
defaultValue: '1048576b',
Expand Down Expand Up @@ -195,13 +200,7 @@ export class HttpConfig {
rawExternalUrlConfig: ExternalUrlConfig
) {
this.autoListen = rawHttpConfig.autoListen;
// TODO: Consider dropping support for '0' in v8.0.0. This value is passed
// to hapi, which validates it. Prior to hapi v20, '0' was considered a
// valid host, however the validation logic internally in hapi was
// re-written for v20 and hapi no longer considers '0' a valid host. For
// details, see:
// https://github.com/elastic/kibana/issues/86716#issuecomment-749623781
this.host = rawHttpConfig.host === '0' ? '0.0.0.0' : rawHttpConfig.host;
this.host = rawHttpConfig.host;
this.port = rawHttpConfig.port;
this.cors = rawHttpConfig.cors;
this.customResponseHeaders = Object.entries(rawHttpConfig.customResponseHeaders ?? {}).reduce(
Expand Down

0 comments on commit ee6752c

Please sign in to comment.