Skip to content

Commit

Permalink
🐛 Address regex url validation bug (#1586)
Browse files Browse the repository at this point in the history
Resolves [1197](https://issues.redhat.com/browse/MTA-1197)

Signed-off-by: ibolton336 <[email protected]>
  • Loading branch information
ibolton336 authored Dec 6, 2023
1 parent 836d531 commit 5b9531f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
104 changes: 61 additions & 43 deletions client/src/app/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,49 +98,67 @@ describe("utils", () => {
expect(result).toBe("myKey");
});

//URL Regex tests
it("Regex should validate git URLs", () => {
const testGitURLs: string[] = [
"[email protected]:konveyor/tackle2-ui",
"http://[email protected]:konveyor/tackle2-ui",
];

for (const url of testGitURLs) {
const gitTestResult = gitUrlRegex.test(url);
expect(gitTestResult).toBe(true);
}
});

it("Regex should validate standard URLs", () => {
const testStandardURLs: string[] = [
"http://www.foo.bar",
"www.foo.bar",
"https://www.github.com/ibolton336/tackle-testapp.git",
];

for (const url of testStandardURLs) {
const standardTestResult = standardURLRegex.test(url);
expect(standardTestResult).toBe(true);
}
});

it("Regex should fail when validating broken standard URLs", () => {
const testBrokenURLs: string[] = [
"",
" http://www.foo.bar ",
" http://www.foo",
" http://wrong",
"wwwfoo.bar",
"foo.bar",
"www.foo.b",
"foo.ba",
"[email protected]:konveyor/tackle2-ui",
];

for (const url of testBrokenURLs) {
const testResult = standardURLRegex.test(url);
expect(testResult).toBe(false);
}
describe("URL Regex tests", () => {
// Define your regex patterns here

it("Regex should validate git URLs", () => {
const testGitURLs = [
"[email protected]:konveyor/tackle2-ui.git",
"http://github.com/konveyor/tackle2-ui.git",
];

for (const url of testGitURLs) {
expect(gitUrlRegex.test(url)).toBe(true);
}
});

it("Regex should fail when validating incorrect git URLs", () => {
const testIncorrectGitURLs = [
"https://",
"git@",
"http://github.com/konveyor",
];

for (const url of testIncorrectGitURLs) {
const result = gitUrlRegex.test(url);
console.log(`Testing URL: ${url}, Result: ${result}`);

expect(result).toBe(false);
}
});

it("Regex should validate standard URLs", () => {
const testStandardURLs = [
"http://www.foo.bar",
"www.foo.bar",
"https://www.github.com/ibolton336/tackle-testapp.git",
];

for (const url of testStandardURLs) {
expect(standardURLRegex.test(url)).toBe(true);
}
});

it("Regex should fail when validating broken standard URLs", () => {
const testBrokenURLs = [
"",
"http://",
"https://",
"http:",
"http://www.foo",
"http://wrong",
"wwwfoo.bar",
"foo.bar",
"www.foo.b",
];

for (const url of testBrokenURLs) {
const result = standardURLRegex.test(url);
console.log(`Testing URL: ${url}, Result: ${result}`);

expect(result).toBe(false);
}
});
});

it("URL should match the same multiple times in a row", () => {
Expand Down
2 changes: 1 addition & 1 deletion client/src/app/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const standardURLRegex =
/^(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})$/;

export const gitUrlRegex =
/^(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\/?|#[-\d\w._]+?)$/;
/^(https?:\/\/[-\w.]+\/[-\w._]+\/[-\w._]+|git@[-\w.]+:[-\w._]+\/[-\w._]+)(\.git)?(\/?|#[-\d\w._]+)?$/;

export const standardStrictURLRegex =
/https:\/\/(www\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)/;
Expand Down

0 comments on commit 5b9531f

Please sign in to comment.