Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
The psl npm package is a domain parsing library that allows you to work with Public Suffix List (PSL). The Public Suffix List is a set of Internet domain name suffixes under which Internet users can directly register names. The psl package provides functionality to parse domain names, extract top-level domains (TLDs), check domain validity, and more.
Parsing domain names
This feature allows you to parse domain names and extract various parts such as the domain, subdomain, and top-level domain (TLD).
const psl = require('psl');
let parsed = psl.parse('www.example.co.uk');
console.log(parsed.domain); // 'example.co.uk'
console.log(parsed.subdomain); // 'www'
console.log(parsed.tld); // 'co.uk'
Checking if a domain is valid
This feature enables you to check if a given domain name is valid according to the rules of the Public Suffix List.
const psl = require('psl');
let isValid = psl.isValid('example.com');
console.log(isValid); // true
Getting domain from a URL
This feature is used to extract the domain from a full URL, which can be useful for applications that need to process or analyze web addresses.
const psl = require('psl');
let domain = psl.get('http://www.example.co.uk');
console.log(domain); // 'example.co.uk'
tldjs is a robust package for working with top-level domains (TLDs). It can accurately parse URLs, extract TLDs, and handle internationalized domain names (IDNs). Compared to psl, tldjs offers a more object-oriented approach and additional methods for handling URLs and cookies.
publicsuffixlist is another npm package that provides an API for the Public Suffix List. It is designed to be a lightweight and fast library for domain parsing. While psl is more widely used, publicsuffixlist focuses on performance and a minimal memory footprint.
parse-domain is a package that offers similar functionality to psl, with the ability to parse domain names and extract parts like the TLD. It differs from psl in its API design and additional options for customization, such as custom TLDs and private domains.
psl
is a JavaScript
domain name parser based on the
Public Suffix List.
This implementation is tested against the test data hosted by Mozilla and kindly provided by Comodo.
Cross browser testing provided by
The Public Suffix List is a cross-vendor initiative to provide an accurate list of domain name suffixes.
The Public Suffix List is an initiative of the Mozilla Project, but is maintained as a community resource. It is available for use in any software, but was originally created to meet the needs of browser manufacturers.
A "public suffix" is one under which Internet users can directly register names. Some examples of public suffixes are ".com", ".co.uk" and "pvt.k12.wy.us". The Public Suffix List is a list of all known public suffixes.
Source: http://publicsuffix.org
This module is available both for Node.js and the browser. See below for more details.
npm install psl
From version v1.11.0
you can now import psl
as ESM.
import psl from 'psl';
If your project still uses CommonJS on Node.js v12 or later (with support for conditional exports), you can continue importing the module like in previous versions.
const psl = require('psl');
⚠️ If you are using Node.js v10 or older (😰), you can still use the latest version of this module, but you will need to import the bundled UMD.
var psl = require('psl/dist/psl.umd.cjs');
If you are using a bundler to build your app, you should be able to import
and/or require
the module just like in Node.js.
In modern browsers you can also import the ESM directly from a CDN
. For
example:
import psl from 'https://unpkg.com/psl@latest/dist/psl.mjs';
Finally, you can still download dist/psl.umd.cjs
and include it in a script tag.
<script src="psl.umd.cjs"></script>
This script is bundled and wrapped in a umd wrapper so you should be able to use it standalone or together with a module loader.
The script is also available on most popular CDNs. For example:
psl.parse(domain)
Parse domain based on Public Suffix List. Returns an Object
with the following
properties:
tld
: Top level domain (this is the public suffix).sld
: Second level domain (the first private part of the domain name).domain
: The domain name is the sld
+ tld
.subdomain
: Optional parts left of the domain.Parse domain without subdomain:
import psl from 'psl';
const parsed = psl.parse('google.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'google'
console.log(parsed.domain); // 'google.com'
console.log(parsed.subdomain); // null
Parse domain with subdomain:
import psl from 'psl';
const parsed = psl.parse('www.google.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'google'
console.log(parsed.domain); // 'google.com'
console.log(parsed.subdomain); // 'www'
Parse domain with nested subdomains:
import psl from 'psl';
const parsed = psl.parse('a.b.c.d.foo.com');
console.log(parsed.tld); // 'com'
console.log(parsed.sld); // 'foo'
console.log(parsed.domain); // 'foo.com'
console.log(parsed.subdomain); // 'a.b.c.d'
psl.get(domain)
Get domain name, sld
+ tld
. Returns null
if not valid.
import psl from 'psl';
// null input.
psl.get(null); // null
// Mixed case.
psl.get('COM'); // null
psl.get('example.COM'); // 'example.com'
psl.get('WwW.example.COM'); // 'example.com'
// Unlisted TLD.
psl.get('example'); // null
psl.get('example.example'); // 'example.example'
psl.get('b.example.example'); // 'example.example'
psl.get('a.b.example.example'); // 'example.example'
// TLD with only 1 rule.
psl.get('biz'); // null
psl.get('domain.biz'); // 'domain.biz'
psl.get('b.domain.biz'); // 'domain.biz'
psl.get('a.b.domain.biz'); // 'domain.biz'
// TLD with some 2-level rules.
psl.get('uk.com'); // null);
psl.get('example.uk.com'); // 'example.uk.com');
psl.get('b.example.uk.com'); // 'example.uk.com');
// More complex TLD.
psl.get('c.kobe.jp'); // null
psl.get('b.c.kobe.jp'); // 'b.c.kobe.jp'
psl.get('a.b.c.kobe.jp'); // 'b.c.kobe.jp'
psl.get('city.kobe.jp'); // 'city.kobe.jp'
psl.get('www.city.kobe.jp'); // 'city.kobe.jp'
// IDN labels.
psl.get('食狮.com.cn'); // '食狮.com.cn'
psl.get('食狮.公司.cn'); // '食狮.公司.cn'
psl.get('www.食狮.公司.cn'); // '食狮.公司.cn'
// Same as above, but punycoded.
psl.get('xn--85x722f.com.cn'); // 'xn--85x722f.com.cn'
psl.get('xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
psl.get('www.xn--85x722f.xn--55qx5d.cn'); // 'xn--85x722f.xn--55qx5d.cn'
psl.isValid(domain)
Check whether a domain has a valid Public Suffix. Returns a Boolean
indicating
whether the domain has a valid Public Suffix.
import psl from 'psl';
psl.isValid('google.com'); // true
psl.isValid('www.google.com'); // true
psl.isValid('x.yz'); // false
There are tests both for Node.js and the browser (using Playwright and BrowserStack).
# Run tests in node.
npm test
# Run tests in browserstack.
npm run test:browserstack
# Update rules from publicsuffix.org
npm run update-rules
# Build ESM, CJS and UMD and create dist files
npm run build
Feel free to fork if you see possible improvements!
The MIT License (MIT)
Copyright (c) 2014-2024 Lupo Montero lupomontero@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Domain name parser based on the Public Suffix List
The npm package psl receives a total of 29,887,876 weekly downloads. As such, psl popularity was classified as popular.
We found that psl demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.