Skip to content

Commit

Permalink
Add iteration limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zacqary committed Jan 6, 2025
1 parent 01d9d03 commit 2615266
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/platform/packages/shared/kbn-rrule/rrule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1131,5 +1131,19 @@ describe('RRule', () => {
`"Cannot create RRule: interval must be greater than 0"`
);
});
it('throws an error when exceeding the iteration limit', () => {
const testFn = () => {
const rule = new RRule({
dtstart: new Date(DATE_2020),
freq: Frequency.YEARLY,
byyearday: [1],
interval: 1,
tzid: 'UTC',
});
rule.all(100001);
};

expect(testFn).toThrowErrorMatchingInlineSnapshot(`"RRule iteration limit exceeded"`);
});
});
});
7 changes: 7 additions & 0 deletions src/platform/packages/shared/kbn-rrule/rrule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type AllResult = Date[] & {
};

const ALL_LIMIT = 10000;
const TIMEOUT_LIMIT = 100000;

export class RRule {
private options: Options;
Expand Down Expand Up @@ -65,12 +66,17 @@ export class RRule {
.toDate();

const nextRecurrences: Moment[] = [];
let iters = 0;

while (
(!count && !until) ||
(count && yieldedRecurrenceCount < count) ||
(until && current.getTime() < new Date(until).getTime())
) {
iters++;
if (iters > TIMEOUT_LIMIT) {
throw new Error('RRule iteration limit exceeded');
}
const next = nextRecurrences.shift()?.toDate();
if (next) {
current = next;
Expand Down Expand Up @@ -281,6 +287,7 @@ const getYearOfRecurrences = function ({

return derivedByyearday.flatMap((dayOfYear) => {
const currentDate = moment(refDT).dayOfYear(dayOfYear);
if (currentDate.year() !== refDT.year()) return [];
if (!derivedByweekday.includes(currentDate.isoWeekday())) return [];
return getDayOfRecurrences({ refDT: currentDate, byhour, byminute, bysecond });
});
Expand Down

0 comments on commit 2615266

Please sign in to comment.