Commit 59f4b79
Use relaxed JSON escaping for log bodies (#340)
* Use relaxed JSON escaping for log bodies
The body formatter and batch serializer build Utf8JsonWriters with no
options, so they use the default HTML-safe JavaScriptEncoder, which
unicode-escapes every quote, ', <, >, & and all non-ASCII characters.
Because Serilog quotes string property values by default, this makes most
stored Loki lines unreadable (a regression from v8.x, which formatted
bodies with Serilog's JsonValueFormatter and standard escaping).
Construct the writers with JsonWriterOptions(Encoder =
JavaScriptEncoder.UnsafeRelaxedJsonEscaping). Output stays valid JSON and
UTF-8, so consumers (including the json parser) are unaffected. Add a unit
test covering markup and non-ASCII characters.
* Harden escaping test: assert quotes render as \" not "
Addresses CodeRabbit review on #340. Assert at the decoded body level (the raw
HTTP payload embeds the body as a JSON string, double-escaping its quotes), so
a regression back to " for quotes is caught.
* Centralize relaxed JSON writer; add Format-path and label escaping tests
The relaxed-encoder JsonWriterOptions was defined twice (formatter and
serializer) with drifting comments, and each Utf8JsonWriter opted in per
construction site: a future writer that forgot the options argument would
silently revert to HTML-safe \uXXXX escaping with no compiler warning.
Extract a single Infrastructure.JsonWriterDefaults.createWriter as the one
way the sink builds a writer, so escaping cannot drift between sites. The
shared comment also corrects the description: the relaxed encoder still
escapes U+2028/U+2029 and DEL, so it is not "only what JSON mandates".
Add the two missing wire-format guards:
- the public LokiJsonTextFormatter.Format entry point (the sink fast path
bypasses it, so its writer site had no test coverage);
- promoted label values (the envelope writer escapes label keys/values too).
The escaping tests sample neutral, multi-script non-ASCII (accented Latin
plus CJK) rather than a single script.
---------
Co-authored-by: Mykhailo Shevchuk <myte@ukr.net>1 parent fa376ad commit 59f4b79
5 files changed
Lines changed: 93 additions & 4 deletions
File tree
- src/Serilog.Sinks.Grafana.Loki
- Infrastructure
- tests/Serilog.Sinks.Grafana.Loki.UnitTests
Lines changed: 36 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
15 | 14 | | |
16 | 15 | | |
17 | 16 | | |
| |||
173 | 172 | | |
174 | 173 | | |
175 | 174 | | |
176 | | - | |
| 175 | + | |
177 | 176 | | |
178 | 177 | | |
179 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
33 | | - | |
| 33 | + | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | | - | |
| 91 | + | |
91 | 92 | | |
92 | 93 | | |
93 | 94 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
| |||
Lines changed: 52 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
255 | 307 | | |
256 | 308 | | |
257 | 309 | | |
| |||
0 commit comments