Module: WebriskHash::Suffixes
Overview
Suffix/Prefix Expression Generation for Web Risk API
Generates all possible host suffix and path prefix combinations for a canonicalized URL. These combinations are used to create the expressions that will be hashed and checked against Web Risk lists.
The process follows the Web Risk API specification: https://cloud.google.com/web-risk/docs/urls-hashing#suffixprefix-expressions
Instance Method Summary collapse
-
#suffix_postfix_expressions(canonical_url) ⇒ Set<String>
Generate suffix/prefix expressions for a canonicalized URL.
Instance Method Details
#suffix_postfix_expressions(canonical_url) ⇒ Set<String>
Generate suffix/prefix expressions for a canonicalized URL
Returns up to 30 different host suffix and path prefix combinations. Only the host and path components are used; scheme, username, password, and port are discarded.
For the host, tries at most 5 different strings:
- The exact hostname in the URL
- Up to 4 hostnames formed by starting with the last 5 components and successively removing the leading component Note: Additional hostnames are not checked if the host is an IP address
For the path, tries at most 6 different strings:
- The exact path of the URL, including query parameters
- The exact path of the URL, without query parameters 3-6. The four paths formed by starting at root (/) and successively appending path components, including trailing slash
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/webrisk_hash/suffixes.rb', line 55 def suffix_postfix_expressions(canonical_url) return Set.new unless canonical_url u = canonical_url.sub(%r{^[^:]+://}, '') host, rest = u.split('/', 2) path_and_query = "/#{rest || ''}" if path_and_query.include?('?') path, query = path_and_query.split('?', 2) else path = path_and_query query = nil end host_suffixes = generate_host_suffixes(host) path_prefixes = generate_path_prefixes(path, query) results = [] host_suffixes.each do |h| path_prefixes.each do |p| results << (h + p) end end Set.new(results) end |