JavaScript Restrictor
Browser extension that improves privacy and security
http_shield_common.js File Reference

This file contains common functions for Network Boundary Shield. More...

Functions

function isIPV4 (url)
 
function isIPV6 (url)
 
function isIPV4Private (ipAddr)
 
function isIPV6Private (ipAddr)
 
function parseCSV (csv, ipv4)
 
function CSVToArray (strData)
 
function expandIPV6 (ip6addr)
 
function checkWhitelist (hostname)
 
function notifyBlockedRequest (origin, target, resource)
 
function messageListener ({message, site}, sender)
 The event listener, hooked up to the webExtension onMessage event. More...
 
function commonMessageListener (message, sender)
 

Variables

var localIPV4DNSZones
 
var localIPV6DNSZones
 
var doNotBlockHosts = new Object()
 
let readFile
 

Detailed Description

This file contains common functions for Network Boundary Shield.

Author
Copyright (C) 2020 Pavel Pohner
Copyright (C) 2020-2021 Martin Bednář
License:
SPDX-License-Identifier: GPL-3.0-or-later

This file contains basic logic of the NBS, NBS global variables and objects, functions for reading and parsing CSV files, and functions for identifying and processing IP addresses and checking IP ranges.

Function Documentation

◆ checkWhitelist()

function checkWhitelist (   hostname)

Check if the hostname or any of it's domains is whitelisted.

Parameters
hostnameAny hostname (subdomains allowed).
Returns
TRUE when domain (or subdomain) is whitelisted, FALSE otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ commonMessageListener()

function commonMessageListener (   message,
  sender 
)

Event listener hooked up to webExtensions onMessage event. Does appropriate action based on message (e.g. Turn on/off the NBS, add/remove a site to/from whitelist, ...).

Parameters
messageReceives full message.
senderSender of the message.
Here is the call graph for this function:

◆ CSVToArray()

function CSVToArray (   strData)

Auxillary function for parsing CSV files. Converts CSV to array.

Parameters
strDataLoaded CSV file as a string.
Returns
array containing CSV rows.
Here is the caller graph for this function:

◆ expandIPV6()

function expandIPV6 (   ip6addr)

Function for expanding shorten ipv6 addresses.

Parameters
ip6addrValid ipv6 address.
Returns
expanded ipv6 address in string.
Here is the caller graph for this function:

◆ isIPV4()

function isIPV4 (   url)

Checks validity of IPv4 addresses.

Parameters
urlAn URL that may or may not contains an IPv4 address instead of a domain name.
Returns
TRUE if the url matches IPv4 regex, FALSE otherwise.
Here is the caller graph for this function:

◆ isIPV4Private()

function isIPV4Private (   ipAddr)

Checks whether the ipAddr is found in IPv4 localZones. If the IPv4 address is found in any IPv4 local zone, it means that this IPv4 address is private. IPv4 local zone is e.g. 192.168.000.000/16.

Parameters
ipAddrValid IPv4 address.
Returns
TRUE if ipAddr exists in localZones fetched from IANA, FALSE otherwise.

\seealso https://github.com/polcak/jsrestrictor/issues/125 and the tempral white listing of 0.0.0.0 until we provide a checkbox for notifications.

Here is the caller graph for this function:

◆ isIPV6()

function isIPV6 (   url)

Checks validity IPV6 address.

Parameters
urlAn URL that may or may not contains an IPv6 address instead of a domain name.
Returns
TRUE, if URL is valid IPV6 address, FALSE otherwise.
Here is the caller graph for this function:

◆ isIPV6Private()

function isIPV6Private (   ipAddr)

Checks whether the ipAddr is found in IPv6 localZones. If the IPv6 address is found in any IPv6 local zone, it means that this IPv6 address is private. IPv6 local zone is e.g. fe80::/10.

Parameters
ipAddrValid IPv6 address.
Returns
TRUE if ipAddr exists in localZones fetched from IANA, FALSE otherwise.

\seealso https://github.com/polcak/jsrestrictor/issues/125 and the tempral white listing of 0.0.0.0 until we provide a checkbox for notifications.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ messageListener()

function messageListener (   {message, site},
  sender 
)

The event listener, hooked up to the webExtension onMessage event.

The listener sends message response which contains information if the current site is whitelisted or not.

Parameters
messageReceives full message (destructured as {message, site}).
senderSender of the message.

◆ notifyBlockedRequest()

function notifyBlockedRequest (   origin,
  target,
  resource 
)

Creates and presents notification to the user. Works with webExtensions notification API. Creates notification about blocked request.

Parameters
originOrigin of the request.
targetTarget of the request.
resourceType of the resource.
Here is the caller graph for this function:

◆ parseCSV()

function parseCSV (   csv,
  ipv4 
)

Function for parsing CSV files obtained from IANA. It strips .IN-ADDR and .IP6 from zones and comma delimiter, merges them into array by CSV rows.

Parameters
csvCSV obtained from IANA.
ipv4Boolean, saying whether the csv is IPv4 CSV or IPv6.
Returns
an array of parsed CSV values.
Here is the call graph for this function:

Variable Documentation

◆ doNotBlockHosts

var doNotBlockHosts = new Object()

Associtive array of hosts, that are currently among trusted "do not blocked" hosts.

◆ localIPV4DNSZones

var localIPV4DNSZones

Locally served IPV4 DNS zones loaded from IANA.

◆ localIPV6DNSZones

var localIPV6DNSZones

Locally served IPV6 DNS zones loaded from IANA.

◆ readFile

let readFile
Initial value:
= (_path) => {
return new Promise((resolve, reject) => {
fetch(_path, {mode:'same-origin'})
.then(function(_res) {
return _res.blob();
})
.then(function(_blob) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
resolve(this.result);
});
reader.readAsText(_blob);
})
.catch(error => {
reject(error);
});
});
}

The function for reading a locally stored csv file.

Parameters
_pathString with a fully-qualified URL. E.g.: moz-extension://2c127fa4-62c7-7e4f-90e5-472b45eecfdc/beasts/frog.dat
Returns
promise for returning content of the file as a string.