1use contextual_encoder::{
10 for_cdata, for_css_string, for_css_url, for_html, for_html_attribute, for_html_content,
11 for_html_unquoted_attribute, for_java, for_javascript, for_javascript_attribute,
12 for_javascript_block, for_javascript_source, for_rust_byte_string, for_rust_char,
13 for_rust_string, for_uri_component, for_xml, for_xml11, for_xml11_attribute, for_xml11_content,
14 for_xml_attribute, for_xml_comment, for_xml_content,
15};
16
17fn main() {
18 let input = r#"<script>alert("xss")</script>"#;
19
20 println!("input: {input}");
21 println!();
22
23 println!("--- html ---");
29 println!(" for_html: {}", for_html(input));
30
31 println!(
33 " for_html_content: {}",
34 for_html_content(input)
35 );
36
37 println!(
39 " for_html_attribute: {}",
40 for_html_attribute(input)
41 );
42
43 println!(
45 " for_html_unquoted_attribute: {}",
46 for_html_unquoted_attribute(input)
47 );
48 println!();
49
50 println!("--- javascript ---");
52 println!(" for_javascript: {}", for_javascript(input));
53
54 println!(
56 " for_javascript_attribute: {}",
57 for_javascript_attribute(input)
58 );
59
60 println!(
62 " for_javascript_block: {}",
63 for_javascript_block(input)
64 );
65
66 println!(
68 " for_javascript_source: {}",
69 for_javascript_source(input)
70 );
71 println!();
72
73 println!("--- css ---");
75 println!(" for_css_string: {}", for_css_string(input));
76
77 println!(" for_css_url: {}", for_css_url(input));
79 println!();
80
81 println!("--- uri ---");
83 println!(
84 " for_uri_component: {}",
85 for_uri_component(input)
86 );
87 println!();
88
89 println!("--- xml 1.0 ---");
91 println!(" for_xml: {}", for_xml(input));
92 println!(" for_xml_content: {}", for_xml_content(input));
93 println!(
94 " for_xml_attribute: {}",
95 for_xml_attribute(input)
96 );
97
98 println!(" for_xml_comment: {}", for_xml_comment(input));
100 println!(" for_cdata: {}", for_cdata(input));
101 println!();
102
103 println!("--- xml 1.1 ---");
105 let xml11_input = "a\x01b<c>";
106 println!(" for_xml11: {}", for_xml11(xml11_input));
107 println!(
108 " for_xml11_content: {}",
109 for_xml11_content(xml11_input)
110 );
111 println!(
112 " for_xml11_attribute: {}",
113 for_xml11_attribute(xml11_input)
114 );
115 println!();
116
117 println!("--- java ---");
119 println!(" for_java: {}", for_java(input));
120 println!();
121
122 println!("--- rust ---");
124 println!(" for_rust_string: {}", for_rust_string(input));
125 println!(" for_rust_char: {}", for_rust_char(input));
126 println!(
127 " for_rust_byte_string: {}",
128 for_rust_byte_string(input)
129 );
130
131 let user_name = r#"Bob <img src=x onerror="alert(1)">"#;
136 let user_query = "hello world & goodbye";
137 let user_text = r#"hi from </script><script>alert(1)</script>"#;
138 let user_css_text = r#"hello "css" \ test"#;
139
140 println!("--- practical usage ---");
141
142 println!(r#" <p>{}</p>"#, for_html_content(user_name));
144
145 let href = format!("/search?q={}", for_uri_component(user_query));
149 println!(r#" <a href="{}">search</a>"#, for_html_attribute(&href),);
150
151 println!(
153 r#" <style>.msg::after {{ content: "{}"; }}</style>"#,
154 for_css_string(user_css_text),
155 );
156
157 println!(
159 r#" <button onclick="greet('{}');">hi</button>"#,
160 for_javascript_attribute(user_text),
161 );
162}