feedparser-rs 0.5.3

High-performance RSS/Atom/JSON Feed parser
Documentation
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! SSRF Protection Tests for URL Resolution.
//!
//! These tests verify that malicious xml:base attributes cannot be used
//! to create Server-Side Request Forgery (SSRF) attacks.
#![allow(missing_docs, clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use feedparser_rs::parse;

#[test]
fn test_ssrf_localhost_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://localhost/">
        <icon>admin/config</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.starts_with("http://localhost"),
        "SSRF to localhost should be blocked, got: {icon}"
    );
    // Should return original relative URL instead
    assert_eq!(icon, "admin/config");
}

#[test]
fn test_ssrf_localhost_domain_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://localhost:8080/">
        <logo>secret/api/key</logo>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let logo = feed.feed.logo.as_deref().unwrap();
    assert!(
        !logo.contains("localhost"),
        "SSRF to localhost domain should be blocked, got: {logo}"
    );
}

#[test]
fn test_ssrf_loopback_ip_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://127.0.0.1/">
        <icon>config.php</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("127.0.0.1"),
        "SSRF to loopback IP should be blocked, got: {icon}"
    );
}

#[test]
fn test_ssrf_private_ip_192_168_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://192.168.1.1/">
        <icon>config.php</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("192.168"),
        "SSRF to private IP 192.168.x.x should be blocked, got: {icon}"
    );
}

#[test]
fn test_ssrf_private_ip_10_x_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://10.0.0.1/">
        <icon>admin/backup.sql</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("10.0.0.1"),
        "SSRF to private IP 10.x.x.x should be blocked, got: {icon}"
    );
}

#[test]
fn test_ssrf_private_ip_172_16_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://172.20.0.1/">
        <icon>internal/service</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("172.20"),
        "SSRF to private IP 172.16-31.x.x should be blocked, got: {icon}"
    );
}

#[test]
fn test_ssrf_metadata_endpoint_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom"
          xml:base="http://169.254.169.254/latest/">
        <icon>meta-data/iam/security-credentials/</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("169.254.169.254"),
        "SSRF to AWS metadata endpoint should be blocked, got: {icon}"
    );
}

#[test]
fn test_ssrf_ipv6_loopback_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://[::1]/">
        <icon>admin/config</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("[::1]"),
        "SSRF to IPv6 loopback should be blocked, got: {icon}"
    );
}

#[test]
fn test_safe_public_urls_work() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://example.com/">
        <icon>images/icon.png</icon>
        <logo>images/logo.png</logo>
    </feed>"#;

    let feed = parse(xml).unwrap();
    assert_eq!(
        feed.feed.icon.as_deref(),
        Some("http://example.com/images/icon.png")
    );
    assert_eq!(
        feed.feed.logo.as_deref(),
        Some("http://example.com/images/logo.png")
    );
}

#[test]
fn test_safe_https_urls_work() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://secure.example.com/">
        <icon>icon.png</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    assert_eq!(
        feed.feed.icon.as_deref(),
        Some("https://secure.example.com/icon.png")
    );
}

#[test]
fn test_rss_enclosure_ssrf_blocked() {
    // In RSS, the channel link serves as the base URL
    // Test with malicious channel link
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0">
        <channel>
            <title>Test</title>
            <link>http://192.168.1.1/</link>
            <item>
                <title>Test Item</title>
                <enclosure url="backup.sql" type="application/sql" length="1000" />
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    let enclosure = &feed.entries[0].enclosures[0];
    assert!(
        !enclosure.url.contains("192.168"),
        "SSRF in RSS enclosure should be blocked, got: {}",
        enclosure.url
    );
    // Should return original relative URL when base is malicious
    assert_eq!(enclosure.url, "backup.sql");
}

#[test]
fn test_rss_link_ssrf_blocked() {
    // In RSS, the channel link serves as the base URL
    let xml = br#"<?xml version="1.0"?>
    <rss version="2.0">
        <channel>
            <title>Test</title>
            <link>http://localhost/</link>
            <item>
                <title>Test Item</title>
                <link>admin/config.php</link>
            </item>
        </channel>
    </rss>"#;

    let feed = parse(xml).unwrap();
    let link = feed.entries[0].link.as_deref().unwrap();
    assert!(
        !link.contains("localhost"),
        "SSRF in RSS item link should be blocked, got: {link}"
    );
    // Should return original relative URL when base is malicious
    assert_eq!(link, "admin/config.php");
}

#[test]
fn test_atom_link_ssrf_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://10.0.0.1/">
        <id>test</id>
        <title>Test</title>
        <updated>2024-01-01T00:00:00Z</updated>
        <link href="admin/panel" rel="alternate" />
    </feed>"#;

    let feed = parse(xml).unwrap();
    let link_href = &feed.feed.links[0].href;
    assert!(
        !link_href.contains("10.0.0.1"),
        "SSRF in Atom link should be blocked, got: {link_href}"
    );
    assert_eq!(link_href, "admin/panel");
}

