1use contextual_encoder::{
10 for_cdata, for_css_string, for_css_url, for_form_urlencoded, for_html, for_html_attribute,
11 for_html_content, for_html_unquoted_attribute, for_javascript, for_javascript_attribute,
12 for_javascript_block, for_javascript_source, for_js_template, for_json, for_rust_byte_string,
13 for_rust_char, for_rust_char_checked, for_rust_string, for_sql, for_sql_backslash,
14 for_uri_component, for_xml, for_xml11, for_xml11_attribute, for_xml11_content,
15 for_xml_attribute, for_xml_comment, for_xml_content,
16};
17
18fn main() {
19 let input = r#"<script>alert("xss")</script>"#;
20
21 println!("input: {input}");
22 println!();
23
24 println!("--- html ---");
30 println!(" for_html: {}", for_html(input));
31
32 println!(
34 " for_html_content: {}",
35 for_html_content(input)
36 );
37
38 println!(
40 " for_html_attribute: {}",
41 for_html_attribute(input)
42 );
43
44 println!(
46 " for_html_unquoted_attribute: {}",
47 for_html_unquoted_attribute(input)
48 );
49 println!();
50
51 println!("--- javascript ---");
53 println!(" for_javascript: {}", for_javascript(input));
54
55 println!(
57 " for_javascript_attribute: {}",
58 for_javascript_attribute(input)
59 );
60
61 println!(
63 " for_javascript_block: {}",
64 for_javascript_block(input)
65 );
66
67 println!(
69 " for_javascript_source: {}",
70 for_javascript_source(input)
71 );
72
73 let template_input = r#"`Hello ${name}`, welcome</script>"#;
75 println!(
76 " for_js_template: {}",
77 for_js_template(template_input)
78 );
79 println!();
80
81 println!("--- css ---");
83 println!(" for_css_string: {}", for_css_string(input));
84
85 println!(" for_css_url: {}", for_css_url(input));
87 println!();
88
89 println!("--- uri ---");
91 println!(
92 " for_uri_component: {}",
93 for_uri_component(input)
94 );
95
96 println!(
98 " for_form_urlencoded: {}",
99 for_form_urlencoded(input)
100 );
101 println!();
102
103 println!("--- xml 1.0 ---");
105 println!(" for_xml: {}", for_xml(input));
106 println!(" for_xml_content: {}", for_xml_content(input));
107 println!(
108 " for_xml_attribute: {}",
109 for_xml_attribute(input)
110 );
111
112 println!(" for_xml_comment: {}", for_xml_comment(input));
114 println!(" for_cdata: {}", for_cdata(input));
115 println!();
116
117 println!("--- xml 1.1 ---");
119 let xml11_input = "a\x01b<c>";
120 println!(" for_xml11: {}", for_xml11(xml11_input));
121 println!(
122 " for_xml11_content: {}",
123 for_xml11_content(xml11_input)
124 );
125 println!(
126 " for_xml11_attribute: {}",
127 for_xml11_attribute(xml11_input)
128 );
129 println!();
130
131 println!("--- rust ---");
133 println!(" for_rust_string: {}", for_rust_string(input));
134 println!(
135 " for_rust_byte_string: {}",
136 for_rust_byte_string(input)
137 );
138 println!(" for_rust_char(\"'\"): {}", for_rust_char("'"));
140 println!(
141 " for_rust_char_checked(input): {:?}",
142 for_rust_char_checked(input)
143 );
144 println!();
145
146 println!("--- json ---");
148 println!(" for_json: {}", for_json(input));
149 println!();
150
151 println!("--- sql ---");
153 println!(" for_sql: {}", for_sql(input));
154 println!(
155 " for_sql_backslash: {}",
156 for_sql_backslash(input)
157 );
158
159 let user_name = r#"Bob <img src=x onerror="alert(1)">"#;
164 let user_query = "hello world & goodbye";
165 let user_text = r#"hi from </script><script>alert(1)</script>"#;
166 let user_css_text = r#"hello "css" \ test"#;
167
168 println!("--- practical usage ---");
169
170 println!(r#" <p>{}</p>"#, for_html_content(user_name));
172
173 let href = format!("/search?q={}", for_uri_component(user_query));
177 println!(r#" <a href="{}">search</a>"#, for_html_attribute(&href),);
178
179 println!(
181 r#" <style>.msg::after {{ content: "{}"; }}</style>"#,
182 for_css_string(user_css_text),
183 );
184
185 println!(
187 r#" <button onclick="greet('{}');">hi</button>"#,
188 for_javascript_attribute(user_text),
189 );
190}