From 32b7a7df00c267d9bf3e38e51da1104a1995238f Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 8 Jul 2022 21:15:07 +0200 Subject: [PATCH] fix: Handle sanitizeUrl can receive non-string value closes #3746 --- app/editor/components/LinkEditor.tsx | 2 +- shared/utils/urls.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/editor/components/LinkEditor.tsx b/app/editor/components/LinkEditor.tsx index 63711142e..02e497e60 100644 --- a/app/editor/components/LinkEditor.tsx +++ b/app/editor/components/LinkEditor.tsx @@ -113,7 +113,7 @@ class LinkEditor extends React.Component { this.discardInputValue = true; const { from, to } = this.props; - href = sanitizeHref(href); + href = sanitizeHref(href) ?? ""; this.props.onSelectLink({ href, title, from, to }); }; diff --git a/shared/utils/urls.ts b/shared/utils/urls.ts index 2143b216c..640991116 100644 --- a/shared/utils/urls.ts +++ b/shared/utils/urls.ts @@ -77,7 +77,11 @@ export function isExternalUrl(url: string) { * @param href The href to sanitize * @returns The sanitized href */ -export function sanitizeHref(href: string) { +export function sanitizeHref(href: string | null | undefined) { + if (!href) { + return undefined; + } + if ( !isUrl(href) && !href.startsWith("/") &&