Skip to content

Commit

Permalink
fix: domain match routine
Browse files Browse the repository at this point in the history
The domain match routine can fail in cases where the domain suffix characters can be located in the string before the suffix.

Changing the logic to use `lastIndexOf` instead of `indexOf` seems more appropriate for testing the suffix here.
  • Loading branch information
colincasey committed Apr 19, 2022
1 parent 30246e6 commit 35b7a13
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/cookie.js
Expand Up @@ -368,7 +368,7 @@ function domainMatch(str, domStr, canonicalize) {
/* " o All of the following [three] conditions hold:" */

/* "* The domain string is a suffix of the string" */
const idx = str.indexOf(domStr);
const idx = str.lastIndexOf(domStr);
if (idx <= 0) {
return false; // it's a non-match (-1) or prefix (0)
}
Expand Down
4 changes: 3 additions & 1 deletion test/domain_and_path_test.js
Expand Up @@ -33,7 +33,6 @@
const vows = require("vows");
const assert = require("assert");
const tough = require("../lib/cookie");
const Cookie = tough.Cookie;

function matchVows(func, table) {
const theVows = {};
Expand Down Expand Up @@ -105,6 +104,9 @@ vows
["www.aaaa.com", "aaa.com", false],
["www.aaa.com", "aaa.com", true],
["www.aexample.com", "example.com", false], // has to match on "." boundary
["computer.com", "com", true], // suffix string found at start of domain
["becoming.com", "com", true], // suffix string found in middle of domain
["sitcom.com", "com", true], // suffix string found just before the '.' boundary

// S5.1.3 "The string is a host name (i.e., not an IP address)"
["192.168.0.1", "168.0.1", false], // because str is an IP (v4)
Expand Down

0 comments on commit 35b7a13

Please sign in to comment.