From 8453cd2852fc76a1d2bdd1197d4f245856861560 Mon Sep 17 00:00:00 2001 From: Darshan Date: Sat, 8 Feb 2025 10:48:13 +0530 Subject: [PATCH 1/5] fix: allowing invalid hostnames on web platforms. --- src/lib/helpers/string.ts | 18 ++++++++++++++++++ .../platforms/wizard/flutter/step1.svelte | 12 +++++++++++- .../overview/platforms/wizard/web/step1.svelte | 8 +++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/lib/helpers/string.ts b/src/lib/helpers/string.ts index 2ef6ac191..ecf5c03aa 100644 --- a/src/lib/helpers/string.ts +++ b/src/lib/helpers/string.ts @@ -48,3 +48,21 @@ const formatter = Intl.NumberFormat('en', { export function formatNum(number: number): string { return formatter.format(number); } + +/** + * Validates a hostname, optionally allowing wildcards. + * + * @param {string} hostname - The hostname to validate. + * @param {boolean} [wildcard=true] - Whether to allow wildcard hostnames (e.g., `*.example.com`). + */ +export function isHostnameValid(hostname: string, wildcard: boolean = true): boolean { + if (!hostname) return false; + + if (!wildcard && hostname.startsWith('*.')) return false; + + const hostnameRegex = wildcard + ? /^(\*\.)?([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/ + : /^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/; + + return hostnameRegex.test(hostname); +} diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte index e9bb56165..207378bbb 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte @@ -8,6 +8,7 @@ import { Submit, trackEvent } from '$lib/actions/analytics'; import { PlatformType } from '@appwrite.io/console'; import { isValueOfStringEnum } from '$lib/helpers/types'; + import { isHostnameValid } from '$lib/helpers/string'; // enum Platform { // Android = 'flutter-android', @@ -84,6 +85,11 @@ }; async function beforeSubmit() { + // double-check the hostname value. + if (platform === PlatformType.Flutterweb && !isHostnameValid($createPlatform.hostname)) { + throw new Error('Please enter a valid hostname'); + } + if ($createPlatform.$id) { await sdk.forConsole.projects.deletePlatform(projectId, $createPlatform.$id); } @@ -115,7 +121,11 @@ }[platform]; - + {registee} registration
diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte index 661b2472d..6927b848c 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte @@ -8,11 +8,17 @@ import { createPlatform } from '../store'; import { Submit, trackEvent } from '$lib/actions/analytics'; import { PlatformType } from '@appwrite.io/console'; + import { isHostnameValid } from '$lib/helpers/string'; const projectId = $page.params.project; const suggestions = ['*.vercel.app', '*.netlify.app', '*.gitpod.io']; async function beforeSubmit() { + // double-check the hostname value. + if (!isHostnameValid($createPlatform.hostname)) { + throw new Error('Please enter a valid hostname'); + } + if ($createPlatform.$id) { await sdk.forConsole.projects.updatePlatform( projectId, @@ -43,7 +49,7 @@ } - + Hostname registration Date: Sat, 22 Feb 2025 11:31:55 +0530 Subject: [PATCH 2/5] update: logic to verify hostname while maintaining backwards compatibility. --- src/lib/layout/wizard.svelte | 8 +++++- src/lib/layout/wizardStep.svelte | 9 +++++-- src/lib/stores/wizard.ts | 2 +- .../platforms/wizard/flutter/step1.svelte | 25 +++++++++++++------ .../platforms/wizard/web/step1.svelte | 21 +++++++++++++--- 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/src/lib/layout/wizard.svelte b/src/lib/layout/wizard.svelte index 95f462c07..4a27583c5 100644 --- a/src/lib/layout/wizard.svelte +++ b/src/lib/layout/wizard.svelte @@ -68,10 +68,12 @@ } async function submit() { + let interceptorResponse = null; + if ($wizard.interceptor) { $wizard.nextDisabled = true; try { - await $wizard.interceptor(); + interceptorResponse = await $wizard.interceptor(); } catch (error) { if (!$wizard.interceptorNotificationEnabled) return; addNotification({ @@ -84,6 +86,10 @@ } } + // maintains backward compatibility. + // explicit boolean check as the return type can be undefined/void too! + if (typeof interceptorResponse === 'boolean' && interceptorResponse === false) return; + wizard.setInterceptor(null); if (isLastStep) { if ($wizard.finalAction) { diff --git a/src/lib/layout/wizardStep.svelte b/src/lib/layout/wizardStep.svelte index cddb57463..ffbd91945 100644 --- a/src/lib/layout/wizardStep.svelte +++ b/src/lib/layout/wizardStep.svelte @@ -4,9 +4,14 @@ import { onDestroy } from 'svelte'; /** - * Pass callback that is run before the Form submit, can be canceled with throwing an exception. + * Pass callback that is run before the Form submit, + * can be canceled by throwing an `Exception` or returning `false`. + * + * Example - + * 1. validate > throw Error > notification is shown + * 2. validate > return False > show your UI as no notification is shown. */ - export let beforeSubmit: () => Promise = null; + export let beforeSubmit: () => Promise | Promise = null; export let nextDisabled = false; $: wizard.setNextDisabled(nextDisabled); diff --git a/src/lib/stores/wizard.ts b/src/lib/stores/wizard.ts index 6a752584f..479bd4d33 100644 --- a/src/lib/stores/wizard.ts +++ b/src/lib/stores/wizard.ts @@ -8,7 +8,7 @@ export type WizardStore = { media?: string; component?: typeof SvelteComponent; cover?: typeof SvelteComponent; - interceptor?: () => Promise; + interceptor?: () => Promise | Promise; finalAction?: () => Promise; nextDisabled: boolean; step: number; diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte index 207378bbb..b8e87f999 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte @@ -1,7 +1,7 @@ - + {registee} registration
@@ -185,6 +190,10 @@ required bind:value={$createPlatform.hostname} /> + {#if error} + {error} + {/if} +
{#each suggestions as suggestion} { + error = null; + // double-check the hostname value. if (!isHostnameValid($createPlatform.hostname)) { - throw new Error('Please enter a valid hostname'); + error = 'Please enter a valid hostname'; + return false; } if ($createPlatform.$id) { @@ -47,9 +51,13 @@ $createPlatform.$id = platform.$id; } + + $: if (!$createPlatform.hostname && error) { + error = null; + } - + Hostname registration + + {#if error} + {error} + {/if} +
{#each suggestions as suggestion} Date: Sat, 22 Feb 2025 11:35:17 +0530 Subject: [PATCH 3/5] remove: type. --- .../overview/platforms/wizard/flutter/step1.svelte | 2 +- .../overview/platforms/wizard/web/step1.svelte | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte index b8e87f999..630201d50 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte @@ -86,7 +86,7 @@ [PlatformType.Flutterwindows]: 'Package Name' }; - async function beforeSubmit(): Promise { + async function beforeSubmit() { error = null; // double-check the hostname value. diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte index 1d91b3afb..d97430b32 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/web/step1.svelte @@ -14,7 +14,7 @@ const projectId = $page.params.project; const suggestions = ['*.vercel.app', '*.netlify.app', '*.gitpod.io']; - async function beforeSubmit(): Promise { + async function beforeSubmit() { error = null; // double-check the hostname value. From 73253518b97c6c53bd73b7012cd6ff1ba6054cd3 Mon Sep 17 00:00:00 2001 From: Darshan Date: Thu, 27 Feb 2025 19:55:54 +0530 Subject: [PATCH 4/5] improve: validation logic. --- src/lib/elements/forms/inputText.svelte | 8 +++++++ src/lib/helpers/string.ts | 17 ++----------- src/lib/layout/wizard.svelte | 8 +------ src/lib/layout/wizardStep.svelte | 9 ++----- src/lib/stores/wizard.ts | 2 +- .../platforms/wizard/flutter/step1.svelte | 24 ++++--------------- .../platforms/wizard/web/step1.svelte | 23 ++++-------------- 7 files changed, 22 insertions(+), 69 deletions(-) diff --git a/src/lib/elements/forms/inputText.svelte b/src/lib/elements/forms/inputText.svelte index 2c69fa8e4..8ca3ef587 100644 --- a/src/lib/elements/forms/inputText.svelte +++ b/src/lib/elements/forms/inputText.svelte @@ -11,6 +11,8 @@ export let id: string; export let name: string = id; export let value = ''; + export let pattern: string = null; + export let patternError: string = ''; export let placeholder = ''; export let required = false; export let hideRequired = false; @@ -45,6 +47,11 @@ return; } + if (patternError && element.validity.patternMismatch) { + error = patternError; + return; + } + error = element.validationMessage; }; @@ -118,6 +125,7 @@ {disabled} {readonly} {required} + {pattern} {maxlength} autocomplete={autocomplete ? 'on' : 'off'} type="text" diff --git a/src/lib/helpers/string.ts b/src/lib/helpers/string.ts index ecf5c03aa..e28a98f2d 100644 --- a/src/lib/helpers/string.ts +++ b/src/lib/helpers/string.ts @@ -50,19 +50,6 @@ export function formatNum(number: number): string { } /** - * Validates a hostname, optionally allowing wildcards. - * - * @param {string} hostname - The hostname to validate. - * @param {boolean} [wildcard=true] - Whether to allow wildcard hostnames (e.g., `*.example.com`). + * Returns a regex to check hostname validity. Supports wildcards too! */ -export function isHostnameValid(hostname: string, wildcard: boolean = true): boolean { - if (!hostname) return false; - - if (!wildcard && hostname.startsWith('*.')) return false; - - const hostnameRegex = wildcard - ? /^(\*\.)?([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/ - : /^([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,63}$/; - - return hostnameRegex.test(hostname); -} +export const hostnameRegex = String.raw`(\*)|(\*\.)?(?!-)[A-Za-z0-9\-]+([\-\.]{1}[a-z0-9]+)*\.[A-Za-z]{2,18}`; diff --git a/src/lib/layout/wizard.svelte b/src/lib/layout/wizard.svelte index 4a27583c5..95f462c07 100644 --- a/src/lib/layout/wizard.svelte +++ b/src/lib/layout/wizard.svelte @@ -68,12 +68,10 @@ } async function submit() { - let interceptorResponse = null; - if ($wizard.interceptor) { $wizard.nextDisabled = true; try { - interceptorResponse = await $wizard.interceptor(); + await $wizard.interceptor(); } catch (error) { if (!$wizard.interceptorNotificationEnabled) return; addNotification({ @@ -86,10 +84,6 @@ } } - // maintains backward compatibility. - // explicit boolean check as the return type can be undefined/void too! - if (typeof interceptorResponse === 'boolean' && interceptorResponse === false) return; - wizard.setInterceptor(null); if (isLastStep) { if ($wizard.finalAction) { diff --git a/src/lib/layout/wizardStep.svelte b/src/lib/layout/wizardStep.svelte index ffbd91945..cddb57463 100644 --- a/src/lib/layout/wizardStep.svelte +++ b/src/lib/layout/wizardStep.svelte @@ -4,14 +4,9 @@ import { onDestroy } from 'svelte'; /** - * Pass callback that is run before the Form submit, - * can be canceled by throwing an `Exception` or returning `false`. - * - * Example - - * 1. validate > throw Error > notification is shown - * 2. validate > return False > show your UI as no notification is shown. + * Pass callback that is run before the Form submit, can be canceled with throwing an exception. */ - export let beforeSubmit: () => Promise | Promise = null; + export let beforeSubmit: () => Promise = null; export let nextDisabled = false; $: wizard.setNextDisabled(nextDisabled); diff --git a/src/lib/stores/wizard.ts b/src/lib/stores/wizard.ts index 479bd4d33..6a752584f 100644 --- a/src/lib/stores/wizard.ts +++ b/src/lib/stores/wizard.ts @@ -8,7 +8,7 @@ export type WizardStore = { media?: string; component?: typeof SvelteComponent; cover?: typeof SvelteComponent; - interceptor?: () => Promise | Promise; + interceptor?: () => Promise; finalAction?: () => Promise; nextDisabled: boolean; step: number; diff --git a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte index 630201d50..d67c3f6d9 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/wizard/flutter/step1.svelte @@ -1,14 +1,14 @@ @@ -188,12 +174,10 @@ placeholder={placeholder[platform].hostname} tooltip={placeholder[platform].tooltip} required + pattern={hostnameRegex} + patternError="Please enter a valid hostname" bind:value={$createPlatform.hostname} /> - {#if error} - {error} - {/if} -
{#each suggestions as suggestion} @@ -73,12 +60,10 @@ placeholder="localhost" tooltip="The hostname that your website will use to interact with the Appwrite APIs in production or development environments. No protocol or port number required." required + pattern={hostnameRegex} + patternError="Please enter a valid hostname" bind:value={$createPlatform.hostname} /> - {#if error} - {error} - {/if} -
{#each suggestions as suggestion} Date: Thu, 27 Feb 2025 19:59:06 +0530 Subject: [PATCH 5/5] add: validation logic in update screen for web platforms. --- .../overview/platforms/[platform]/web.svelte | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/routes/(console)/project-[project]/overview/platforms/[platform]/web.svelte b/src/routes/(console)/project-[project]/overview/platforms/[platform]/web.svelte index 34aafe9b9..1d1f4520e 100644 --- a/src/routes/(console)/project-[project]/overview/platforms/[platform]/web.svelte +++ b/src/routes/(console)/project-[project]/overview/platforms/[platform]/web.svelte @@ -8,6 +8,7 @@ import { onMount } from 'svelte'; import { project } from '../../../store'; import { platform } from './store'; + import { hostnameRegex } from '$lib/helpers/string'; let hostname: string = null; @@ -28,7 +29,7 @@ await invalidate(Dependencies.PLATFORM); addNotification({ type: 'success', - message: 'Platform Hostname has been updated' + message: 'Platform hostname has been updated' }); } catch (error) { addNotification({ @@ -49,7 +50,8 @@ id="hostname" label="Hostname" bind:value={hostname} - required + pattern={hostnameRegex} + patternError="Please enter a valid hostname" placeholder="myapp.com" />