From 1974d8f58b460953f9c6577f28963dc4985daa67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Acid=20Chicken=20=28=E7=A1=AB=E9=85=B8=E9=B6=8F=29?= Date: Wed, 6 Feb 2019 13:37:20 +0900 Subject: [PATCH] Add URL validation (#4148) --- src/config/load.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/config/load.ts b/src/config/load.ts index fc3e69919..5bb01f3d4 100644 --- a/src/config/load.ts +++ b/src/config/load.ts @@ -47,14 +47,21 @@ export default function load() { return Object.assign(config, mixin); } -function validateUrl(url: string) { +function tryCreateUrl(url: string) { try { return new URL(url); } catch (e) { - throw `url="${url}" is not a valid URL`; + throw `url="${url}" is not a valid URL.`; } } +function validateUrl(url: string) { + const result = tryCreateUrl(url); + if (result.pathname.replace('/', '').length) throw `url="${url}" is not a valid URL, has a pathname.`; + if (!url.includes(result.host)) throw `url="${url}" is not a valid URL, has an invalid hostname.`; + return result; +} + function normalizeUrl(url: string) { return url.endsWith('/') ? url.substr(0, url.length - 1) : url; }