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_javascript, for_javascript_attribute, for_javascript_block,
12 for_javascript_source, for_js_template, for_json, for_rust_byte_string, for_rust_char,
13 for_rust_string, for_sql, for_sql_backslash, for_uri_component, for_xml, for_xml11,
14 for_xml11_attribute, for_xml11_content, 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
72 let template_input = r#"`Hello ${name}`, welcome</script>"#;
74 println!(
75 " for_js_template: {}",
76 for_js_template(template_input)
77 );
78 println!();
79
80 println!("--- css ---");
82 println!(" for_css_string: {}", for_css_string(input));
83
84 println!(" for_css_url: {}", for_css_url(input));
86 println!();
87
88 println!("--- uri ---");
90 println!(
91 " for_uri_component: {}",
92 for_uri_component(input)
93 );
94 println!();
95
96 println!("--- xml 1.0 ---");
98 println!(" for_xml: {}", for_xml(input));
99 println!(" for_xml_content: {}", for_xml_content(input));
100 println!(
101 " for_xml_attribute: {}",
102 for_xml_attribute(input)
103 );
104
105 println!(" for_xml_comment: {}", for_xml_comment(input));
107 println!(" for_cdata: {}", for_cdata(input));
108 println!();
109
110 println!("--- xml 1.1 ---");
112 let xml11_input = "a\x01b<c>";
113 println!(" for_xml11: {}", for_xml11(xml11_input));
114 println!(
115 " for_xml11_content: {}",
116 for_xml11_content(xml11_input)
117 );
118 println!(
119 " for_xml11_attribute: {}",
120 for_xml11_attribute(xml11_input)
121 );
122 println!();
123
124 println!("--- rust ---");
126 println!(" for_rust_string: {}", for_rust_string(input));
127 println!(" for_rust_char: {}", for_rust_char(input));
128 println!(
129 " for_rust_byte_string: {}",
130 for_rust_byte_string(input)
131 );
132 println!();
133
134 println!("--- json ---");
136 println!(" for_json: {}", for_json(input));
137 println!();
138
139 println!("--- sql ---");
141 println!(" for_sql: {}", for_sql(input));
142 println!(
143 " for_sql_backslash: {}",
144 for_sql_backslash(input)
145 );
146
147 let user_name = r#"Bob <img src=x onerror="alert(1)">"#;
152 let user_query = "hello world & goodbye";
153 let user_text = r#"hi from </script><script>alert(1)</script>"#;
154 let user_css_text = r#"hello "css" \ test"#;
155
156 println!("--- practical usage ---");
157
158 println!(r#" <p>{}</p>"#, for_html_content(user_name));
160
161 let href = format!("/search?q={}", for_uri_component(user_query));
165 println!(r#" <a href="{}">search</a>"#, for_html_attribute(&href),);
166
167 println!(
169 r#" <style>.msg::after {{ content: "{}"; }}</style>"#,
170 for_css_string(user_css_text),
171 );
172
173 println!(
175 r#" <button onclick="greet('{}');">hi</button>"#,
176 for_javascript_attribute(user_text),
177 );
178}