Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make isExplicitlyDisallowed() return undefined if invalid URL and add caution #39

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,19 @@ This will return `undefined` if the URL isn't valid for this robots.txt.

**boolean or undefined**

> [!CAUTION]
> This is not part of the robots.txt specification and should only be used with
> the websites owners permission.
> This method is only intended for special purposes where a user-agent shouldn't
> fallback to matching against global (*) rules.
>
> An example of this behaviour is [Google AdsBot](https://developers.google.com/search/docs/crawling-indexing/google-special-case-crawlers)
> which must be explicitly excluded. This is done with the website owners permission.

Returns trues if explicitly disallowed for the specified user agent (User Agent wildcards are discarded).

This will return undefined if the URL is not valid for this robots.txt file.

### getMatchingLineNumber(url, [ua])

**number or undefined**
Expand Down
16 changes: 9 additions & 7 deletions Robots.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,9 @@ Robots.prototype._getRule = function (url, ua, explicit) {

var rules = this._rules[userAgent];
if (!explicit) {
rules = rules || this._rules['*']
rules = rules || this._rules['*'];
}
rules = rules || []
rules = rules || [];

var path = urlEncodeToUpper(parsedUrl.pathname + parsedUrl.search);
var rule = findRule(path, rules);
Expand Down Expand Up @@ -438,21 +438,23 @@ Robots.prototype.isDisallowed = function (url, ua) {
};

/**
* Returns trues if explicitly disallowed
* Returns trues if explicitly disallowed
* for the specified user agent (User Agent wildcards are discarded).
*
*
* This will return undefined if the URL is not valid for this robots.txt file.
*
* @param {string} url
* @param {string} ua
* @return {boolean?}
*/
Robots.prototype.isExplicitlyDisallowed = function(url, ua) {
Robots.prototype.isExplicitlyDisallowed = function (url, ua) {
var rule = this._getRule(url, ua, true);
if (typeof rule === 'undefined') {
return true;
return;
}

return !(!rule || rule.allow);
}
};

/**
* Gets the crawl delay if there is one.
Expand Down
17 changes: 15 additions & 2 deletions test/Robots.js
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ describe('Robots', function () {
var robots = robotsParser(url, contents);

expect(robots.isExplicitlyDisallowed(url, userAgent)).to.equal(false)
})
});

it('should be disallowed when user agent equal robots rule in explicit mode', function () {
var contents = [
Expand All @@ -886,5 +886,18 @@ describe('Robots', function () {
var robots = robotsParser(url, contents);

expect(robots.isExplicitlyDisallowed(url, userAgent)).to.equal(true)
})
});

it('should return undefined when given an invalid URL in explicit mode', function () {
var contents = [
'User-agent: SomeBot',
'Disallow: /',
].join('\n')

var url = 'https://www.example.com/hello'
var userAgent = 'SomeBot';
var robots = robotsParser('http://example.com', contents);

expect(robots.isExplicitlyDisallowed(url, userAgent)).to.equal(undefined)
});
});
Loading