Skip to content

Commit 29fb678

Browse files
committed
fix(settings): regex sanitization, handle form submission on enter
1 parent d80daa3 commit 29fb678

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

src/modules/components/Settings.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export function Settings({
5454
onClose();
5555
}, [localBaseUrl, onClose, setBaseUrl]);
5656

57+
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
58+
e.preventDefault();
59+
onSave();
60+
};
61+
5762
return (
5863
<Modal
5964
variant={ModalVariant.small}
@@ -80,7 +85,10 @@ export function Settings({
8085
</Button>,
8186
]}
8287
>
83-
<Form id="settings-form">
88+
<Form
89+
id="settings-form"
90+
onSubmit={handleSubmit}
91+
>
8492
<FormGroup
8593
label="Override Rekor Endpoint"
8694
labelIcon={
@@ -130,6 +138,10 @@ export function Settings({
130138
</FormHelperText>
131139
)}
132140
</FormGroup>
141+
<button
142+
type="submit"
143+
style={{ display: "none" }}
144+
></button>
133145
</Form>
134146
</Modal>
135147
);

src/modules/utils/utils.test.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,25 @@ import { isAcceptedProtocol, isValidUrl, validateUrl } from "./validateUrl";
22

33
describe("URL Validation Tests", () => {
44
describe("Individual Function Tests", () => {
5-
it("isValidUrl: should validate URL structure", () => {
6-
expect(isValidUrl("http://validsite.com")).toBe(true);
7-
expect(isValidUrl("justastring")).toBe(false);
8-
expect(isValidUrl("")).toBe(false);
9-
expect(isValidUrl("http://invalidhostname")).toBe(false);
10-
});
11-
125
it("isAcceptedProtocol: should check for https protocols", () => {
136
expect(isAcceptedProtocol("http://example.com")).toBe(false);
147
expect(isAcceptedProtocol("example.com")).toBe(false);
158
expect(isAcceptedProtocol("www.example.com")).toBe(false);
169
expect(isAcceptedProtocol("ftp://example.com")).toBe(false);
10+
expect(isAcceptedProtocol("http://rekor")).toBe(false);
1711
expect(isAcceptedProtocol("https://example.com")).toBe(true);
1812
});
13+
14+
it("isValidUrl: http(s) protocol, valid characters, and tld", () => {
15+
expect(isValidUrl("http://rekor")).toBe(true);
16+
expect(isValidUrl("https://rekor")).toBe(true);
17+
expect(isValidUrl("https://rekor🦩")).toBe(false);
18+
expect(isValidUrl("https://rekor-example")).toBe(true);
19+
expect(isValidUrl("https://rekor-example.com")).toBe(true);
20+
expect(isValidUrl("https://")).toBe(false);
21+
expect(isValidUrl("https://₮∌⎛")).toBe(false);
22+
expect(isValidUrl("https://😝")).toBe(false);
23+
});
1924
});
2025

2126
describe("validateUrl: Composite Function Tests", () => {

src/modules/utils/validateUrl.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ export function isAcceptedProtocol(url: string): boolean {
1818
}
1919

2020
/**
21-
* Checks if the given string is a valid URL.
21+
* Checks if the given string is a valid URL, based on:
22+
* 1) http(s) protocol; 2) valid alphanumeric & special chars;
23+
* 3) combined length of subdomain & domain must be between 2 and 256
24+
* https://regex101.com/r/ecDRn6/1
2225
* @param url The URL to validate.
2326
* @returns True if the URL is valid, false otherwise.
2427
*/
2528
export const isValidUrl = (url: string): boolean => {
29+
const regexVal =
30+
/^(http(s)?:\/\/.)[-a-zA-Z0-9@:%._\+~#=]{2,256}[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&\/=]*)$/gm;
31+
2632
try {
27-
const parsedUrl = new URL(url);
28-
// check for presence of a dot
29-
return parsedUrl.hostname.includes(".");
33+
return regexVal.test(url);
3034
} catch (error) {
3135
return false;
3236
}

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
".next/types/**/*.ts",
2727
"jest.setup.js"
2828
],
29-
"exclude": ["coverage", "cypress", "node_modules"]
29+
"exclude": ["coverage", "cypress", "cypress.config.ts", "node_modules"]
3030
}

0 commit comments

Comments
 (0)