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_string, for_sql, for_sql_backslash, for_uri_component, for_xml,
14 for_xml11, for_xml11_attribute, for_xml11_content, for_xml_attribute, for_xml_comment,
15 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!(" for_rust_char: {}", for_rust_char(input));
135 println!(
136 " for_rust_byte_string: {}",
137 for_rust_byte_string(input)
138 );
139 println!();
140
141 println!("--- json ---");
143 println!(" for_json: {}", for_json(input));
144 println!();
145
146 println!("--- sql ---");
148 println!(" for_sql: {}", for_sql(input));
149 println!(
150 " for_sql_backslash: {}",
151 for_sql_backslash(input)
152 );
153
154 let user_name = r#"Bob <img src=x onerror="alert(1)">"#;
159 let user_query = "hello world & goodbye";
160 let user_text = r#"hi from </script><script>alert(1)</script>"#;
161 let user_css_text = r#"hello "css" \ test"#;
162
163 println!("--- practical usage ---");
164
165 println!(r#" <p>{}</p>"#, for_html_content(user_name));
167
168 let href = format!("/search?q={}", for_uri_component(user_query));
172 println!(r#" <a href="{}">search</a>"#, for_html_attribute(&href),);
173
174 println!(
176 r#" <style>.msg::after {{ content: "{}"; }}</style>"#,
177 for_css_string(user_css_text),
178 );
179
180 println!(
182 r#" <button onclick="greet('{}');">hi</button>"#,
183 for_javascript_attribute(user_text),
184 );
185}