From e338370219a5585d7bf51624c52acb2f450019a2 Mon Sep 17 00:00:00 2001 From: William Harrison <87287585+wdhdev@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:26:28 +0800 Subject: [PATCH 1/2] optimisations + test mailto redirects --- domains/william.json | 1 + tests/records.test.js | 35 ++++++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/domains/william.json b/domains/william.json index fcec296e5..6950dc060 100644 --- a/domains/william.json +++ b/domains/william.json @@ -8,6 +8,7 @@ }, "redirect_config": { "custom_paths": { + "/email": "mailto:will@hrsn.dev", "/github": "https://github.com/wdhdev" }, "redirect_paths": true diff --git a/tests/records.test.js b/tests/records.test.js index 69a42feb0..13aa8e1ec 100644 --- a/tests/records.test.js +++ b/tests/records.test.js @@ -89,6 +89,8 @@ function isValidHexadecimal(value) { } function validateRecordValues(t, data, file) { + const subdomain = file.replace(/\.json$/, ""); + Object.entries(data.record).forEach(([key, value]) => { // General validation for arrays if (["A", "AAAA", "MX", "NS"].includes(key)) { @@ -126,6 +128,11 @@ function validateRecordValues(t, data, file) { `${file}: Record value for ${key} must start with http:// or https://` ); t.notThrows(() => new URL(value), `${file}: Invalid URL for ${key}`); + + const urlHost = new URL(value).host; + const isSelfReferencing = + file === "@.json" ? urlHost === "is-a.dev" : urlHost === `${subdomain}.is-a.dev`; + t.true(!isSelfReferencing, `${file}: URL cannot point to itself`); } } @@ -162,27 +169,33 @@ function validateRecordValues(t, data, file) { customPaths.forEach((customPath, idx) => { const customRedirectURL = data.redirect_config.custom_paths[customPath]; + const urlMessage = `${file}: Custom path in redirect_config`; + // Validate the custom path t.true( pathRegex.test(customPath), - `${file}: Custom path in redirect_config must start with a slash, contain only alphanumeric characters, hyphens, underscores, periods, and slashes, and cannot end with a slash at index ${idx}` + `${urlMessage} must start with a slash, contain only alphanumeric characters, hyphens, underscores, periods, and slashes, and cannot end with a slash at index ${idx}` ); t.true( customPath.length >= 2 && customPath.length <= 255, - `${file}: Custom path in redirect_config should be 2-255 characters long at index ${idx}` + `${urlMessage} should be 2-255 characters long at index ${idx}` ); + + // Validate the redirect URL t.true( data.record.URL !== customRedirectURL, - `${file}: Custom path in redirect_config should be different from the URL record at index ${idx}` - ); - t.true( - customRedirectURL.startsWith("http://") || customRedirectURL.startsWith("https://"), - `${file}: Custom path in redirect_config must start with http:// or https:// at index ${idx}` - ); - t.notThrows( - () => new URL(customRedirectURL), - `${file}: Invalid URL for custom path in redirect_config at index ${idx}` + `${urlMessage} should be different from the URL record at index ${idx}` ); + // t.true( + // customRedirectURL.startsWith("http://") || customRedirectURL.startsWith("https://"), + // `${urlMessage} must start with http:// or https:// at index ${idx}` + // ); + t.notThrows(() => new URL(customRedirectURL), `${urlMessage} contains an invalid URL at index ${idx}`); + + // Check for self-referencing redirects + const urlHost = new URL(customRedirectURL).host; + const isSelfReferencing = file === "@.json" ? urlHost === "is-a.dev" : urlHost === `${subdomain}.is-a.dev`; + t.true(!isSelfReferencing, `${urlMessage} cannot point to itself at index ${idx}`); }); } } From 961a85dd0877432f5ce5fe90da40d0a18852682e Mon Sep 17 00:00:00 2001 From: William Harrison <87287585+wdhdev@users.noreply.github.com> Date: Mon, 13 Jan 2025 13:29:46 +0800 Subject: [PATCH 2/2] remove mailto test --- domains/william.json | 1 - tests/records.test.js | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/domains/william.json b/domains/william.json index 6950dc060..fcec296e5 100644 --- a/domains/william.json +++ b/domains/william.json @@ -8,7 +8,6 @@ }, "redirect_config": { "custom_paths": { - "/email": "mailto:will@hrsn.dev", "/github": "https://github.com/wdhdev" }, "redirect_paths": true diff --git a/tests/records.test.js b/tests/records.test.js index 13aa8e1ec..4d03a0e75 100644 --- a/tests/records.test.js +++ b/tests/records.test.js @@ -186,10 +186,10 @@ function validateRecordValues(t, data, file) { data.record.URL !== customRedirectURL, `${urlMessage} should be different from the URL record at index ${idx}` ); - // t.true( - // customRedirectURL.startsWith("http://") || customRedirectURL.startsWith("https://"), - // `${urlMessage} must start with http:// or https:// at index ${idx}` - // ); + t.true( + customRedirectURL.startsWith("http://") || customRedirectURL.startsWith("https://"), + `${urlMessage} must start with http:// or https:// at index ${idx}` + ); t.notThrows(() => new URL(customRedirectURL), `${urlMessage} contains an invalid URL at index ${idx}`); // Check for self-referencing redirects