URL patterns » History » Version 12
koszko, 02/25/2022 06:45 PM
update for 1.0
1 | 1 | jahoti | # URL patterns |
---|---|---|---|
2 | |||
3 | We want to be able to apply different rules and custom scripts for different websites. However, merely specifying "do this for `https://example.com`" is not enough. Single site's pages might differ strongly and require different custom scripts to be loaded. However, always matching against a full URL like `https://example.com/something/somethingelse` doesn't allow us to properly handle a site that serves similar pages for multiple values substituted for `somethingelse`. |
||
4 | |||
5 | {{toc}} |
||
6 | |||
7 | ## Currently employed solution |
||
8 | 6 | jahoti | Wildcards are being used to address the problem. Each page entry in Haketilo settings has a URL pattern that specifies to which internet pages it applies. A URL pattern can be as as simple as literal URL in which case it only matches itself. It can also contain wildcards in the form of one or more asterisks (`*`) that correspond to multiple possible strings occurring in that place. |
9 | 1 | jahoti | |
10 | Wildcards can appear in URL's domain and path that follows it. These 2 types of wildcards are handled separately. |
||
11 | |||
12 | ### Domain wildcards |
||
13 | A domain wildcard takes the form of one, two or three asterisks occurring in place of a single domain name segment at the beginning (left). Depending on the number of asterisks, the meaning is as follows: |
||
14 | * no asterisks (e.g. `example.com`) - match domain name exactly (e.g. `example.com`) |
||
15 | * one asterisk (e.g. `*.example.com`) - match all domains resulting from substituting `*` with a **single** segment (e.g. `banana.example.com` or `pineapple.example.com` but **not** `pineapple.pen.example.com` nor `example.com`) |
||
16 | * two asterisks (e.g. `**.example.com`) - match all domains resulting from substituting `**` with **two or more** segments (e.g. `monad.breakfast.example.com` or `pure.monad.breakfast.example.com` but **not** `cabalhell.example.com` nor `example.com`) |
||
17 | * three asterisks (e.g. `***.example.com`) - match all domains resulting from substituting `***` with **zero or more** segments (e.g. `hello.parkmeter.example.com` or `iliketrains.example.com` or `example.com`) |
||
18 | |||
19 | ### Path wildcards |
||
20 | A path wildcard takes the form of one, two or three asterisks occurring in place of a single path segment at the end of path (right). Depending on the number of asterisks, the meaning is as follows: |
||
21 | 7 | koszko | * no asterisks (e.g. `/joke/clowns`) - match path exactly (e.g. `/joke/clowns`) |
22 | * one asterisk (e.g. `/itscalled/*`) - match all paths resulting from substituting `*` with a **single** segment (e.g. `/itscalled/gnulinux` or `/itscalled/glamp` but **not** `/itscalled/` nor `/itscalled/gnu/linux`) |
||
23 | * two asterisks (e.g. `/another/**`) - match all paths resulting from substituting `**` with **two or more** segments (e.g. `/another/nsa/backdoor` or `/another/best/programming/language` but **not** `/another/apibreak` nor `/another`) |
||
24 | * three asterisks (e.g. `/mail/dmarc/***`) - match all paths resulting from substituting `***` with **zero or more** segments (e.g. `/mail/dmarc/spf`, `/mail/dmarc` or `/mail/dmarc/dkim/failure` but **not** `/mail/`) |
||
25 | 1 | jahoti | |
26 | 7 | koszko | If pattern ends **without** a trailing slash, it mathes paths with any number of trailing slashes, including zero. If pattern ends **with** a trailing slash, it only mathes paths with one or more trailing slashes. For example, `/itscalled/*` matches `/itscalled/gnulinux`, `/itscalled/gnulinux/` and `/itscalled/gnulinux//` while `/itscalled/*/` only matches `/itscalled/gnulinux/` and `/itscalled/gnulinux//` out of those three. |
27 | 1 | jahoti | |
28 | If two patterns only differ by the presence of a trailing slash, pattern **with** a trailing slash is considered **more specific**. |
||
29 | |||
30 | 12 | koszko | Additionally, any path with literal trailing asterisks is matched by itself, even if such pattern would otherwise be treated as wildcard (e.g. `/gobacktoxul/**` matches `/gobacktoxul/**`). This is likely to change in the future and would best not be relied upon. Appending three additional asterisks to path pattern to represent literal asterisks is being considered. |
31 | |||
32 | 1 | jahoti | ### Future additions |
33 | Right now a URL's query string is being completely disregarded for the purpose of matching the URL patterns. In the future, support for matching URLs with specific query parameters and maybe even HTTP(s) requests with specific POST parameters or specific cookies might me added. |
||
34 | |||
35 | Currently, protocols in the URL are matched exactly. Making protocol wildcard might make little sense because sites are very unlikely to be serving similar content over for example `http://` and `ftp://`. However, support for some wildcard protocol that matches both `http://` and `https://` might be added in the future. |
||
36 | |||
37 | The wildcards that have been added so far were designed to allow a reasonable level of flexibility, considering some common ways websites are served. However, only practice can show what works best, and so wildcard semantics are subject to change as the project matures. |
||
38 | |||
39 | ### Wildcard priorities and querying |
||
40 | In case multiple patterns match some URL, the more specific one is preferred. Specificity is considered as follows: |
||
41 | * If patterns only differ in the final path segment, the one with least wildcard asterisks in that segment if preferred. |
||
42 | * If patterns, besides the above, only differ in path length, one with longer path is preferred. Neither final wildcard segment nor trailing dashes account for path length. |
||
43 | * If patterns, besides the above, only differ in the initial domain segment, one with least wildcard asterisks in that segment is preferred. |
||
44 | * If patterns differ in domain length, one with longer domain is preferred. Initial wildcard segment does not account for domain length. |
||
45 | |||
46 | 12 | koszko | As an example, consider the URL `http://settings.query.example.com/google/tries/destroy/adblockers//`. Patterns matching it are, in the following order: |
47 | 1 | jahoti | |
48 | 2 | koszko | ``` |
49 | 12 | koszko | http://settings.query.example.com/google/tries/destroy/adblockers/ |
50 | 1 | jahoti | http://settings.query.example.com/google/tries/destroy/adblockers |
51 | 12 | koszko | http://settings.query.example.com/google/tries/destroy/adblockers/***/ |
52 | 1 | jahoti | http://settings.query.example.com/google/tries/destroy/adblockers/*** |
53 | 12 | koszko | http://settings.query.example.com/google/tries/destroy/*/ |
54 | 1 | jahoti | http://settings.query.example.com/google/tries/destroy/* |
55 | 12 | koszko | http://settings.query.example.com/google/tries/destroy/***/ |
56 | 1 | jahoti | http://settings.query.example.com/google/tries/destroy/*** |
57 | 12 | koszko | http://settings.query.example.com/google/tries/**/ |
58 | 1 | jahoti | http://settings.query.example.com/google/tries/** |
59 | 12 | koszko | http://settings.query.example.com/google/tries/***/ |
60 | 1 | jahoti | http://settings.query.example.com/google/tries/*** |
61 | 12 | koszko | http://settings.query.example.com/google/**/ |
62 | 1 | jahoti | http://settings.query.example.com/google/** |
63 | 12 | koszko | http://settings.query.example.com/google/***/ |
64 | 1 | jahoti | http://settings.query.example.com/google/*** |
65 | 12 | koszko | http://settings.query.example.com/**/ |
66 | 1 | jahoti | http://settings.query.example.com/** |
67 | 12 | koszko | http://settings.query.example.com/***/ |
68 | 1 | jahoti | http://settings.query.example.com/*** |
69 | 12 | koszko | http://***.settings.query.example.com/google/tries/destroy/adblockers/ |
70 | 1 | jahoti | http://***.settings.query.example.com/google/tries/destroy/adblockers |
71 | 12 | koszko | http://***.settings.query.example.com/google/tries/destroy/adblockers/***/ |
72 | 1 | jahoti | http://***.settings.query.example.com/google/tries/destroy/adblockers/*** |
73 | 12 | koszko | http://***.settings.query.example.com/google/tries/destroy/*/ |
74 | 1 | jahoti | http://***.settings.query.example.com/google/tries/destroy/* |
75 | 12 | koszko | http://***.settings.query.example.com/google/tries/destroy/***/ |
76 | 1 | jahoti | http://***.settings.query.example.com/google/tries/destroy/*** |
77 | 12 | koszko | http://***.settings.query.example.com/google/tries/**/ |
78 | 1 | jahoti | http://***.settings.query.example.com/google/tries/** |
79 | 12 | koszko | http://***.settings.query.example.com/google/tries/***/ |
80 | 1 | jahoti | http://***.settings.query.example.com/google/tries/*** |
81 | 12 | koszko | http://***.settings.query.example.com/google/**/ |
82 | 1 | jahoti | http://***.settings.query.example.com/google/** |
83 | 12 | koszko | http://***.settings.query.example.com/google/***/ |
84 | 1 | jahoti | http://***.settings.query.example.com/google/*** |
85 | 12 | koszko | http://***.settings.query.example.com/**/ |
86 | 1 | jahoti | http://***.settings.query.example.com/** |
87 | 12 | koszko | http://***.settings.query.example.com/***/ |
88 | 1 | jahoti | http://***.settings.query.example.com/*** |
89 | 12 | koszko | http://*.query.example.com/google/tries/destroy/adblockers/ |
90 | 1 | jahoti | http://*.query.example.com/google/tries/destroy/adblockers |
91 | 12 | koszko | http://*.query.example.com/google/tries/destroy/adblockers/***/ |
92 | 1 | jahoti | http://*.query.example.com/google/tries/destroy/adblockers/*** |
93 | 12 | koszko | http://*.query.example.com/google/tries/destroy/*/ |
94 | 1 | jahoti | http://*.query.example.com/google/tries/destroy/* |
95 | 12 | koszko | http://*.query.example.com/google/tries/destroy/***/ |
96 | 1 | jahoti | http://*.query.example.com/google/tries/destroy/*** |
97 | 12 | koszko | http://*.query.example.com/google/tries/**/ |
98 | 1 | jahoti | http://*.query.example.com/google/tries/** |
99 | 12 | koszko | http://*.query.example.com/google/tries/***/ |
100 | 1 | jahoti | http://*.query.example.com/google/tries/*** |
101 | 12 | koszko | http://*.query.example.com/google/**/ |
102 | 1 | jahoti | http://*.query.example.com/google/** |
103 | 12 | koszko | http://*.query.example.com/google/***/ |
104 | 1 | jahoti | http://*.query.example.com/google/*** |
105 | 12 | koszko | http://*.query.example.com/**/ |
106 | 1 | jahoti | http://*.query.example.com/** |
107 | 12 | koszko | http://*.query.example.com/***/ |
108 | 1 | jahoti | http://*.query.example.com/*** |
109 | 12 | koszko | http://***.query.example.com/google/tries/destroy/adblockers/ |
110 | 1 | jahoti | http://***.query.example.com/google/tries/destroy/adblockers |
111 | 12 | koszko | http://***.query.example.com/google/tries/destroy/adblockers/***/ |
112 | 1 | jahoti | http://***.query.example.com/google/tries/destroy/adblockers/*** |
113 | 12 | koszko | http://***.query.example.com/google/tries/destroy/*/ |
114 | 1 | jahoti | http://***.query.example.com/google/tries/destroy/* |
115 | 12 | koszko | http://***.query.example.com/google/tries/destroy/***/ |
116 | 1 | jahoti | http://***.query.example.com/google/tries/destroy/*** |
117 | 12 | koszko | http://***.query.example.com/google/tries/**/ |
118 | 1 | jahoti | http://***.query.example.com/google/tries/** |
119 | 12 | koszko | http://***.query.example.com/google/tries/***/ |
120 | 1 | jahoti | http://***.query.example.com/google/tries/*** |
121 | 12 | koszko | http://***.query.example.com/google/**/ |
122 | 1 | jahoti | http://***.query.example.com/google/** |
123 | 12 | koszko | http://***.query.example.com/google/***/ |
124 | 1 | jahoti | http://***.query.example.com/google/*** |
125 | 12 | koszko | http://***.query.example.com/**/ |
126 | 1 | jahoti | http://***.query.example.com/** |
127 | 12 | koszko | http://***.query.example.com/***/ |
128 | 1 | jahoti | http://***.query.example.com/*** |
129 | 12 | koszko | http://**.example.com/google/tries/destroy/adblockers/ |
130 | 1 | jahoti | http://**.example.com/google/tries/destroy/adblockers |
131 | 12 | koszko | http://**.example.com/google/tries/destroy/adblockers/***/ |
132 | 1 | jahoti | http://**.example.com/google/tries/destroy/adblockers/*** |
133 | 12 | koszko | http://**.example.com/google/tries/destroy/*/ |
134 | 1 | jahoti | http://**.example.com/google/tries/destroy/* |
135 | 12 | koszko | http://**.example.com/google/tries/destroy/***/ |
136 | 1 | jahoti | http://**.example.com/google/tries/destroy/*** |
137 | 12 | koszko | http://**.example.com/google/tries/**/ |
138 | 1 | jahoti | http://**.example.com/google/tries/** |
139 | 12 | koszko | http://**.example.com/google/tries/***/ |
140 | 1 | jahoti | http://**.example.com/google/tries/*** |
141 | 12 | koszko | http://**.example.com/google/**/ |
142 | 1 | jahoti | http://**.example.com/google/** |
143 | 12 | koszko | http://**.example.com/google/***/ |
144 | 1 | jahoti | http://**.example.com/google/*** |
145 | 12 | koszko | http://**.example.com/**/ |
146 | 1 | jahoti | http://**.example.com/** |
147 | 12 | koszko | http://**.example.com/***/ |
148 | 1 | jahoti | http://**.example.com/*** |
149 | 12 | koszko | http://***.example.com/google/tries/destroy/adblockers/ |
150 | 1 | jahoti | http://***.example.com/google/tries/destroy/adblockers |
151 | 12 | koszko | http://***.example.com/google/tries/destroy/adblockers/***/ |
152 | 1 | jahoti | http://***.example.com/google/tries/destroy/adblockers/*** |
153 | 12 | koszko | http://***.example.com/google/tries/destroy/*/ |
154 | 1 | jahoti | http://***.example.com/google/tries/destroy/* |
155 | 12 | koszko | http://***.example.com/google/tries/destroy/***/ |
156 | 1 | jahoti | http://***.example.com/google/tries/destroy/*** |
157 | 12 | koszko | http://***.example.com/google/tries/**/ |
158 | 1 | jahoti | http://***.example.com/google/tries/** |
159 | 12 | koszko | http://***.example.com/google/tries/***/ |
160 | 1 | jahoti | http://***.example.com/google/tries/*** |
161 | 12 | koszko | http://***.example.com/google/**/ |
162 | 1 | jahoti | http://***.example.com/google/** |
163 | 12 | koszko | http://***.example.com/google/***/ |
164 | 1 | jahoti | http://***.example.com/google/*** |
165 | 12 | koszko | http://***.example.com/**/ |
166 | 1 | jahoti | http://***.example.com/** |
167 | 12 | koszko | http://***.example.com/***/ |
168 | 1 | jahoti | http://***.example.com/*** |
169 | 11 | koszko | ``` |
170 | |||
171 | 1 | jahoti | For a simpler URL like `https://example.com` the patterns would be: |
172 | |||
173 | ``` |
||
174 | https://example.com |
||
175 | https://example.com/*** |
||
176 | https://***.example.com |
||
177 | https://***.example.com/*** |
||
178 | ``` |
||
179 | 11 | koszko | |
180 | Variants of those patterns with a trailing dash added would **not** match the URL. |
||
181 | 4 | koszko | |
182 | ### Limits |
||
183 | 6 | jahoti | In order to prevent some easy-to-conduct DoS attacks, both Haketilo and Hydrilla limit the lengths of domain and path parts of processed URLs. Limits are configured in source code using 4 constants: |
184 | 4 | koszko | * `MAX_URL_PATH_LEN` (set to 12) - the maximum number of path segments |
185 | * `MAX_URL_PATH_CHARS` (set to 255) - maximum length of the path part of a URL |
||
186 | * `MAX_DOMAIN_LEN` (set to 7) - maximum number of domain labels |
||
187 | * `MAX_DOMAIN_CHARS` (set to 100) - maximum length of the domain part of a URL |
||
188 | |||
189 | Whenever one of those limits causes a URL to be truncated, only the patterns that can be deduced from the shortened version are processed. The limits might be changed or completely lifted in some future version of the tools. |
||
190 | |||
191 | 6 | jahoti | For `file://` URLs Haketilo does not impose the limits. |
192 | 1 | jahoti | |
193 | ## Alternative solution: mimicking web server mechanics |
||
194 | While wildcard patterns as presented give a lot of flexibility, they are not the only viable approach to specifying what URLs given settings of custom scripts should be applied to. In fact, wildcards are different from how the server side of a typical website decides what to return for a given URL request. |
||
195 | |||
196 | In a typical scenario, an HTTP server like Apache reads configuration files provided by its administrator and uses various virtual host, redirect, request rewrite, CGI, etc. instructions to decide how to handle given URL. It is possible using a schema that mimics the configuration options typically used with web servers would give more efficiency in specifying what page settings to apply when. |
||
197 | |||
198 | This approach shall be considered in the future. |