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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
[assembly: InternalsVisibleTo("Jellyfin.Drawing.Skia.Tests")]
namespace Jellyfin.Drawing.Skia;
/// <summary>
/// Validates that an SVG document does not reference external resources before it is rasterized.
/// </summary>
internal static class SvgSecurityValidator
{
// Guards against a chain of nested data:image/svg+xml payloads.
private const int MaxDataUriDepth = 4;
// Upper bound for a decompressed svgz payload carried inside a data URI, to guard against decompression bombs.
private const int MaxDecompressedBytes = 16 * 1024 * 1024;
private const int DecompressBufferSize = 81920;
private static readonly XmlReaderSettings _scanSettings = new()
{
DtdProcessing = DtdProcessing.Parse,
XmlResolver = null,
MaxCharactersFromEntities = 1024 * 1024,
IgnoreComments = true,
IgnoreProcessingInstructions = true,
IgnoreWhitespace = true,
CloseInput = false
};
/// <summary>
/// Determines whether the SVG at the given path is safe to rasterize, i.e. contains no references
/// to external resources.
/// </summary>
/// <param name="path">The path to the SVG file.</param>
/// <param name="reason">When this method returns <c>false</c>, the reason the document was rejected.</param>
/// <returns><c>true</c> if the document is free of external references; otherwise <c>false</c>.</returns>
public static bool IsSafe(string path, [NotNullWhen(false)] out string? reason)
{
try
{
using var stream = File.OpenRead(path);
reason = Validate(stream, 0);
}
catch (IOException ex)
{
reason = "Unable to read the file for validation: " + ex.Message;
}
catch (UnauthorizedAccessException ex)
{
reason = "Unable to read the file for validation: " + ex.Message;
}
return reason is null;
}
/// <summary>
/// Determines whether the SVG in the given stream is safe to rasterize.
/// </summary>
/// <param name="stream">The stream containing the SVG document.</param>
/// <param name="reason">When this method returns <c>false</c>, the reason the document was rejected.</param>
/// <returns><c>true</c> if the document is free of external references; otherwise <c>false</c>.</returns>
public static bool IsSafe(Stream stream, [NotNullWhen(false)] out string? reason)
{
reason = Validate(stream, 0);
return reason is null;
}
private static string? Validate(Stream stream, int depth)
{
try
{
using var reader = XmlReader.Create(stream, _scanSettings);
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.DocumentType:
{
var subset = reader.Value;
if (!string.IsNullOrEmpty(subset)
&& (subset.Contains("SYSTEM", StringComparison.OrdinalIgnoreCase)
|| subset.Contains("PUBLIC", StringComparison.OrdinalIgnoreCase)))
{
return "The document declares an external DTD entity";
}
break;
}
case XmlNodeType.Element when reader.HasAttributes:
{
for (var i = 0; i < reader.AttributeCount; i++)
{
reader.MoveToAttribute(i);
var isHref = reader.LocalName.Equals("href", StringComparison.OrdinalIgnoreCase);
var reason = isHref
? ValidateReference(reader.Value, depth, "href")
: ValidateCss(reader.Value, depth);
if (reason is not null)
{
return reason;
}
}
reader.MoveToElement();
break;
}
case XmlNodeType.Text:
case XmlNodeType.CDATA:
{
var reason = ValidateCss(reader.Value, depth);
if (reason is not null)
{
return reason;
}
break;
}
}
}
return null;
}
catch (XmlException ex)
{
// Malformed markup, a forbidden DTD construct or an unresolved external entity: refuse to render.
return "The document could not be safely parsed: " + ex.Message;
}
}
private static string? ValidateReference(ReadOnlySpan<char> value, int depth, string context)
{
var trimmed = value.Trim();
if (trimmed.IsEmpty || trimmed[0] == '#')
{
return null;
}
if (trimmed.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
{
return ValidateDataUri(trimmed, depth, context);
}
return "An external resource is referenced via " + context;
}
private static string? ValidateDataUri(ReadOnlySpan<char> dataUri, int depth, string context)
{
// "data:[<mediatype>][;base64],<payload>" (mirrors Svg.Model's data URI parsing).
var comma = dataUri.IndexOf(',');
if (comma < 0)
{
return "A malformed data URI is referenced via " + context;
}
var header = dataUri[5..comma];
var firstSeparator = header.IndexOf(';');
var mediaType = (firstSeparator < 0 ? header : header[..firstSeparator]).Trim();
// Only "image/svg+xml" is re-parsed as SVG by the renderer; any other type is treated as raster data.
if (!mediaType.Contains('/') || !mediaType.Equals("image/svg+xml", StringComparison.OrdinalIgnoreCase))
{
return null;
}
if (depth >= MaxDataUriDepth)
{
return "Nested data URIs exceed the allowed depth";
}
var lastSeparator = header.LastIndexOf(';');
var isBase64 = lastSeparator >= 0
&& header[(lastSeparator + 1)..].Trim().Equals("base64", StringComparison.OrdinalIgnoreCase);
var payload = dataUri[(comma + 1)..].Trim();
byte[]? buffer = null;
try
{
int length;
if (isBase64)
{
buffer = ArrayPool<byte>.Shared.Rent((payload.Length / 4 * 3) + 3);
if (!Convert.TryFromBase64Chars(payload, buffer, out length))
{
return "An undecodable data URI is referenced via " + context;
}
}
else
{
var unescaped = Uri.UnescapeDataString(payload.ToString());
buffer = ArrayPool<byte>.Shared.Rent(Encoding.UTF8.GetMaxByteCount(unescaped.Length));
length = Encoding.UTF8.GetBytes(unescaped, buffer);
}
if (length > 2 && buffer[0] == 0x1F && buffer[1] == 0x8B)
{
using var decompressed = Decompress(buffer, length);
return Validate(decompressed, depth + 1);
}
using var stream = new MemoryStream(buffer, 0, length, false);
return Validate(stream, depth + 1);
}
catch (FormatException ex)
{
return "An undecodable data URI is referenced via " + context + ": " + ex.Message;
}
catch (InvalidDataException ex)
{
return "An invalid compressed data URI is referenced via " + context + ": " + ex.Message;
}
finally
{
if (buffer is not null)
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
private static MemoryStream Decompress(byte[] compressed, int length)
{
using var input = new MemoryStream(compressed, 0, length, false);
using var gzip = new GZipStream(input, CompressionMode.Decompress);
var output = new MemoryStream();
var buffer = ArrayPool<byte>.Shared.Rent(DecompressBufferSize);
try
{
var total = 0;
int read;
while ((read = gzip.Read(buffer, 0, buffer.Length)) > 0)
{
total += read;
if (total > MaxDecompressedBytes)
{
throw new InvalidDataException("Compressed data URI exceeds the allowed size");
}
output.Write(buffer, 0, read);
}
}
catch
{
output.Dispose();
throw;
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
output.Position = 0;
return output;
}
private static string? ValidateCss(ReadOnlySpan<char> value, int depth)
{
if (value.IsEmpty)
{
return null;
}
var index = 0;
while (true)
{
var found = value[index..].IndexOf("url(", StringComparison.OrdinalIgnoreCase);
if (found < 0)
{
break;
}
var start = index + found + 4;
var close = value[start..].IndexOf(')');
if (close < 0)
{
break;
}
var target = value.Slice(start, close).Trim();
target = target.Trim('\'');
target = target.Trim('"').Trim();
var reason = ValidateReference(target, depth, "url()");
if (reason is not null)
{
return reason;
}
index = start + close + 1;
if (index >= value.Length)
{
break;
}
}
// Handle the bare "@import '...';" form (the "@import url(...)" form is covered above).
index = 0;
while (true)
{
var found = value[index..].IndexOf("@import", StringComparison.OrdinalIgnoreCase);
if (found < 0)
{
break;
}
var rest = value[(index + found + 7)..];
var quote = rest.IndexOfAny('\'', '"');
if (quote >= 0)
{
var afterQuote = rest[(quote + 1)..];
var end = afterQuote.IndexOfAny('\'', '"');
if (end >= 0)
{
var reason = ValidateReference(afterQuote[..end], depth, "@import");
if (reason is not null)
{
return reason;
}
}
}
index = index + found + 7;
if (index >= value.Length)
{
break;
}
}
return null;
}
}
|