Skip to content

Commit 232aa67

Browse files
committed
🐛 fix: url validation
1 parent f4039c3 commit 232aa67

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

src/components/githubDomainChecker.tsx

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,39 @@ interface GitHubDomainCheckerProps {
99
export function GitHubDomainChecker({id}: GitHubDomainCheckerProps = {}) {
1010
const [domain, setDomain] = useState('');
1111
const [isValidDomain, setIsValidDomain] = useState(false);
12+
13+
// Safe function to check if domain is github.com or its subdomain
14+
const isValidGitHubDomain = (input_domain: string): boolean => {
15+
try {
16+
const url = new URL(input_domain.startsWith('http') ? input_domain : `https://${input_domain}`);
17+
const hostname = url.hostname.toLowerCase();
18+
19+
// Exact match for github.com or valid subdomain (like gist.github.com)
20+
return hostname === 'github.com' || hostname.endsWith('.github.com');
21+
} catch {
22+
return false;
23+
}
24+
};
25+
26+
// Safe function to check if it's an enterprise GitHub domain
27+
const isValidEnterpriseDomain = (input_domain: string): boolean => {
28+
try {
29+
const url = new URL(input_domain.startsWith('http') ? input_domain : `https://${input_domain}`);
30+
const hostname = url.hostname.toLowerCase();
31+
32+
// Must be a valid domain with TLD, but not github.com
33+
const domainPattern = /^[\w\-\.]+\.[\w]{2,}$/;
34+
return domainPattern.test(hostname) && !hostname.endsWith('.github.com') && hostname !== 'github.com';
35+
} catch {
36+
return false;
37+
}
38+
};
1239

13-
// Updated to handle github.com URLs with paths (e.g., github.com/user)
1440
const isGitHubCom = (() => {
1541
const trimmedDomain = domain.toLowerCase().trim();
1642
if (!trimmedDomain) return false;
17-
18-
// Remove protocol if present
19-
const domainWithoutProtocol = trimmedDomain.replace(/^https?:\/\//, '');
20-
21-
// Check if it starts with github.com (with or without path)
22-
return domainWithoutProtocol.startsWith('github.com');
43+
44+
return isValidGitHubDomain(trimmedDomain);
2345
})();
2446

2547
const hasInput = domain.trim().length > 0;
@@ -31,17 +53,20 @@ export function GitHubDomainChecker({id}: GitHubDomainCheckerProps = {}) {
3153
setIsValidDomain(false);
3254
return;
3355
}
56+
57+
// Check if it's a valid GitHub.com domain or subdomain
58+
if (isValidGitHubDomain(trimmedDomain)) {
59+
setIsValidDomain(true);
60+
return;
61+
}
3462

35-
// Check if it contains github.com (valid)
36-
if (trimmedDomain.toLowerCase().includes('github.com')) {
63+
// For enterprise, validate as proper domain
64+
if (isValidEnterpriseDomain(trimmedDomain)) {
3765
setIsValidDomain(true);
3866
return;
3967
}
4068

41-
// For enterprise, check if it's a valid URL or domain format
42-
const urlPattern = /^(https?:\/\/)?([\w\-\.]+\.[\w]{2,})(\/.*)?$/;
43-
const isValidUrl = urlPattern.test(trimmedDomain);
44-
setIsValidDomain(isValidUrl);
69+
setIsValidDomain(false);
4570
};
4671

4772
const handleDomainChange = ev => {

0 commit comments

Comments
 (0)