Limit bytesizevalue length for 8.x/7.x (#200733)

## Summary

Inspired by https://github.com/elastic/kibana/pull/193529 but does not
change the regular expression, it only limits the string length which is
anyway the biggest performance improvement. This makes it a lot safer to
backport since it's less likely that we could break existing kibana
configurations that had typos.


### Checklist

### Identify risks

- [ ] Could cause a Kibana to refuse to start up after an upgrade if it had a byte size configuration value that was excessively long.
This commit is contained in:
Rudolf Meijering 2024-11-20 15:57:29 +01:00 committed by GitHub
parent 4a0ccdb6c4
commit b476f7fb4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View file

@ -36,11 +36,28 @@ describe('parsing units', () => {
expect(ByteSizeValue.parse('1Mb').getValueInBytes()).toBe(1024 * 1024);
});
test('parses the max safe integer', () => {
expect(ByteSizeValue.parse('9007199254740991').getValueInBytes()).toBe(9007199254740991);
expect(ByteSizeValue.parse('9007199254740991b').getValueInBytes()).toBe(9007199254740991);
});
test('throws an error when unsupported unit specified', () => {
expect(() => ByteSizeValue.parse('1tb')).toThrowErrorMatchingInlineSnapshot(
`"Failed to parse value as byte value. Value must be either number of bytes, or follow the format <count>[b|kb|mb|gb] (e.g., '1024kb', '200mb', '1gb'), where the number is a safe positive integer."`
);
});
test('throws an error when unsafe integer', () => {
expect(() => ByteSizeValue.parse('9007199254740992')).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
});
test('throws an error on unusually long input', () => {
expect(() => ByteSizeValue.parse('19007199254740991kb')).toThrowErrorMatchingInlineSnapshot(
`"Value in bytes is expected to be a safe positive integer."`
);
});
});
describe('#constructor', () => {

View file

@ -23,6 +23,10 @@ function renderUnit(value: number, unit: string) {
export class ByteSizeValue {
public static parse(text: string): ByteSizeValue {
if (text.length > 18) {
// Exit early on large input where <count> uses more than 16 digits and is therefore larger than Number.MAX_SAFE_INTEGER
throw new Error('Value in bytes is expected to be a safe positive integer.');
}
const match = /([1-9][0-9]*)(b|kb|mb|gb)/i.exec(text);
if (!match) {
const number = Number(text);