Skip to content

Commit cfa2eb5

Browse files
authored
Merge pull request #1222 from flatcar/fix/code-scanning-41-inefficient-regular-expression
fix(frontend:regex): mitigate regex inefficiency
2 parents 6889d39 + a155c32 commit cfa2eb5

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, test } from 'vitest';
2+
3+
import { REGEX_REVERSE_DOMAIN_ID } from '../../utils/regex';
4+
5+
describe('reverseDomainIdRegex', () => {
6+
const regex = REGEX_REVERSE_DOMAIN_ID;
7+
8+
test.each([
9+
// valid examples
10+
['com.example', true],
11+
['org.my-app', true],
12+
['net.service1', true],
13+
['a.b', true],
14+
['A.B1', true],
15+
['abc.def-ghi', true],
16+
['MyCompany.MyApp', true],
17+
18+
// invalid examples
19+
['a', false], // only one segment
20+
['com.', false], // ends with a dot
21+
['.example', false], // starts with a dot
22+
['com..example', false], // double dots
23+
['com.-example', false], // segment starts with dash
24+
['com.exa-', false], // segment ends with dash
25+
['1com.example', false], // first segment starts with digit
26+
['com.exa$mple', false], // invalid character
27+
['com.exa mple', false], // space not allowed
28+
['com..example', false], // empty segment
29+
])('validates "%s"', (input, expected) => {
30+
expect(regex.test(input)).toBe(expected);
31+
});
32+
});

frontend/src/components/Applications/ApplicationEdit.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Yup from 'yup';
1212

1313
import { Application } from '../../api/apiDataTypes';
1414
import { applicationsStore } from '../../stores/Stores';
15+
import { REGEX_REVERSE_DOMAIN_ID } from '../../utils/regex';
1516

1617
export interface ApplicationEditProps {
1718
create?: any;
@@ -152,10 +153,7 @@ export default function ApplicationEdit(props: ApplicationEditProps) {
152153
// * All characters must be alphanumeric, or a dash.
153154
// Each segment must start with a letter.
154155
// Each segment must not end with a dash.
155-
.matches(
156-
/^[a-zA-Z]+([a-zA-Z0-9-]*[a-zA-Z0-9])*(\.[a-zA-Z]+([a-zA-Z0-9-]*[a-zA-Z0-9])*)+$/,
157-
t('common|reverse_domain_id_error')
158-
)
156+
.matches(REGEX_REVERSE_DOMAIN_ID, t('common|reverse_domain_id_error'))
159157
.nullable(),
160158
description: Yup.string().max(
161159
maxDescChars,

frontend/src/utils/regex.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
export const REGEX_SEMVER =
33
/^((\d+)\.(\d+)\.(\d+))(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?$/;
44
export const REGEX_SIZE = /^[0-9]{0,20}$/;
5+
export const REGEX_REVERSE_DOMAIN_ID =
6+
/^[A-Za-z](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+$/;

0 commit comments

Comments
 (0)