Skip to content

Commit

Permalink
Improve logging pattern schema validation (#200317)
Browse files Browse the repository at this point in the history
## Summary

This makes pattern schema validation stricter by avoiding unrestricted
matching, enforces realistic timezone constraints, and sets an upper
bound to the date format string.

| Characteristic | Before | After |
----|----|----|
| Optional Groups | Capturing groups for optional sections. |
Non-capturing groups for optional sections.|
|Timezone Validation | Allows any characters except }. | Restricts to
A-Za-z/_+-.|
|Clarity | More complex and verbose. | Cleaner, more restrictive, and
simpler.|
|Performance | Captures unnecessary groups. | Limits captures to needed
named groups.|

---------

Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
TinaHeiligers and elasticmachine authored Nov 18, 2024
1 parent d910e5e commit c7bcc5c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { last } from 'lodash';
import { LogRecord } from '@kbn/logging';
import { Conversion } from './types';

const dateRegExp = /%date({(?<format>[^}]+)})?({(?<timezone>[^}]+)})?/g;
const dateRegExp = /%date(?:\{(?<format>[^}]+)\})?(?:\{(?<timezone>[A-Za-z/_+-]+)\})?/g;

const formats = {
ISO8601: 'ISO8601',
Expand All @@ -29,7 +29,6 @@ function formatDate(
): string {
const momentDate = moment(date);
momentDate.tz(timezone ?? moment.tz.guess());

switch (dateFormat) {
case formats.ISO8601:
return momentDate.toISOString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,21 @@ describe('schema', () => {
`"Date format expected one of ISO8601, ISO8601_TZ, ABSOLUTE, UNIX, UNIX_MILLIS, but given: HH"`
);
});

it('fails on %date with schema too long', () => {
const generateLongFormat = () => {
const longFormat = [];
for (let i = 1; i < 1001; i++) {
longFormat.push(`${i}`);
}
return longFormat.join('');
};
expect(() =>
patternSchema.validate(`%date${generateLongFormat()}`)
).toThrowErrorMatchingInlineSnapshot(
`"value has length [2898] but it must have a maximum length of [1000]."`
);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
const DEFAULT_PATTERN = `[%date][%level][%logger] %message`;

export const patternSchema = schema.string({
maxLength: 1000,
validate: (string) => {
DateConversion.validate!(string);
},
Expand Down

0 comments on commit c7bcc5c

Please sign in to comment.