Commit 4f5bfd9
committed
Fix use of precedence in endpoint routing DFA (#20801)
* Fix use of precedence in endpoint routing DFA
Fixes: #18677
Fixes: #16579
This is a change to how sorting is use when building endpoint routing's graph of
nodes that is eventually transformed into the route table. There were
bugs in how this was done that made it incompatible in some niche
scenarios both with previous implementations and how we describe the
features in the abstract.
There are a wide array of cases that might have been impacted by this
bug because routing is a pattern language. Generally the bugs will involve a
catch-all, and some something that changes ordering of templates.
Issue #18677 has the simplest repro for this, the following templates
would not behave as expected:
```
a/{*b}
{a}/{b}
```
One would expect any URL Path starting with `/a` to match the first
route, but that's not what happens.
---
The change supports an opt-in via the following AppContext switch:
```
Microsoft.AspNetCore.Routing.UseCorrectCatchAllBehavior
```
Set to true to enable the correct behavior.
---
The root cause of this bug was an issue in how the algorithm used to be
build the DFA was designed. Specifically that it uses a BFS to build the
graph, and it uses an up-front one-time sort of endpoints in order to
drive that BFS.
The building of the graph has the expectation that at each level, we
will process **all** literal segments (`/a`) and then **all** parameter
segments (`/{a}`) and then **all** catch-all segments (`/{*a}`). Routing
defines a concept called *precedence* that defines the *conceptual*
order in while segments types are ordered.
So there are two problems:
- We sort based on criteria other than precedence (#16579)
- We can't rely on a one-time sort, it needs to be done at each level
(#18677)
---
The fix is to repeat the sort operation at each level and use precedence
as the only key for sorting (as dictated by the graph building algo).
We do a sort of the matches of each node *after* building the
precedence-based part of the DFA, based on the full sorting criteria, to
maintain compatibility.
* Add test1 parent 1b99352 commit 4f5bfd9
File tree
9 files changed
+807
-22
lines changed- src/Http/Routing
- ref
- src
- Matching
- Template
- test/UnitTests
- Matching
9 files changed
+807
-22
lines changedLines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| 202 | + | |
| 203 | + | |
202 | 204 | | |
203 | 205 | | |
204 | 206 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
43 | 53 | | |
44 | 54 | | |
45 | 55 | | |
| |||
52 | 62 | | |
53 | 63 | | |
54 | 64 | | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
55 | 71 | | |
56 | 72 | | |
57 | 73 | | |
58 | 74 | | |
59 | 75 | | |
60 | 76 | | |
61 | 77 | | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
67 | 86 | | |
68 | 87 | | |
69 | 88 | | |
70 | 89 | | |
71 | | - | |
72 | | - | |
| 90 | + | |
| 91 | + | |
73 | 92 | | |
74 | 93 | | |
75 | 94 | | |
| |||
79 | 98 | | |
80 | 99 | | |
81 | 100 | | |
82 | | - | |
| 101 | + | |
| 102 | + | |
83 | 103 | | |
84 | | - | |
| 104 | + | |
85 | 105 | | |
| 106 | + | |
86 | 107 | | |
87 | 108 | | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
88 | 123 | | |
89 | 124 | | |
90 | 125 | | |
91 | 126 | | |
92 | | - | |
| 127 | + | |
93 | 128 | | |
94 | 129 | | |
95 | 130 | | |
96 | | - | |
| 131 | + | |
97 | 132 | | |
98 | 133 | | |
99 | 134 | | |
| |||
102 | 137 | | |
103 | 138 | | |
104 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
105 | 148 | | |
106 | 149 | | |
107 | | - | |
| 150 | + | |
108 | 151 | | |
109 | 152 | | |
110 | 153 | | |
| |||
122 | 165 | | |
123 | 166 | | |
124 | 167 | | |
125 | | - | |
| 168 | + | |
| 169 | + | |
126 | 170 | | |
127 | 171 | | |
128 | 172 | | |
129 | 173 | | |
130 | 174 | | |
131 | 175 | | |
132 | 176 | | |
133 | | - | |
| 177 | + | |
| 178 | + | |
134 | 179 | | |
135 | 180 | | |
136 | 181 | | |
| |||
281 | 326 | | |
282 | 327 | | |
283 | 328 | | |
284 | | - | |
| 329 | + | |
285 | 330 | | |
286 | 331 | | |
287 | 332 | | |
| |||
302 | 347 | | |
303 | 348 | | |
304 | 349 | | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
305 | 362 | | |
306 | 363 | | |
307 | 364 | | |
| |||
670 | 727 | | |
671 | 728 | | |
672 | 729 | | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
673 | 734 | | |
674 | 735 | | |
675 | 736 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
219 | 219 | | |
220 | 220 | | |
221 | 221 | | |
222 | | - | |
| 222 | + | |
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
| |||
260 | 260 | | |
261 | 261 | | |
262 | 262 | | |
263 | | - | |
| 263 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
0 commit comments