-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path137.java
More file actions
1425 lines (1346 loc) · 54.1 KB
/
Copy path137.java
File metadata and controls
1425 lines (1346 loc) · 54.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 Jeff Ichnowski
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above
// copyright notice, this list of conditions and the following
// disclaimer.
//
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// * Neither the name of the OWASP nor the names of its
// contributors may be used to endorse or promote products
// derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
package org.owasp.encoder;
import java.io.IOException;
import java.io.Writer;
import java.nio.CharBuffer;
import java.nio.charset.CoderResult;
/**
* Encode -- fluent interface for contextual encoding. Example usage in a JSP:
*
* <pre>
* <input value="<%=Encode.forHtml(value)%>" />
* </pre>
*
* <p>There are two versions of each contextual encoding method. The first
* takes a {@code String} argument and returns the encoded version as a
* {@code String}. The second version writes the encoded version directly
* to a {@code Writer}.</p>
*
* <p>Please make sure to read and understand the context that the method encodes
* for. Encoding for the incorrect context will likely lead to exposing a
* cross-site scripting vulnerability.</p>
*
* @author Jeff Ichnowski
*/
public final class Encode {
/** No instances. */
private Encode() {}
/**
* <p>Encodes for (X)HTML text content and text attributes. Since
* this method encodes for both contexts, it may be slightly less
* efficient to use this method over the methods targeted towards
* the specific contexts ({@link #forHtmlAttribute(String)} and
* {@link #forHtmlContent(String)}. In general this method should
* be preferred unless you are really concerned with saving a few
* bytes or are writing a framework that utilizes this
* package.</p>
*
* <b>Example JSP Usage</b>
* <pre>
* <div><%=Encode.forHtml(unsafeData)%></div>
*
* <input value="<%=Encode.forHtml(unsafeData)%>" />
* </pre>
*
* <table border="0" class="memberSummary" summary="Shows the input and results of encoding">
* <caption><b>Encoding Table</b></caption>
* <thead>
* <tr>
* <th align="left" class="colFirst">Input</th>
* <th align="left" class="colLast">Result</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td class="colFirst">{@code &}</td>
* <td class="colLast">{@code &}</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">{@code <}</td>
* <td class="colLast">{@code <}</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">{@code >}</td>
* <td class="colLast">{@code >}</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">{@code "}</td>
* <td class="colLast">{@code "}</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">{@code '}</td>
* <td class="colLast">{@code '}</td>
* </tr>
* </tbody>
* </table>
*
* <p><b>Additional Notes</b></p>
* <ul>
* <li>The encoding of the greater-than sign ({@code >}) is not
* strictly required, but is included for maximum
* compatibility.</li>
*
* <li>Numeric encoding is used for double-quote character ({@code
* "}) as it shorter than the also valid {@code "}.</li>
*
* <li>Carriage return (U+0D), line-feed (U+0A), horizontal tab
* (U+09) and space (U+20) are valid in quoted attributes and in
* block in an unescaped form.</li>
*
* <li>Surrogate pairs are passed through only if valid.</li>
*
* <li>Characters that are not <a
* href="http://www.w3.org/TR/REC-xml/#charsets">valid according
* to the XML specification</a> are replaced by a space character
* as they could lead to parsing errors. In particular only {@code #x9
* | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
* [#x10000-#x10FFFF]} are considered valid.</li>
* </ul>
*
* @param input the data to encode
* @return the data encoded for html.
*/
public static String forHtml(String input) {
return forXml(input);
}
/**
* See {@link #forHtml(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forHtml(Writer out, String input) throws IOException {
forXml(out, input);
}
/**
* <p>This method encodes for HTML text content. It does not escape
* quotation characters and is thus unsafe for use with
* HTML attributes. Use either forHtml or forHtmlAttribute for those
* methods.</p>
*
* <b>Example JSP Usage</b>
* <pre>
* <div><%=Encode.forHtmlContent(unsafeData)%></div>
* </pre>
* <table border="0" class="memberSummary" summary="Shows the input and results of encoding">
* <caption><b>Encoding Table</b></caption>
* <thead>
* <tr>
* <th align="left" class="colFirst">Input</th>
* <th align="left" class="colLast">Result</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td class="colFirst">{@code &}</td>
* <td class="colLast">{@code &}</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">{@code <}</td>
* <td class="colLast">{@code <}</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">{@code >}</td>
* <td class="colLast">{@code >}</td>
* </tr>
* </tbody>
* </table>
*
* <p><b>Additional Notes</b></p>
* <ul>
* <li>Single-quote character ({@code '}) and double-quote
* character ({@code "}) do not require encoding in HTML
* blocks, unlike other HTML contexts.</li>
*
* <li>The encoding of the greater-than sign ({@code >}) is not
* strictly required, but is included for maximum
* compatibility.</li>
*
* <li>Carriage return (U+0D), line-feed (U+0A), horizontal tab
* (U+09) and space (U+20) are valid in quoted attributes and in
* block in an unescaped form.</li>
*
* <li>Surrogate pairs are passed through only if valid.</li>
*
* <li>Characters that are not <a
* href="http://www.w3.org/TR/REC-xml/#charsets">valid according
* to the XML specification</a> are replaced by a space character
* as they could lead to parsing errors. In particular only {@code #x9
* | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
* [#x10000-#x10FFFF]} are considered valid.</li>
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forHtmlContent(String input) {
return forXmlContent(input);
}
/**
* See {@link #forHtmlContent(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forHtmlContent(Writer out, String input)
throws IOException
{
forXmlContent(out, input);
}
/**
* <p>This method encodes for HTML text attributes.</p>
*
* <b>Example JSP Usage</b>
* <pre>
* <div><%=Encode.forHtmlAttribute(unsafeData)%></div>
* </pre>
*
* <table border="0" class="memberSummary" summary="Shows the input and results of encoding">
* <caption><b>Encoding Table</b></caption>
* <thead>
* <tr>
* <th align="left" class="colFirst">Input</th>
* <th align="left" class="colLast">Result</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td class="colFirst">{@code &}</td>
* <td class="colLast">{@code &}</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">{@code <}</td>
* <td class="colLast">{@code <}</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">{@code "}</td>
* <td class="colLast">{@code "}</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">{@code '}</td>
* <td class="colLast">{@code '}</td>
* </tr>
* </tbody>
* </table>
*
* <p><b>Additional Notes</b></p>
* <ul>
* <li>Both the single-quote character ({@code '}) and the
* double-quote character ({@code "}) are encoded so this is safe
* for HTML attributes with either enclosing character.</li>
*
* <li>The encoding of the greater-than sign ({@code >}) is not
* required for attributes.</li>
*
* <li>Numeric encoding is used for double-quote character ({@code
* "}) as it shorter than the also valid {@code "}.</li>
*
* <li>Carriage return (U+0D), line-feed (U+0A), horizontal tab
* (U+09) and space (U+20) are valid in quoted attributes and in
* block in an unescaped form.</li>
*
* <li>Surrogate pairs are passed through only if valid.</li>
*
* <li>Characters that are not <a
* href="http://www.w3.org/TR/REC-xml/#charsets">valid according
* to the XML specification</a> are replaced by a space character
* as they could lead to parsing errors. In particular only {@code #x9
* | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
* [#x10000-#x10FFFF]} are considered valid.</li>
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forHtmlAttribute(String input) {
return forXmlAttribute(input);
}
/**
* See {@link #forHtmlAttribute(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forHtmlAttribute(Writer out, String input)
throws IOException
{
forXmlAttribute(out, input);
}
/**
* <p>Encodes for unquoted HTML attribute values. {@link
* #forHtml(String)} or {@link #forHtmlAttribute(String)} should
* usually be preferred over this method as quoted attributes are
* XHTML compliant.</p>
*
* <p>When using this method, the caller is not required to
* provide quotes around the attribute (since it is encoded for
* such context). The caller should make sure that the attribute
* value does not abut unsafe characters--and thus should usually
* err on the side of including a space character after the
* value.</p>
*
* <p>Use of this method is discouraged as quoted attributes are
* generally more compatible and safer. Also note, that no
* attempt has been made to optimize this encoding, though it is
* still probably faster than other encoding libraries.</p>
*
* <b>Example JSP Usage</b>
* <pre>
* <input value=<%=Encode.forHtmlUnquotedAttribute(input)%> >
* </pre>
*
* <table border="0" class="memberSummary" summary="Shows the input and results of encoding">
* <caption><b>Encoding Table</b></caption>
* <thead>
* <tr>
* <th align="left" class="colFirst">Input</th>
* <th align="left" class="colLast">Result</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td class="colFirst">{@code U+0009} (horizontal tab)</td>
* <td class="colLast">{@code 	}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code U+000A} (line feed)</td>
* <td class="colLast">{@code }</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code U+000C} (form feed)</td>
* <td class="colLast">{@code }</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code U+000D} (carriage return)</td>
* <td class="colLast">{@code }</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code U+0020} (space)</td>
* <td class="colLast">{@code  }</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code &}</td>
* <td class="colLast">{@code &}</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code <}</td>
* <td class="colLast">{@code <}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code >}</td>
* <td class="colLast">{@code >}</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code "}</td>
* <td class="colLast">{@code "}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code '}</td>
* <td class="colLast">{@code '}</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code /}</td>
* <td class="colLast">{@code /}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code =}</td>
* <td class="colLast">{@code =}</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code `}</td>
* <td class="colLast">{@code `}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code U+0085} (next line)</td>
* <td class="colLast">{@code …}</td></tr>
* <tr class="altColor">
* <td class="colFirst">{@code U+2028} (line separator)</td>
* <td class="colLast">{@code 
}</td></tr>
* <tr class="rowColor">
* <td class="colFirst">{@code U+2029} (paragraph separator)</td>
* <td class="colLast">{@code 
}</td></tr>
* </tbody>
* </table>
*
* <p><b>Additional Notes</b></p>
* <ul>
* <li>The following characters are <i>not</i> encoded:
* {@code 0-9, a-z, A-Z}, {@code !}, {@code
* #}, {@code $}, {@code %},
* {@code (}, {@code )}, {@code
* *}, {@code +}, {@code ,},
* {@code -}, {@code .}, {@code
* [}, {@code \}, {@code ]},
* {@code ^}, {@code _}, {@code
* }}.</li>
*
* <li>Surrogate pairs are passed through only if valid. Invalid
* surrogate pairs are replaced by a hyphen (-).</li>
*
* <li>Characters in the C0 and C1 control blocks and not
* otherwise listed above are considered invalid and replaced by a
* hyphen (-) character.</li>
*
* <li>Unicode "non-characters" are replaced by hyphens (-).</li>
* </ul>
*
* @param input the attribute value to be encoded.
* @return the attribute value encoded for unquoted attribute
* context.
*/
public static String forHtmlUnquotedAttribute(String input) {
return encode(Encoders.HTML_UNQUOTED_ATTRIBUTE_ENCODER, input);
}
/**
* See {@link #forHtmlUnquotedAttribute(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forHtmlUnquotedAttribute(Writer out, String input)
throws IOException
{
encode(Encoders.HTML_UNQUOTED_ATTRIBUTE_ENCODER, out, input);
}
// HTML comment encoding is not currently supported because
// of the number of vendor-specific sequences that would need
// to be handled (e.g. "<!--[if IE]-->"
// public static String forHtmlComment(String input) {
// // only alphanumeric and space, everything else becomes a space
//
// // HTML comment context needs to avoid browser extensions
// // such as "<!--[if IE]-->"
// throw new UnsupportedOperationException();
// }
/**
* Encodes for CSS strings. The context must be surrounded by quotation
* characters. It is safe for use in both style blocks and attributes in
* HTML.
*
* <b>Example JSP Usage</b>
* <pre>
* <div style="background: url('<=Encode.forCssString(...)%>');">
*
* <style type="text/css">
* background: url('<%=Encode.forCssString(...)%>');
* </style>
* </pre>
*
* <b>Encoding Notes</b>
* <ul>
*
* <li>The following characters are encoded using hexidecimal
* encodings: {@code U+0000} - {@code U+001f},
* {@code "},
* {@code '},
* {@code \},
* {@code <},
* {@code &},
* {@code (},
* {@code )},
* {@code /},
* {@code >},
* {@code U+007f},
* line separator ({@code U+2028}),
* paragraph separator ({@code U+2029}).</li>
*
* <li>Any character requiring encoding is encoded as {@code \xxx}
* where {@code xxx} is the shortest hexidecimal representation of
* its Unicode code point (after decoding surrogate pairs if
* necessary). This encoding is never zero padded. Thus, for
* example, the tab character is encoded as {@code \9}, not {@code
* \0009}.</li>
*
* <li>The encoder looks ahead 1 character in the input and
* appends a space to an encoding to avoid the next character
* becoming part of the hexidecimal encoded sequence. Thus
* “{@code '1}” is encoded as “{@code \27
* 1}”, and not as “{@code \271}”. If a space
* is not necessary, it is not included, thus “{@code
* 'x}” is encoded as “{@code \27x}”, and not as
* “{@code \27 x}”.</li>
*
* <li>Surrogate pairs are passed through only if valid. Invalid
* surrogate pairs are replaced by an underscore (_).</li>
*
* <li>Unicode "non-characters" are replaced by underscores (_).</li>
*
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forCssString(String input) {
// need to watch out for CSS expressions
return encode(Encoders.CSS_STRING_ENCODER, input);
}
/**
* See {@link #forCssString(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forCssString(Writer out, String input)
throws IOException
{
encode(Encoders.CSS_STRING_ENCODER, out, input);
}
/**
* Encodes for CSS URL contexts. The context must be surrounded by {@code "url("}
* and {@code ")"}. It is safe for use in both style blocks and attributes in HTML.
* Note: this does not do any checking on the quality or safety of the URL
* itself. The caller should insure that the URL is safe for embedding
* (e.g. input validation) by other means.
*
* <b>Example JSP Usage</b>
* <pre>
* <div style="background:url(<=Encode.forCssUrl(...)%>);">
*
* <style type="text/css">
* background: url(<%=Encode.forCssUrl(...)%>);
* </style>
* </pre>
* <b>Encoding Notes</b>
* <ul>
*
* <li>The following characters are encoded using hexidecimal
* encodings: {@code U+0000} - {@code U+001f},
* {@code "},
* {@code '},
* {@code \},
* {@code <},
* {@code &},
* {@code /},
* {@code >},
* {@code U+007f},
* line separator ({@code U+2028}),
* paragraph separator ({@code U+2029}).</li>
*
* <li>Any character requiring encoding is encoded as {@code \xxx}
* where {@code xxx} is the shortest hexidecimal representation of
* its Unicode code point (after decoding surrogate pairs if
* necessary). This encoding is never zero padded. Thus, for
* example, the tab character is encoded as {@code \9}, not {@code
* \0009}.</li>
*
* <li>The encoder looks ahead 1 character in the input and
* appends a space to an encoding to avoid the next character
* becoming part of the hexidecimal encoded sequence. Thus
* “{@code '1}” is encoded as “{@code \27
* 1}”, and not as “{@code \271}”. If a space
* is not necessary, it is not included, thus “{@code
* 'x}” is encoded as “{@code \27x}”, and not as
* “{@code \27 x}”.</li>
*
* <li>Surrogate pairs are passed through only if valid. Invalid
* surrogate pairs are replaced by an underscore (_).</li>
*
* <li>Unicode "non-characters" are replaced by underscores (_).</li>
*
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forCssUrl(String input) {
return encode(Encoders.CSS_URL_ENCODER, input);
}
/**
* See {@link #forCssUrl(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forCssUrl(Writer out, String input)
throws IOException
{
encode(Encoders.CSS_URL_ENCODER, out, input);
}
/**
* <p>Performs percent-encoding of a URL according to RFC 3986. The provided
* URL is assumed to a valid URL. This method does not do any checking on
* the quality or safety of the URL itself. In many applications it may
* be better to use {@link java.net.URI} instead. Note: this is a
* particularly dangerous context to put untrusted content in, as for
* example a "javascript:" URL provided by a malicious user would be
* "properly" escaped, and still execute.</p>
*
* <b>Encoding Table</b>
* <p>The following characters are <i>not</i> encoded:</p>
* <pre>
* U+20: ! # $ & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; = ?
* U+40: @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ ] _
* U+60: a b c d e f g h i j k l m n o p q r s t u v w x y z ~
* </pre>
*
* <b>Encoding Notes</b>
* <ul>
*
* <li>The single-quote character({@code '}) <b>is not encoded</b>.</li>
*
* <li>This encoding is not intended to be used standalone. The
* output should be encoded to the target context. For example:
* {@code <a
* href="<%=Encode.forHtmlAttribute(Encode.forUri(uri))%>">...</a>}.
* (Note, the single-quote character ({@code '}) is not
* encoded.)</li>
*
* <li>URL encoding is an encoding for bytes, not unicode. The
* input string is thus first encoded as a sequence of UTF-8
* byte. The bytes are then encoded as {@code %xx} where {@code
* xx} is the two-digit hexidecimal representation of the
* byte. (The implementation does this as one step for
* performance.)</li>
*
* <li>Surrogate pairs are first decoded to a Unicode code point
* before encoding as UTF-8.</li>
*
* <li>Invalid characters (e.g. partial or invalid surrogate
* pairs), are replaced with a hyphen ({@code -}) character.</li>
*
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
@Deprecated public static String forUri(String input) {
return encode(Encoders.URI_ENCODER, input);
}
/**
* See {@link #forUri(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*
* @deprecated There is never a need to encode a complete URI with this form of encoding.
*/
@Deprecated public static void forUri(Writer out, String input)
throws IOException
{
encode(Encoders.URI_ENCODER, out, input);
}
/**
* Performs percent-encoding for a component of a URI, such as a query
* parameter name or value, path or query-string. In particular this
* method insures that special characters in the component do not get
* interpreted as part of another component.
*
* <pre>
* <a href="http://www.owasp.org/<%=Encode.forUriComponent(...)%>?query#fragment">
*
* <a href="/search?value=<%=Encode.forUriComponent(...)%>&order=1#top">
* </pre>
*
* <b>Encoding Table</b>
* <p>The following characters are <i>not</i> encoded:</p>
* <pre>
* U+20: - . 0 1 2 3 4 5 6 7 8 9
* U+40: @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z _
* U+60: a b c d e f g h i j k l m n o p q r s t u v w x y z ~
* </pre>
*
* <b>Encoding Notes</b>
* <ul>
*
* <li>Unlike {@link #forUri(String)} this method is safe to be
* used in most containing contexts, including: HTML/XML, CSS,
* and JavaScript contexts.</li>
*
* <li>URL encoding is an encoding for bytes, not unicode. The
* input string is thus first encoded as a sequence of UTF-8
* byte. The bytes are then encoded as {@code %xx} where {@code
* xx} is the two-digit hexidecimal representation of the
* byte. (The implementation does this as one step for
* performance.)</li>
*
* <li>Surrogate pairs are first decoded to a Unicode code point
* before encoding as UTF-8.</li>
*
* <li>Invalid characters (e.g. partial or invalid surrogate
* pairs), are replaced with a hyphen ({@code -}) character.</li>
*
* </ul>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forUriComponent(String input) {
return encode(Encoders.URI_COMPONENT_ENCODER, input);
}
/**
* See {@link #forUriComponent(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forUriComponent(Writer out, String input)
throws IOException
{
encode(Encoders.URI_COMPONENT_ENCODER, out, input);
}
/**
* Encoder for XML and XHTML. See {@link #forHtml(String)} for a
* description of the encoding and context.
*
* @see #forHtml(String)
* @param input the input to encode
* @return the encoded result
*/
public static String forXml(String input) {
return encode(Encoders.XML_ENCODER, input);
}
/**
* See {@link #forXml(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forXml(Writer out, String input)
throws IOException
{
encode(Encoders.XML_ENCODER, out, input);
}
/**
* Encoder for XML and XHTML text content. See {@link
* #forHtmlContent(String)} for description of encoding and
* context.
*
* @see #forHtmlContent(String)
* @param input the input to encode
* @return the encoded result
*/
public static String forXmlContent(String input) {
return encode(Encoders.XML_CONTENT_ENCODER, input);
}
/**
* See {@link #forXmlContent(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forXmlContent(Writer out, String input)
throws IOException
{
encode(Encoders.XML_CONTENT_ENCODER, out, input);
}
/**
* Encoder for XML and XHTML attribute content. See {@link
* #forHtmlAttribute(String)} for description of encoding and
* context.
*
* @see #forHtmlAttribute(String)
* @param input the input to encode
* @return the encoded result
*/
public static String forXmlAttribute(String input) {
return encode(Encoders.XML_ATTRIBUTE_ENCODER, input);
}
/**
* See {@link #forXmlAttribute(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forXmlAttribute(Writer out, String input)
throws IOException
{
encode(Encoders.XML_ATTRIBUTE_ENCODER, out, input);
}
/**
* Encoder for XML comments. <strong>NOT FOR USE WITH
* (X)HTML CONTEXTS.</strong> (X)HTML comments may be interpreted by
* browsers as something other than a comment, typically in vendor
* specific extensions (e.g. {@code <--if[IE]-->}).
* For (X)HTML it is recommend that unsafe content never be included
* in a comment.
*
* <p>The caller must provide the comment start and end sequences.</p>
*
* <p>This method replaces all invalid XML characters with spaces,
* and replaces the "--" sequence (which is invalid in XML comments)
* with "-~" (hyphen-tilde). <b>This encoding behavior may change
* in future releases.</b> If the comments need to be decoded, the
* caller will need to come up with their own encode/decode system.</p>
*
* <pre>
* out.println("<?xml version='1.0'?>");
* out.println("<data>");
* out.println("<!-- "+Encode.forXmlComment(comment)+" -->");
* out.println("</data>");
* </pre>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forXmlComment(String input) {
return encode(Encoders.XML_COMMENT_ENCODER, input);
}
/**
* See {@link #forXmlComment(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forXmlComment(Writer out, String input)
throws IOException
{
encode(Encoders.XML_COMMENT_ENCODER, out, input);
}
/**
* Encodes data for an XML CDATA section. On the chance that the input
* contains a terminating {@code "]]>"}, it will be replaced by
* {@code "]]>]]<![CDATA[>"}.
* As with all XML contexts, characters that are invalid according to the
* XML specification will be replaced by a space character. Caller must
* provide the CDATA section boundaries.
*
* <pre>
* <xml-data><![CDATA[<%=Encode.forCDATA(...)%>]]></xml-data>
* </pre>
*
* @param input the input to encode
* @return the encoded result
*/
public static String forCDATA(String input) {
return encode(Encoders.CDATA_ENCODER, input);
}
/**
* See {@link #forCDATA(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forCDATA(Writer out, String input)
throws IOException
{
encode(Encoders.CDATA_ENCODER, out, input);
}
/**
* Encodes for a Java string. This method will use "\b", "\t", "\r", "\f",
* "\n", "\"", "\'", "\\", octal and unicode escapes. Valid surrogate
* pairing is not checked. The caller must provide the enclosing quotation
* characters. This method is useful for when writing code generators and
* outputting debug messages.
*
* <pre>
* out.println("public class Hello {");
* out.println(" public static void main(String[] args) {");
* out.println(" System.out.println(\"" + Encode.forJava(message) + "\");");
* out.println(" }");
* out.println("}");
* </pre>
*
* @param input the input to encode
* @return the input encoded for java strings.
*/
public static String forJava(String input) {
return encode(Encoders.JAVA_ENCODER, input);
}
/**
* See {@link #forJava(String)} for description of encoding. This
* version writes directly to a Writer without an intervening string.
*
* @param out where to write encoded output
* @param input the input string to encode
* @throws IOException if thrown by writer
*/
public static void forJava(Writer out, String input)
throws IOException
{
encode(Encoders.JAVA_ENCODER, out, input);
}
/**
* <p>Encodes for a JavaScript string. It is safe for use in HTML
* script attributes (such as {@code onclick}), script
* blocks, JSON files, and JavaScript source. The caller MUST
* provide the surrounding quotation characters for the string.
* Since this performs additional encoding so it can work in all
* of the JavaScript contexts listed, it may be slightly less
* efficient than using one of the methods targetted to a specific
* JavaScript context ({@link #forJavaScriptAttribute(String)},
* {@link #forJavaScriptBlock}, {@link #forJavaScriptSource}).
* Unless you are interested in saving a few bytes of output or
* are writing a framework on top of this library, it is recommend
* that you use this method over the others.</p>
*
* <b>Example JSP Usage:</b>
* <pre>
* <button onclick="alert('<%=Encode.forJavaScript(data)%>');">
* <script type="text/javascript">
* var data = "<%=Encode.forJavaScript(data)%>";
* </script>
* </pre>
*
* <table cellspacing="1" class="memberSummary" cellpadding="1" border="0">
* <caption><b>Encoding Description</b></caption>
* <thead>
* <tr>
* <th align="left" colspan="2" class="colFirst">Input Character</th>
* <th align="left" class="colLast">Encoded Result</th>
* <th align="left" class="colLast">Notes</th>
* </tr>
* </thead>
* <tbody>
* <tr class="altColor">
* <td class="colFirst">U+0008</td><td><i>BS</i></td>
* <td class="colLast"><code>\b</code></td>
* <td class="colLast">Backspace character</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">U+0009</td><td><i>HT</i></td>
* <td class="colLast"><code>\t</code></td>
* <td class="colLast">Horizontal tab character</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">U+000A</td><td><i>LF</i></td>
* <td class="colLast"><code>\n</code></td>
* <td class="colLast">Line feed character</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">U+000C</td><td><i>FF</i></td>
* <td class="colLast"><code>\f</code></td>
* <td class="colLast">Form feed character</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">U+000D</td><td><i>CR</i></td>
* <td class="colLast"><code>\r</code></td>
* <td class="colLast">Carriage return character</td>
* </tr>
* <tr class="rowColor">
* <td class="colFirst">U+0022</td><td><code>"</code></td>
* <td class="colLast"><code>\x22</code></td>
* <td class="colLast">The encoding <code>\"</code> is not used here because
* it is not safe for use in HTML attributes. (In HTML
* attributes, it would also be correct to use
* "\&quot;".)</td>
* </tr>
* <tr class="altColor">
* <td class="colFirst">U+0026</td><td><code>&</code></td>
* <td class="colLast"><code>\x26</code></td>