domain-block.js (1655B)
1 /* 2 GoToSocial 3 Copyright (C) GoToSocial Authors admin@gotosocial.org 4 SPDX-License-Identifier: AGPL-3.0-or-later 5 6 This program is free software: you can redistribute it and/or modify 7 it under the terms of the GNU Affero General Public License as published by 8 the Free Software Foundation, either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU Affero General Public License for more details. 15 16 You should have received a copy of the GNU Affero General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 "use strict"; 21 22 const isValidDomain = require("is-valid-domain"); 23 const psl = require("psl"); 24 25 function isValidDomainBlock(domain) { 26 return isValidDomain(domain, { 27 /* 28 Wildcard prefix *. can be stripped since it's equivalent to not having it, 29 but wildcard anywhere else in the domain is not handled by the backend so it's invalid. 30 */ 31 wildcard: false, 32 allowUnicode: true 33 }); 34 } 35 36 /* 37 Still can't think of a better function name for this, 38 but we're checking a domain against the Public Suffix List <https://publicsuffix.org/> 39 to see if we should suggest removing subdomain(s) since they're likely owned/ran by the same party 40 social.example.com -> suggests example.com 41 */ 42 function hasBetterScope(domain) { 43 const lookup = psl.get(domain); 44 if (lookup && lookup != domain) { 45 return lookup; 46 } else { 47 return false; 48 } 49 } 50 51 module.exports = { isValidDomainBlock, hasBetterScope };