#[test]
fn test_nested_xml_base_ssrf_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://example.com/">
        <entry xml:base="http://192.168.1.100/">
            <id>test</id>
            <title>Test</title>
            <updated>2024-01-01T00:00:00Z</updated>
            <link href="secret/data" />
        </entry>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let entry_link = &feed.entries[0].links[0].href;
    assert!(
        !entry_link.contains("192.168"),
        "SSRF via nested xml:base should be blocked, got: {entry_link}"
    );
}

#[test]
fn test_absolute_urls_bypass_malicious_base() {
    // Even with malicious base, absolute URLs should work
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://localhost/">
        <icon>http://cdn.example.com/icon.png</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    assert_eq!(
        feed.feed.icon.as_deref(),
        Some("http://cdn.example.com/icon.png"),
        "Absolute URLs should override base"
    );
}

#[test]
fn test_relative_urls_without_base_unchanged() {
    // Without xml:base, relative URLs should remain relative
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
        <icon>images/icon.png</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    assert_eq!(
        feed.feed.icon.as_deref(),
        Some("images/icon.png"),
        "Relative URLs without base should remain unchanged"
    );
}

#[test]
fn test_special_schemes_unaffected() {
    // mailto: and tel: should pass through
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://localhost/">
        <link href="mailto:admin@example.com" />
    </feed>"#;

    let feed = parse(xml).unwrap();
    assert_eq!(
        feed.feed.links[0].href, "mailto:admin@example.com",
        "Special schemes should not be affected by base"
    );
}

#[test]
fn test_file_scheme_protection() {
    // file:// schemes in xml:base should not resolve because resolve_url
    // only works with http/https bases. Non-HTTP schemes result in the
    // original href being returned unchanged.
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="file:///etc/">
        <icon>passwd</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    // file:// base URL parsing fails in url::Url::parse, so original href is returned
    assert_eq!(icon, "passwd", "file:// base should not resolve");
    assert!(
        !icon.starts_with("file://"),
        "file:// scheme should not be in result"
    );
}

#[test]
fn test_google_metadata_domain_blocked() {
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom"
          xml:base="http://metadata.google.internal/">
        <icon>computeMetadata/v1/instance/service-accounts/default/token</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.contains("metadata.google.internal"),
        "Google metadata endpoint should be blocked, got: {icon}"
    );
}

#[test]
fn test_absolute_malicious_url_in_href_blocked() {
    // If href itself is an absolute malicious URL, it should be blocked
    // even when base URL is safe (or when there's no base URL)
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="http://example.com/">
        <icon>http://localhost/admin/config</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap_or("");
    assert!(
        !icon.contains("localhost"),
        "Absolute malicious URL in href should be blocked, got: {icon}"
    );
    // Should return empty string for dangerous absolute URLs
    assert!(
        icon.is_empty(),
        "Dangerous absolute URL should result in empty string, got: {icon}"
    );
}

#[test]
fn test_absolute_malicious_private_ip_in_href_blocked() {
    // Private IP in href should be blocked
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
        <link href="http://192.168.1.1/internal/api" rel="alternate" />
    </feed>"#;

    let feed = parse(xml).unwrap();
    let link_href = &feed.feed.links[0].href;
    assert!(
        !link_href.contains("192.168"),
        "Absolute malicious private IP in href should be blocked, got: {link_href}"
    );
    // Should return empty string for dangerous absolute URLs
    assert!(
        link_href.is_empty(),
        "Dangerous absolute URL should result in empty string, got: {link_href}"
    );
}

#[test]
fn test_case_insensitive_scheme_bypass_blocked() {
    // Uppercase schemes should also be blocked (RFC 3986 - schemes are case-insensitive)
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="FILE:///etc/">
        <icon>passwd</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.to_lowercase().starts_with("file://"),
        "Uppercase FILE:// scheme should be blocked, got: {icon}"
    );
}

#[test]
fn test_mixed_case_javascript_scheme_blocked() {
    // Mixed case javascript: should be blocked
    let xml = br#"<?xml version="1.0"?>
    <feed xmlns="http://www.w3.org/2005/Atom" xml:base="JaVaScRiPt:alert(1)//">
        <icon>test</icon>
    </feed>"#;

    let feed = parse(xml).unwrap();
    let icon = feed.feed.icon.as_deref().unwrap();
    assert!(
        !icon.to_lowercase().contains("javascript"),
        "Mixed case javascript: scheme should be blocked, got: {icon}"
    );
}