Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
robots-parser
Advanced tools
A specification compliant robots.txt parser with wildcard (*) matching support.
The robots-parser npm package is a tool for parsing robots.txt files, which are used to manage and control the behavior of web crawlers. This package allows you to easily interpret the rules defined in a robots.txt file and determine whether a specific user-agent is allowed to access a particular URL.
Parse robots.txt
This feature allows you to parse a robots.txt file and check if a specific URL is allowed or disallowed for a given user-agent. In the example, the parser checks if 'Googlebot' is allowed to access '/private/' and '/public/' URLs.
const robotsParser = require('robots-parser');
const robotsTxt = `
User-agent: *
Disallow: /private/
`;
const parser = robotsParser('http://example.com/robots.txt', robotsTxt);
console.log(parser.isAllowed('http://example.com/private/', 'Googlebot')); // false
console.log(parser.isAllowed('http://example.com/public/', 'Googlebot')); // true
Check crawl delay
This feature allows you to retrieve the crawl delay specified for a particular user-agent. In the example, the parser retrieves the crawl delay for 'Googlebot', which is set to 10 seconds.
const robotsTxt = `
User-agent: Googlebot
Crawl-delay: 10
`;
const parser = robotsParser('http://example.com/robots.txt', robotsTxt);
console.log(parser.getCrawlDelay('Googlebot')); // 10
Get sitemap URLs
This feature allows you to extract sitemap URLs from a robots.txt file. In the example, the parser retrieves two sitemap URLs specified in the robots.txt file.
const robotsTxt = `
Sitemap: http://example.com/sitemap.xml
Sitemap: http://example.com/sitemap2.xml
`;
const parser = robotsParser('http://example.com/robots.txt', robotsTxt);
console.log(parser.getSitemaps()); // ['http://example.com/sitemap.xml', 'http://example.com/sitemap2.xml']
The robots-txt-parser package is another tool for parsing robots.txt files. It provides similar functionality to robots-parser, allowing you to check if a URL is allowed for a specific user-agent, retrieve crawl delays, and extract sitemap URLs. However, robots-txt-parser has a slightly different API and may offer additional features such as asynchronous parsing.
The robotstxt package is a robust library for parsing and interpreting robots.txt files. It offers comprehensive support for all the directives in a robots.txt file and provides a user-friendly API. Compared to robots-parser, robotstxt may offer more advanced features and better performance for large-scale applications.
A robots.txt parser which aims to be complaint with the draft specification.
The parser currently supports:
Via NPM:
npm install robots-parser
or via Yarn:
yarn add robots-parser
var robotsParser = require('robots-parser');
var robots = robotsParser('http://www.example.com/robots.txt', [
'User-agent: *',
'Disallow: /dir/',
'Disallow: /test.html',
'Allow: /dir/test.html',
'Allow: /test.html',
'Crawl-delay: 1',
'Sitemap: http://example.com/sitemap.xml',
'Host: example.com'
].join('\n'));
robots.isAllowed('http://www.example.com/test.html', 'Sams-Bot/1.0'); // true
robots.isAllowed('http://www.example.com/dir/test.html', 'Sams-Bot/1.0'); // true
robots.isDisallowed('http://www.example.com/dir/test2.html', 'Sams-Bot/1.0'); // true
robots.getCrawlDelay('Sams-Bot/1.0'); // 1
robots.getSitemaps(); // ['http://example.com/sitemap.xml']
robots.getPreferredHost(); // example.com
boolean or undefined
Returns true if crawling the specified URL is allowed for the specified user-agent.
This will return undefined
if the URL isn't valid for this robots.txt.
boolean or undefined
Returns true if crawling the specified URL is not allowed for the specified user-agent.
This will return undefined
if the URL isn't valid for this robots.txt.
number or undefined
Returns the line number of the matching directive for the specified URL and user-agent if any.
Line numbers start at 1 and go up (1-based indexing).
Returns -1 if there is no matching directive. If a rule is manually added without a lineNumber then this will return undefined for that rule.
number or undefined
Returns the number of seconds the specified user-agent should wait between requests.
Returns undefined if no crawl delay has been specified for this user-agent.
array
Returns an array of sitemap URLs specified by the sitemap:
directive.
string or null
Returns the preferred host name specified by the host:
directive or null if there isn't one.
Fixed bug with https:
URLs defaulting to port 80
instead of 443
if no port is specified.
Thanks to @dskvr for reporting
This affects comparing URLs with the default HTTPs port to URLs without it.
For example, comparing https://example.com/
to https://example.com:443/
or vice versa.
They should be treated as equivalent but weren't due to the incorrect port
being used for https:
.
Fixed bug where if the user-agent passed to isAllowed()
/ isDisallowed()
is called "constructor" it would throw an error.
Added support for relative URLs. This does not affect the default behavior so can safely be upgraded.
Relative matching is only allowed if both the robots.txt URL and the URLs being checked are relative.
For example:
var robots = robotsParser('/robots.txt', [
'User-agent: *',
'Disallow: /dir/',
'Disallow: /test.html',
'Allow: /dir/test.html',
'Allow: /test.html'
].join('\n'));
robots.isAllowed('/test.html', 'Sams-Bot/1.0'); // false
robots.isAllowed('/dir/test.html', 'Sams-Bot/1.0'); // true
robots.isDisallowed('/dir/test2.html', 'Sams-Bot/1.0'); // true
getMatchingLineNumber()
methodThis release is not 100% backwards compatible as it now uses the new URL APIs which are not supported in Node < 7.
The MIT License (MIT)
Copyright (c) 2014 Sam Clarke
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
A specification compliant robots.txt parser with wildcard (*) matching support.
The npm package robots-parser receives a total of 1,046,080 weekly downloads. As such, robots-parser popularity was classified as popular.
We found that robots-parser demonstrated a not healthy version release cadence and project activity because the last version was released 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.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.