Skip to content

Commit

Permalink
Add option to filter by HTTP method in static network filters
Browse files Browse the repository at this point in the history
Related issue:
- uBlockOrigin/uBlock-issues#2117

Option: `method=`
Value: a list of `|`-separated lowercased method names. Negated
method names are allowed. These are valid methods:

- connect
- delete
- get
- head
- options
- patch
- post
- put

As per DNR's own documentation:
- https://developer.chrome.com/docs/extensions/reference/declarativeNetRequest/#type-RequestMethod

The logger shows the method used for every network request. It's
possible to filter the logger output for most-common methods: `get`,
`head`, `post`.
  • Loading branch information
gorhill committed Dec 22, 2022
1 parent e5a9b06 commit b698187
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 89 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Expand Up @@ -5,6 +5,7 @@
"esversion": 8,
"globals": {
"chrome": false, // global variable in Chromium, Chrome, Opera
"globalThis": false,
"self": false,
"vAPI": false,
"URLSearchParams": false,
Expand Down
44 changes: 26 additions & 18 deletions src/js/background.js
Expand Up @@ -176,8 +176,8 @@ const µBlock = { // jshint ignore:line

// Read-only
systemSettings: {
compiledMagic: 50, // Increase when compiled format changes
selfieMagic: 50, // Increase when selfie format changes
compiledMagic: 51, // Increase when compiled format changes
selfieMagic: 51, // Increase when selfie format changes
},

// https://github.com/uBlockOrigin/uBlock-issues/issues/759#issuecomment-546654501
Expand Down Expand Up @@ -285,6 +285,7 @@ const µBlock = { // jshint ignore:line
this.fromTabId(tabId); // Must be called AFTER tab context management
this.realm = '';
this.id = details.requestId;
this.setMethod(details.method);
this.setURL(details.url);
this.aliasURL = details.aliasURL || undefined;
if ( this.itype !== this.SUB_FRAME ) {
Expand Down Expand Up @@ -337,24 +338,31 @@ const µBlock = { // jshint ignore:line
}

toLogger() {
this.tstamp = Date.now();
if ( this.domain === undefined ) {
void this.getDomain();
}
if ( this.docDomain === undefined ) {
void this.getDocDomain();
}
if ( this.tabDomain === undefined ) {
void this.getTabDomain();
}
const filters = this.filter;
const details = {
id: this.id,
tstamp: Date.now(),
realm: this.realm,
method: this.getMethodName(),
type: this.stype,
tabId: this.tabId,
tabDomain: this.getTabDomain(),
tabHostname: this.getTabHostname(),
docDomain: this.getDocDomain(),
docHostname: this.getDocHostname(),
domain: this.getDomain(),
hostname: this.getHostname(),
url: this.url,
aliasURL: this.aliasURL,
filter: undefined,
};
// Many filters may have been applied to the current context
if ( Array.isArray(filters) === false ) {
return logger.writeOne(this);
if ( Array.isArray(this.filter) === false ) {
details.filter = this.filter;
return logger.writeOne(details);
}
for ( const filter of filters ) {
this.filter = filter;
logger.writeOne(this);
for ( const filter of this.filter ) {
details.filter = filter;
logger.writeOne(details);
}
}
};
Expand Down
73 changes: 72 additions & 1 deletion src/js/filtering-context.js
Expand Up @@ -84,6 +84,48 @@ const typeStrToIntMap = {
'other': OTHER,
};

const METHOD_NONE = 0;
const METHOD_CONNECT = 1 << 1;
const METHOD_DELETE = 1 << 2;
const METHOD_GET = 1 << 3;
const METHOD_HEAD = 1 << 4;
const METHOD_OPTIONS = 1 << 5;
const METHOD_PATCH = 1 << 6;
const METHOD_POST = 1 << 7;
const METHOD_PUT = 1 << 8;

const methodStrToBitMap = {
'': METHOD_NONE,
'connect': METHOD_CONNECT,
'delete': METHOD_DELETE,
'get': METHOD_GET,
'head': METHOD_HEAD,
'options': METHOD_OPTIONS,
'patch': METHOD_PATCH,
'post': METHOD_POST,
'put': METHOD_PUT,
'CONNECT': METHOD_CONNECT,
'DELETE': METHOD_DELETE,
'GET': METHOD_GET,
'HEAD': METHOD_HEAD,
'OPTIONS': METHOD_OPTIONS,
'PATCH': METHOD_PATCH,
'POST': METHOD_POST,
'PUT': METHOD_PUT,
};

const methodBitToStrMap = new Map([
[ METHOD_NONE, '' ],
[ METHOD_CONNECT, 'connect' ],
[ METHOD_DELETE, 'delete' ],
[ METHOD_GET, 'get' ],
[ METHOD_HEAD, 'head' ],
[ METHOD_OPTIONS, 'options' ],
[ METHOD_PATCH, 'patch' ],
[ METHOD_POST, 'post' ],
[ METHOD_PUT, 'put' ],
]);

/******************************************************************************/

const FilteringContext = class {
Expand All @@ -94,7 +136,8 @@ const FilteringContext = class {
this.tstamp = 0;
this.realm = '';
this.id = undefined;
this.itype = 0;
this.method = 0;
this.itype = NO_TYPE;
this.stype = undefined;
this.url = undefined;
this.aliasURL = undefined;
Expand Down Expand Up @@ -133,6 +176,7 @@ const FilteringContext = class {
fromFilteringContext(other) {
this.realm = other.realm;
this.type = other.type;
this.method = other.method;
this.url = other.url;
this.hostname = other.hostname;
this.domain = other.domain;
Expand Down Expand Up @@ -358,6 +402,23 @@ const FilteringContext = class {
}
return this;
}

setMethod(a) {
this.method = methodStrToBitMap[a] || 0;
return this;
}

getMethodName() {
return FilteringContext.getMethodName(this.method);
}

static getMethod(a) {
return methodStrToBitMap[a] || 0;
}

static getMethodName(a) {
return methodBitToStrMap.get(a) || '';
}
};

/******************************************************************************/
Expand Down Expand Up @@ -386,6 +447,16 @@ FilteringContext.prototype.INLINE_ANY = FilteringContext.INLINE_ANY = INLINE_ANY
FilteringContext.prototype.PING_ANY = FilteringContext.PING_ANY = PING_ANY;
FilteringContext.prototype.SCRIPT_ANY = FilteringContext.SCRIPT_ANY = SCRIPT_ANY;

FilteringContext.prototype.METHOD_NONE = FilteringContext.METHOD_NONE = METHOD_NONE;
FilteringContext.prototype.METHOD_CONNECT = FilteringContext.METHOD_CONNECT = METHOD_CONNECT;
FilteringContext.prototype.METHOD_DELETE = FilteringContext.METHOD_DELETE = METHOD_DELETE;
FilteringContext.prototype.METHOD_GET = FilteringContext.METHOD_GET = METHOD_GET;
FilteringContext.prototype.METHOD_HEAD = FilteringContext.METHOD_HEAD = METHOD_HEAD;
FilteringContext.prototype.METHOD_OPTIONS = FilteringContext.METHOD_OPTIONS = METHOD_OPTIONS;
FilteringContext.prototype.METHOD_PATCH = FilteringContext.METHOD_PATCH = METHOD_PATCH;
FilteringContext.prototype.METHOD_POST = FilteringContext.METHOD_POST = METHOD_POST;
FilteringContext.prototype.METHOD_PUT = FilteringContext.METHOD_PUT = METHOD_PUT;

/******************************************************************************/

export { FilteringContext };

0 comments on commit b698187

Please sign in to comment.