Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: handle error in parseInternalLink (#7065)

01zulfi69504a150e8d
diff --git a/packages/core/src/utils/__tests__/internal-link.test.ts b/packages/core/src/utils/__tests__/internal-link.test.ts
new file mode 100644
index 000000000..adeceba89
--- /dev/null
+++ b/packages/core/src/utils/__tests__/internal-link.test.ts
@@ -0,0 +1,50 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { describe, expect, it } from "vitest";
+import { parseInternalLink } from "../internal-link";
+
+describe("parseInternalLink", () => {
+ const invalidInternalLinks = [
+ "",
+ "invalid-url",
+ "http://google.com",
+ "https://google.com"
+ ];
+ invalidInternalLinks.forEach((url) => {
+ it(`should return undefined when not internal link: ${url}`, () => {
+ expect(parseInternalLink(url)).toBeUndefined();
+ });
+ });
+ const validInternalLinks = [
+ {
+ url: "nn://note/123",
+ expected: { type: "note", id: "123", params: {} }
+ },
+ {
+ url: "nn://note/123?blockId=456",
+ expected: { type: "note", id: "123", params: { blockId: "456" } }
+ }
+ ];
+ validInternalLinks.forEach(({ url, expected }) => {
+ it(`should parse internal link: ${url}`, () => {
+ expect(parseInternalLink(url)).toEqual(expected);
+ });
+ });
+});
diff --git a/packages/core/src/utils/internal-link.ts b/packages/core/src/utils/internal-link.ts
index 80ee1edc4..fc00bb73a 100644
--- a/packages/core/src/utils/internal-link.ts
+++ b/packages/core/src/utils/internal-link.ts
@@ -52,7 +52,12 @@ export function createInternalLink<T extends InternalLinkType>(
}
export function parseInternalLink(link: string): InternalLink | undefined {
- const url = new URL(link);
+ let url;
+ try {
+ url = new URL(link);
+ } catch (e) {
+ return;
+ }
if (url.protocol !== "nn:") return;
const [type, id] = url.href.split("?")[0].split("/").slice(2);