Skip to content

Commit

Permalink
Renaming blacklist/whitelist aligned with iOS mozilla-mobile#7557
Browse files Browse the repository at this point in the history
  • Loading branch information
vmadalin authored and mergify[bot] committed Oct 26, 2020
1 parent 4ed8b9e commit f57c1bf
Show file tree
Hide file tree
Showing 19 changed files with 239 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,8 @@ class SystemEngineView @JvmOverloads constructor(
URL_MATCHER?.setCategoriesEnabled(categories) ?: run {
URL_MATCHER = UrlMatcher.createMatcher(
resources,
R.raw.domain_blacklist,
R.raw.domain_whitelist,
R.raw.domain_blocklist,
R.raw.domain_safelist,
categories)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import android.util.JsonReader
import java.util.ArrayList

/**
* Stores white-listed URIs for individual hosts.
* Stores safe-listed URIs for individual hosts.
*/
internal class WhiteList {
private val rootNode: WhiteListTrie = WhiteListTrie.createRootNode()
internal class Safelist {
private val rootNode: SafelistTrie = SafelistTrie.createRootNode()

/**
* Adds the provided whitelist for the provided host.
* Adds the provided safelist for the provided host.
*
* @param host the reversed host URI ("foo.com".reverse())
* @param whitelist a [Trie] representing the white-listed URIs
* @param safelist a [Trie] representing the safe-listed URIs
*/
fun put(host: ReversibleString, whitelist: Trie) {
rootNode.putWhiteList(host, whitelist)
fun put(host: ReversibleString, safelist: Trie) {
rootNode.putSafelist(host, safelist)
}

/**
* Checks if the given resource is white-listed for the given host.
* Checks if the given resource is safe-listed for the given host.
*
* @param host the host URI as string ("foo.com")
* @param host the resources URI as string ("bar.com")
Expand All @@ -36,7 +36,7 @@ internal class WhiteList {
}

/**
* Checks if the given resource is white-listed for the given host.
* Checks if the given resource is safe-listed for the given host.
*
* @param hostUri the host URI
* @param resource the resources URI
Expand All @@ -53,9 +53,9 @@ internal class WhiteList {
}

private fun contains(site: ReversibleString, resource: ReversibleString, revHostTrie: Trie): Boolean {
val next = revHostTrie.children.get(site.charAt(0).toInt()) as? WhiteListTrie ?: return false
val next = revHostTrie.children.get(site.charAt(0).toInt()) as? SafelistTrie ?: return false

if (next.whitelist?.findNode(resource) != null) {
if (next.safelist?.findNode(resource) != null) {
return true
}

Expand Down Expand Up @@ -83,21 +83,21 @@ internal class WhiteList {

companion object {
/**
* Parses json for white-listed URIs.
* Parses json for safe-listed URIs.
*
* @param reader a JsonReader
* @return the white list.
* @return the safe list.
*/
@Suppress("NestedBlockDepth")
fun fromJson(reader: JsonReader): WhiteList {
val whiteList = WhiteList()
fun fromJson(reader: JsonReader): Safelist {
val safelist = Safelist()
reader.beginObject()

while (reader.hasNext()) {
reader.skipValue()
reader.beginObject()

val whitelistTrie = Trie.createRootNode()
val safelistTrie = Trie.createRootNode()
val propertyList = ArrayList<String>()
while (reader.hasNext()) {
val itemName = reader.nextName()
Expand All @@ -110,66 +110,66 @@ internal class WhiteList {
} else if (itemName == "resources") {
reader.beginArray()
while (reader.hasNext()) {
whitelistTrie.put(reader.nextString().reverse())
safelistTrie.put(reader.nextString().reverse())
}
reader.endArray()
}
}
propertyList.forEach { whiteList.put(it.reverse(), whitelistTrie) }
propertyList.forEach { safelist.put(it.reverse(), safelistTrie) }
reader.endObject()
}
reader.endObject()
return whiteList
return safelist
}
}
}

/**
* A [Trie] implementation which stores a white list (another [Trie]).
* A [Trie] implementation which stores a safe list (another [Trie]).
*/
internal class WhiteListTrie private constructor(character: Char, parent: WhiteListTrie?) : Trie(character, parent) {
var whitelist: Trie? = null
internal class SafelistTrie private constructor(character: Char, parent: SafelistTrie?) : Trie(character, parent) {
var safelist: Trie? = null

override fun createNode(character: Char, parent: Trie): Trie {
return WhiteListTrie(character, parent as WhiteListTrie)
return SafelistTrie(character, parent as SafelistTrie)
}

/**
* Adds new nodes (recursively) for all chars in the provided string and stores
* the provide whitelist Trie.
* the provide safelist Trie.
*
* @param string the string for which a node should be added.
* @param whitelist the whitelist to store.
* @param safelist the safelist to store.
* @return the newly created node or the existing one.
*/
fun putWhiteList(string: String, whitelist: Trie) {
this.putWhiteList(string.reversible(), whitelist)
fun putSafelist(string: String, safelist: Trie) {
this.putSafelist(string.reversible(), safelist)
}

/**
* Adds new nodes (recursively) for all chars in the provided string and stores
* the provide whitelist Trie.
* the provide safelist Trie.
*
* @param string the string for which a node should be added.
* @param whitelist the whitelist to store.
* @param safelist the safelist to store.
* @return the newly created node or the existing one.
*/
fun putWhiteList(string: ReversibleString, whitelist: Trie) {
val node = super.put(string) as WhiteListTrie
fun putSafelist(string: ReversibleString, safelist: Trie) {
val node = super.put(string) as SafelistTrie

if (node.whitelist != null) {
throw IllegalStateException("Whitelist already set for node $string")
if (node.safelist != null) {
throw IllegalStateException("Safelist already set for node $string")
}

node.whitelist = whitelist
node.safelist = safelist
}

companion object {
/**
* Creates a new root node.
*/
fun createRootNode(): WhiteListTrie {
return WhiteListTrie(Character.MIN_VALUE, null)
fun createRootNode(): SafelistTrie {
return SafelistTrie(Character.MIN_VALUE, null)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@ import java.nio.charset.StandardCharsets.UTF_8
import java.util.LinkedList

/**
* Provides functionality to process categorized URL black/white lists and match
* Provides functionality to process categorized URL block/safe lists and match
* URLs against these lists.
*/
class UrlMatcher {
private val categories: MutableMap<String, Trie>
internal val enabledCategories = HashSet<String>()

private val whiteList: WhiteList?
private val safelist: Safelist?
private val previouslyMatched = HashSet<String>()
private val previouslyUnmatched = HashSet<String>()

constructor(patterns: Array<String>) {
categories = HashMap()
whiteList = null
safelist = null

val defaultCategory = Trie.createRootNode()
patterns.forEach { defaultCategory.put(it.reverse()) }
Expand All @@ -40,9 +40,9 @@ class UrlMatcher {
enabledCategories: Set<String>,
supportedCategories: Set<String>,
categoryMap: MutableMap<String, Trie>,
whiteList: WhiteList? = null
safelist: Safelist? = null
) {
this.whiteList = whiteList
this.safelist = safelist
this.categories = categoryMap

for ((key) in categoryMap) {
Expand Down Expand Up @@ -85,7 +85,7 @@ class UrlMatcher {
}

/**
* Checks if the given page URI is blacklisted for the given resource URI.
* Checks if the given page URI is blocklisted for the given resource URI.
* Returns true if the site (page URI) is allowed to access
* the resource URI, otherwise false.
*
Expand All @@ -99,7 +99,7 @@ class UrlMatcher {
}

/**
* Checks if the given page URI is blacklisted for the given resource URI.
* Checks if the given page URI is blocklisted for the given resource URI.
* Returns true if the site (page URI) is allowed to access
* the resource URI, otherwise false.
*
Expand All @@ -119,7 +119,7 @@ class UrlMatcher {
return notMatchesFound
}

if (whiteList?.contains(pageURI, resourceURI) == true) {
if (safelist?.contains(pageURI, resourceURI) == true) {
return notMatchesFound
}

Expand Down Expand Up @@ -170,54 +170,54 @@ class UrlMatcher {
* Creates a new matcher instance for the provided URL lists.
*
* @deprecated Pass resources directly
* @param blackListFile resource ID to a JSON file containing the black list
* @param whiteListFile resource ID to a JSON file containing the white list
* @param blocklistFile resource ID to a JSON file containing the block list
* @param safelistFile resource ID to a JSON file containing the safe list
*/
fun createMatcher(
context: Context,
@RawRes blackListFile: Int,
@RawRes whiteListFile: Int,
@RawRes blocklistFile: Int,
@RawRes safelistFile: Int,
enabledCategories: Set<String> = supportedCategories
): UrlMatcher =
createMatcher(context.resources, blackListFile, whiteListFile, enabledCategories)
createMatcher(context.resources, blocklistFile, safelistFile, enabledCategories)

/**
* Creates a new matcher instance for the provided URL lists.
*
* @param blackListFile resource ID to a JSON file containing the black list
* @param whiteListFile resource ID to a JSON file containing the white list
* @param blocklistFile resource ID to a JSON file containing the block list
* @param safelistFile resource ID to a JSON file containing the safe list
*/
fun createMatcher(
resources: Resources,
@RawRes blackListFile: Int,
@RawRes whiteListFile: Int,
@RawRes blocklistFile: Int,
@RawRes safelistFile: Int,
enabledCategories: Set<String> = supportedCategories
): UrlMatcher {
val blackListReader = InputStreamReader(resources.openRawResource(blackListFile), UTF_8)
val whiteListReader = InputStreamReader(resources.openRawResource(whiteListFile), UTF_8)
return createMatcher(blackListReader, whiteListReader, enabledCategories)
val blocklistReader = InputStreamReader(resources.openRawResource(blocklistFile), UTF_8)
val safelistReader = InputStreamReader(resources.openRawResource(safelistFile), UTF_8)
return createMatcher(blocklistReader, safelistReader, enabledCategories)
}

/**
* Creates a new matcher instance for the provided URL lists.
*
* @param black reader containing the black list
* @param white resource ID to a JSON file containing the white list
* @param block reader containing the block list
* @param safe resource ID to a JSON file containing the safe list
*/
fun createMatcher(
black: Reader,
white: Reader,
block: Reader,
safe: Reader,
enabledCategories: Set<String> = supportedCategories
): UrlMatcher {
val categoryMap = HashMap<String, Trie>()

JsonReader(black).use {
JsonReader(block).use {
jsonReader -> loadCategories(jsonReader, categoryMap)
}

var whiteList: WhiteList?
JsonReader(white).use { jsonReader -> whiteList = WhiteList.fromJson(jsonReader) }
return UrlMatcher(enabledCategories, supportedCategories, categoryMap, whiteList)
var safelist: Safelist?
JsonReader(safe).use { jsonReader -> safelist = Safelist.fromJson(jsonReader) }
return UrlMatcher(enabledCategories, supportedCategories, categoryMap, safelist)
}

/**
Expand Down
Loading

0 comments on commit f57c1bf

Please sign in to comment.