Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(community): validate trimmed namespace and URLs in profile forms (#20712)

Innei0ed70d0d8e52
diff --git a/src/routes/(main)/community/(detail)/user/features/SubmitRepoModal.tsx b/src/routes/(main)/community/(detail)/user/features/SubmitRepoModal.tsx
index f0ed06e2dd8..dc704ea34f2 100644
--- a/src/routes/(main)/community/(detail)/user/features/SubmitRepoModal.tsx
+++ b/src/routes/(main)/community/(detail)/user/features/SubmitRepoModal.tsx
@@ -9,6 +9,8 @@ import { useTranslation } from 'react-i18next';
import ImperativeModal from '@/components/ImperativeModal';
import { lambdaClient } from '@/libs/trpc/client';
+import { isGithubRepoUrl } from './githubRepoUrl';
+
interface SubmitRepoModalProps {
beforeSubmit?: () => Promise<{ actAs?: number } | void>;
onClose: () => void;
@@ -16,8 +18,6 @@ interface SubmitRepoModalProps {
open: boolean;
}
-const GITHUB_URL_REGEX = /^https?:\/\/github\.com\/[\w-]+\/[\w.-]+\/?$/;
-
export const SubmitRepoModal = memo<SubmitRepoModalProps>(
({ open, onClose, onSuccess, beforeSubmit }) => {
const { t } = useTranslation('discover');
@@ -88,7 +88,7 @@ export const SubmitRepoModal = memo<SubmitRepoModalProps>(
name="gitUrl"
required={t('user.githubUrlRequired')}
validate={(value: string) =>
- value && !GITHUB_URL_REGEX.test(value) ? t('user.githubUrlInvalid') : undefined
+ value && !isGithubRepoUrl(value) ? t('user.githubUrlInvalid') : undefined
}
>
<Input placeholder="https://github.com/username/repo" />
diff --git a/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.test.ts b/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.test.ts
new file mode 100644
index 00000000000..4db74589239
--- /dev/null
+++ b/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.test.ts
@@ -0,0 +1,13 @@
+import { describe, expect, it } from 'vitest';
+
+import { isGithubRepoUrl } from './githubRepoUrl';
+
+describe('isGithubRepoUrl', () => {
+ it('accepts a repo URL padded with whitespace', () => {
+ expect(isGithubRepoUrl(' https://github.com/lobehub/lobehub\n')).toBe(true);
+ });
+
+ it('rejects non-GitHub URLs', () => {
+ expect(isGithubRepoUrl('https://gitlab.com/lobehub/lobehub')).toBe(false);
+ });
+});
diff --git a/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.ts b/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.ts
new file mode 100644
index 00000000000..f010e79687a
--- /dev/null
+++ b/src/routes/(main)/community/(detail)/user/features/githubRepoUrl.ts
@@ -0,0 +1,4 @@
+const GITHUB_URL_REGEX = /^https?:\/\/github\.com\/[\w-]+\/[\w.-]+\/?$/;
+
+// The submitted URL is trimmed, so a pasted URL with a trailing newline must pass.
+export const isGithubRepoUrl = (value: string) => GITHUB_URL_REGEX.test(value.trim());
diff --git a/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/Content.tsx b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/Content.tsx
index 555548003b5..15854ad01cf 100644
--- a/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/Content.tsx
+++ b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/Content.tsx
@@ -29,6 +29,8 @@ import AvatarUpload from '@/components/AvatarUpload';
import { useFileStore } from '@/store/file';
import type { DiscoverUserInfo } from '@/types/discover';
+import { getNamespaceError, isNamespaceFormatValid, isWebsiteUrlValid } from './validation';
+
interface FormValues {
description?: string;
displayName: string;
@@ -58,14 +60,6 @@ const normalizeNamespace = (value: string) =>
.replaceAll(/^-|-$/g, '')
.slice(0, 32);
-// Mirrors the namespace Form rules below — only run the live availability check
-// once the handle is well-formed, so we don't probe Market for invalid input.
-const NAMESPACE_PATTERN = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
-const isNamespaceFormatValid = (value: string) =>
- value.length >= 3 && value.length <= 32 && NAMESPACE_PATTERN.test(value);
-
-const URL_PATTERN = /^(?:(?:[a-z]+:)?\/\/|www\.)\S+$/i;
-
type NamespaceAvailability = 'available' | 'checking' | 'idle' | 'taken';
export const Content = memo<ContentProps>(({ user, onSuccess }) => {
@@ -249,7 +243,7 @@ export const Content = memo<ContentProps>(({ user, onSuccess }) => {
label={t('user.workspaceProfile.fields.websiteUrl')}
name="websiteUrl"
validate={(value?: string) =>
- value && !URL_PATTERN.test(value) ? t('user.workspaceProfile.errors.url') : undefined
+ value && !isWebsiteUrlValid(value) ? t('user.workspaceProfile.errors.url') : undefined
}
>
<Input
@@ -392,10 +386,8 @@ export const Content = memo<ContentProps>(({ user, onSuccess }) => {
required={t('user.workspaceProfile.errors.namespace.required')}
validate={(value?: string) => {
if (!value) return;
- if (value.length < 3 || value.length > 32)
- return t('user.workspaceProfile.errors.namespace.length');
- if (!NAMESPACE_PATTERN.test(value))
- return t('user.workspaceProfile.errors.namespace.pattern');
+ const error = getNamespaceError(value);
+ if (error) return t(`user.workspaceProfile.errors.namespace.${error}`);
}}
>
<Input
diff --git a/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.test.ts b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.test.ts
new file mode 100644
index 00000000000..60c57a49487
--- /dev/null
+++ b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from 'vitest';
+
+import { getNamespaceError, isNamespaceFormatValid, isWebsiteUrlValid } from './validation';
+
+describe('getNamespaceError', () => {
+ it('accepts a namespace padded with whitespace', () => {
+ expect(getNamespaceError(' acme-labs ')).toBeUndefined();
+ expect(isNamespaceFormatValid(' acme-labs ')).toBe(true);
+ });
+
+ it('measures length after trimming', () => {
+ expect(getNamespaceError(' ab ')).toBe('length');
+ });
+
+ it('still rejects inner whitespace and invalid characters', () => {
+ expect(getNamespaceError('acme labs')).toBe('pattern');
+ expect(getNamespaceError('Acme')).toBe('pattern');
+ });
+});
+
+describe('isWebsiteUrlValid', () => {
+ it('accepts a URL padded with whitespace', () => {
+ expect(isWebsiteUrlValid(' https://acme.dev ')).toBe(true);
+ });
+
+ it('rejects text that is not a URL', () => {
+ expect(isWebsiteUrlValid('acme dev')).toBe(false);
+ });
+});
diff --git a/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.ts b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.ts
new file mode 100644
index 00000000000..1af7ba8d413
--- /dev/null
+++ b/src/routes/(main)/community/(detail)/workspace/features/WorkspaceProfileModal/validation.ts
@@ -0,0 +1,15 @@
+const NAMESPACE_PATTERN = /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
+const URL_PATTERN = /^(?:(?:[a-z]+:)?\/\/|www\.)\S+$/i;
+
+// Inputs are validated trimmed because the availability check and the submitted
+// payload are trimmed; checking the raw value flags a pasted " acme" as malformed
+// while the status beside it reports it available.
+export const getNamespaceError = (value: string): 'length' | 'pattern' | undefined => {
+ const namespace = value.trim();
+ if (namespace.length < 3 || namespace.length > 32) return 'length';
+ if (!NAMESPACE_PATTERN.test(namespace)) return 'pattern';
+};
+
+export const isNamespaceFormatValid = (value: string) => getNamespaceError(value) === undefined;
+
+export const isWebsiteUrlValid = (value: string) => URL_PATTERN.test(value.trim());