Skip to content

Commit 83eb19f

Browse files
committed
ensure uuid is valid in configuration
1 parent 1844093 commit 83eb19f

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/core/server/http/http_config.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* under the License.
1818
*/
1919

20+
import uuid from 'uuid';
2021
import { config, HttpConfig } from '.';
2122
import { Env } from '../config';
2223
import { getEnvOptions } from '../config/__mocks__/env';
@@ -77,6 +78,14 @@ test('throws if basepath is not specified, but rewriteBasePath is set', () => {
7778
expect(() => httpSchema.validate(obj)).toThrowErrorMatchingSnapshot();
7879
});
7980

81+
test('accepts only valid uuids for server.uuid', () => {
82+
const httpSchema = config.schema;
83+
expect(() => httpSchema.validate({ uuid: uuid.v4() })).not.toThrow();
84+
expect(() => httpSchema.validate({ uuid: 'not an uuid' })).toThrowErrorMatchingInlineSnapshot(
85+
`"[uuid]: must be a valid uuid"`
86+
);
87+
});
88+
8089
describe('with TLS', () => {
8190
test('throws if TLS is enabled but `key` is not specified', () => {
8291
const httpSchema = config.schema;

src/core/server/http/http_config.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Env } from '../config';
2222
import { SslConfig, sslSchema } from './ssl_config';
2323

2424
const validBasePathRegex = /(^$|^\/.*[^\/]$)/;
25+
const uuidRegexp = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
2526

2627
const match = (regex: RegExp, errorMsg: string) => (str: string) =>
2728
regex.test(str) ? undefined : errorMsg;
@@ -91,7 +92,11 @@ export const config = {
9192
)
9293
),
9394
}),
94-
uuid: schema.maybe(schema.string()),
95+
uuid: schema.maybe(
96+
schema.string({
97+
validate: match(uuidRegexp, 'must be a valid uuid'),
98+
})
99+
),
95100
},
96101
{
97102
validate: rawConfig => {

0 commit comments

Comments
 (0)