Skip to content

Commit

Permalink
Validate regex in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Sep 11, 2024
1 parent 2b90573 commit 8e8f087
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import test from 'node:test';
import assert, { throws } from 'node:assert';
import { Durationable, Options } from './schema.ts';
import assert, { strictEqual, throws } from 'node:assert';
import { Durationable, Options, yamlPattern } from './schema.ts';
import { Temporal } from 'temporal-polyfill';
import { durationEqual, optionsEqual } from './assert.ts';
import { z } from 'zod';
import { deepStrictEqual } from 'node:assert/strict';
import { checkSync } from 'recheck';

const defaultOptions = Object.freeze({
isEarlyExit: true,
Expand Down Expand Up @@ -76,6 +77,10 @@ test('Options accept all yaml extensions', () => {
);
});

test('regex option does not have higher ReDoS possibilities', () => {
strictEqual(checkSync(yamlPattern.source, '').status, 'safe');
});

test('Options reject invalid values', () => {
throws(() => Options.parse({ ...defaultOptions, leastInterval: Temporal.Duration.from({ seconds: 0 }) }), {
name: 'ZodError',
Expand Down
3 changes: 2 additions & 1 deletion src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export function getDuration(durationable: string | MyDurationLike): Duration {
throw new Error('unexpected value is specified in durations');
}

const workflowFile = z.string().regex(/\.(yml|yaml)$/);
export const yamlPattern = /\.(yml|yaml)$/;
const workflowFile = z.string().regex(yamlPattern);
const matchAllJobs = z.object({
workflowFile: workflowFile,
jobName: z.undefined(), // Preferring undefined over null for backward compatibility
Expand Down

0 comments on commit 8e8f087

Please sign in to comment.