Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Replace control characters that XML 1.0 forbids with U+FFFD when writing channels. [`#204`](https://github.com/rust-syndication/rss/pull/204)

## 2.1.2 - 2026-09-10

- Escape XML special characters in namespace URIs when writing channels. [`#196`](https://github.com/rust-syndication/rss/pull/196)
Expand Down
10 changes: 5 additions & 5 deletions src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::extension::ExtensionMap;
use crate::image::Image;
use crate::item::{content_prefix, Item};
use crate::textinput::TextInput;
use crate::toxml::{ToXml, WriterExt};
use crate::toxml::{ToXml, WriterExt, XmlSafeWriter};
use crate::util::{decode, element_text, skip};

/// Represents the channel of an RSS feed.
Expand Down Expand Up @@ -1119,7 +1119,7 @@ impl Channel {
}
}

fn write<W: Write>(&self, mut writer: Writer<W>) -> Result<W, Error> {
fn write<W: Write>(&self, mut writer: Writer<XmlSafeWriter<W>>) -> Result<W, Error> {
writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("utf-8"), None)))?;

let name = "rss";
Expand All @@ -1142,7 +1142,7 @@ impl Channel {

writer.write_event(Event::End(BytesEnd::new(name)))?;

Ok(writer.into_inner())
Ok(writer.into_inner().into_inner())
}

/// Attempt to write the RSS channel as XML to a writer.
Expand All @@ -1155,7 +1155,7 @@ impl Channel {
/// channel.write_to(writer).unwrap();
/// ```
pub fn write_to<W: Write>(&self, writer: W) -> Result<W, Error> {
self.write(::quick_xml::Writer::new(writer))
self.write(::quick_xml::Writer::new(XmlSafeWriter(writer)))
}

/// Attempt to write the RSS channel as pretty XML to a writer.
Expand All @@ -1174,7 +1174,7 @@ impl Channel {
indent_size: usize,
) -> Result<W, Error> {
self.write(::quick_xml::Writer::new_with_indent(
writer,
XmlSafeWriter(writer),
indent_char,
indent_size,
))
Expand Down
30 changes: 29 additions & 1 deletion src/toxml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,40 @@
// it under the terms of the MIT License and/or Apache 2.0 License.

use std::collections::BTreeMap;
use std::io::Write;
use std::io::{self, Write};

use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Error as XmlError;
use quick_xml::Writer;

/// Writes U+FFFD for each control character XML 1.0 forbids.
pub(crate) struct XmlSafeWriter<W>(pub(crate) W);

impl<W> XmlSafeWriter<W> {
pub(crate) fn into_inner(self) -> W {
self.0
}
}

impl<W: Write> Write for XmlSafeWriter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let forbidden = |b: &u8| matches!(b, 0x00..=0x08 | 0x0B | 0x0C | 0x0E..=0x1F);
let mut chunks = buf.split(forbidden);
if let Some(first) = chunks.next() {
self.0.write_all(first)?;
}
for chunk in chunks {
self.0.write_all("\u{FFFD}".as_bytes())?;
self.0.write_all(chunk)?;
}
Ok(buf.len())
}

fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}

pub trait ToXml {
fn to_xml<W: Write>(&self, writer: &mut Writer<W>) -> Result<(), XmlError>;

Expand Down
31 changes: 31 additions & 0 deletions tests/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,34 @@ fn test_atom_write_item() {
</rss>"#
);
}

#[test]
fn test_control_characters_replaced() {
let channel = ChannelBuilder::default()
.title("Chan\u{1E}nel\u{8}")
.items(vec![ItemBuilder::default()
.title("a\u{0}b\tc\nd".to_string())
.content("<p>x\u{1F}y</p>".to_string())
.enclosure(
EnclosureBuilder::default()
.url("http://example.com/a\u{B}.mp3")
.build(),
)
.build()])
.build();

let output = channel.to_string();
assert!(
!output.contains(|c| matches!(c, '\0'..='\u{8}' | '\u{B}' | '\u{C}' | '\u{E}'..='\u{1F}'))
);

let parsed = output.parse::<Channel>().expect("failed to parse xml");
assert_eq!(parsed.title(), "Chan\u{FFFD}nel\u{FFFD}");
let item = &parsed.items()[0];
assert_eq!(item.title(), Some("a\u{FFFD}b\tc\nd"));
assert_eq!(item.content(), Some("<p>x\u{FFFD}y</p>"));
assert_eq!(
item.enclosure().unwrap().url(),
"http://example.com/a\u{FFFD}.mp3"
);
}
Loading