Skip to content

Commit f12cc45

Browse files
authored
Make regex matching more restrictive (keybase#10434)
* Make regex matching more restrictive * Add reddit subdomains
1 parent 94a0eed commit f12cc45

2 files changed

Lines changed: 42 additions & 27 deletions

File tree

‎browser/js/background.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function generateConditions(matchers) {
7979
// Generate pageMatchRules conditions
8080
const conditions = [];
8181
for (const m of matchers) {
82-
for (const hostName of m.hostEquals) {
82+
for (const hostName of getServiceHosts(m)) {
8383
const cond = {
8484
pageUrl: { originAndPathMatches: m.originAndPathMatches, hostEquals: hostName},
8585
};

‎browser/js/identities.js‎

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@ function parseLocationQuery(s) {
3232
// "getUsername": Function to parse the username from the browser
3333
// `location` object.
3434
//
35-
// "locationMatches": A regular expression used to match a page within the
35+
// "pathMatches": A regular expression used to match the pathname within the
3636
// service (i.e. news.ycombinator.com/user?id=username matches but not
3737
// news.ycombinator.com/newest).
3838
//
3939
// "originAndPathMatches": A re2 style regex used to match a page within
4040
// the service for declarativeContent matching
4141
// (https://developer.chrome.com/extensions/declarativeContent).
4242
//
43-
// "hostEquals": Used to match that the host is the host we want to run on
44-
// (preventing any regex trickery for `locationMatches` or
43+
// "subdomains": Subdomains that the host is considered valid on.
44+
//
45+
// "host": Used to match that the host is the host we want to run on
46+
// (preventing any regex trickery for `pathMatches` or
4547
// `originAndPathMatches`).
4648
//
4749
// "css": (optional) CSS selector which must be present for declarativeContent
@@ -53,65 +55,78 @@ const identityMatchers = [
5355
{
5456
service: "keybase",
5557
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
56-
locationMatches: new RegExp('\\.keybase\\.io/([\\w]+)[/]?'),
57-
originAndPathMatches: 'keybase\\.io/[\\w]+[/]?',
58-
hostEquals: ['keybase.io'],
58+
pathMatches: new RegExp('^([\\w]+)[/]?'),
59+
originAndPathMatches: '^https://keybase\\.io/[\\w]+[/]?',
60+
subdomains: [],
61+
host: 'keybase.io',
5962
css: ['.profile-heading']
6063
},
6164
{
6265
service: "reddit",
6366
getUsername: function(loc) { return loc.pathname.split('/')[2]; },
64-
locationMatches: new RegExp('\\.reddit\\.com/user/([\\w-]+)[/]?$'),
65-
originAndPathMatches: '\\.reddit\\.com/user/[\\w-]+[/]?$',
66-
hostEquals: ['www.reddit.com', 'reddit.com']
67+
pathMatches: new RegExp('^/user/([\\w-]+)[/]?$'),
68+
originAndPathMatches: '^https://[\\w.-]*?\\.reddit\\.com/user/[\\w-]+[/]?$',
69+
subdomains: ['np', 'ssl', 'blog', 'fr', 'pay', 'es', 'en-us', 'en', 'ru',
70+
'us', 'de', 'dd', 'no', 'pt', 'ww', 'ss', '4x', 'sv', 'nl', 'hw', 'hr',
71+
'www'],
72+
host: 'reddit.com'
6773
},
6874
{
6975
service: "twitter",
7076
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
71-
locationMatches: new RegExp('\\.twitter\\.com/([\\w]+)[/]?$'),
72-
originAndPathMatches: 'twitter\\.com/[\\w]+[/]?$',
73-
hostEquals: ['twitter.com'],
77+
pathMatches: new RegExp('^/([\\w]+)[/]?$'),
78+
originAndPathMatches: '^https://twitter\\.com/[\\w]+[/]?$',
79+
subdomains: [],
80+
host: 'twitter.com',
7481
css: ['body.ProfilePage']
7582
},
7683
{
7784
service: "github",
7885
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
79-
locationMatches: new RegExp('\\.github\\.com/([\\w\-]+)[/]?$'),
80-
originAndPathMatches: 'github\\.com/[\\w\-]+[/]?$',
81-
hostEquals: ['github.com'],
86+
pathMatches: new RegExp('^/([\\w\-]+)[/]?$'),
87+
originAndPathMatches: '^https://github\\.com/[\\w\-]+[/]?$',
88+
subdomains: [],
89+
host: 'github.com',
8290
css: ['body.page-profile']
8391
},
8492
{
8593
service: "facebook",
8694
getUsername: function(loc) { return loc.pathname.split('/')[1]; },
87-
locationMatches: new RegExp('\\.facebook\\.com/([\\w\\.]+)[/]?$'),
88-
originAndPathMatches: '\\.facebook\\.com/[\\w\\.]+[/]?$',
89-
hostEquals: ['facebook.com', 'www.facebook.com'],
95+
pathMatches: new RegExp('^/([\\w\\.]+)[/]?$'),
96+
originAndPathMatches: '^https://(www)?\\.facebook\\.com/[\\w\\.]+[/]?$',
97+
subdomains: ['www'],
98+
host: 'facebook.com',
9099
css: ['body.timelineLayout']
91100
},
92101
{
93102
service: "hackernews",
94103
getUsername: function(loc) { return parseLocationQuery(loc.search)["id"]; },
95-
locationMatches: new RegExp('news\\.ycombinator\\.com/user'),
96-
originAndPathMatches: 'news\\.ycombinator\\.com/user',
97-
hostEquals: ['news.ycombinator.com'],
104+
pathMatches: new RegExp('^/user'),
105+
originAndPathMatches: '^https://news\\.ycombinator\\.com/user',
106+
subdomains: [],
107+
host: 'news.ycombinator.com',
98108
css: ['html[op="user"]']
99109
}
100110
];
101111

112+
function getServiceHosts(service) {
113+
hosts = [service.host]
114+
for (const subdomain of service.subdomains) {
115+
hosts.push(subdomain + '.' + service.host)
116+
}
117+
return hosts
118+
}
119+
102120
// Match a window.location and document against a service profile and return
103121
// a User instance. Will skip matching CSS if no document is provided.
104122
function matchService(loc, doc, forceService) {
105-
// Prefix the url with a period if there is no subdomain.
106-
const hasSubdomain = loc.hostname.indexOf(".") !== loc.hostname.lastIndexOf(".");
107-
const url = (!hasSubdomain && ".") + loc.hostname + loc.pathname;
108123

109124
for (const m of identityMatchers) {
110125
if (forceService !== undefined && forceService !== m.service) continue;
111126

112-
const matched = m.hostEquals.some(function(hostName) {
127+
const matched = getServiceHosts(m).some(function(hostName) {
113128
return hostName === loc.hostname
114-
}) && url.match(m.locationMatches);
129+
}) && loc.pathname.match(m.pathMatches);
115130
if (!matched) continue;
116131

117132
const username = safeHTML(m.getUsername(loc));

0 commit comments

Comments
 (0)