File tree Expand file tree Collapse file tree
java10-shim/src/main/java/org/owasp/shim
owasp-java-html-sanitizer/src Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -62,6 +62,6 @@ final class ForJava9AndLater extends Java8Shim {
6262 }
6363
6464 @ Override public <T > Set <T > setCopyOf (Collection <? extends T > c ) {
65- return Set . copyOf ( c );
65+ return Collections . unmodifiableSet ( new LinkedHashSet <>( c ) );
6666 }
6767}
Original file line number Diff line number Diff line change 3333import java .util .HashMap ;
3434import java .util .HashSet ;
3535import java .util .LinkedHashMap ;
36+ import java .util .LinkedHashSet ;
3637import java .util .List ;
3738import java .util .Map ;
3839import java .util .Set ;
@@ -428,7 +429,7 @@ public HtmlPolicyBuilder requireRelNofollowOnLinks() {
428429 public HtmlPolicyBuilder requireRelsOnLinks (String ... linkValues ) {
429430 this .invalidateCompiledState ();
430431 if (this .extraRelsForLinks == null ) {
431- this .extraRelsForLinks = new HashSet <>();
432+ this .extraRelsForLinks = new LinkedHashSet <>();
432433 }
433434 for (String linkValue : linkValues ) {
434435 linkValue = HtmlLexer .canonicalKeywordAttributeValue (linkValue );
@@ -1112,8 +1113,8 @@ static final class JoinRelsOnLinksPolicies
11121113
11131114 public JoinableElementPolicy join (
11141115 Iterable <? extends JoinableElementPolicy > toJoin ) {
1115- Set <String > extra = new HashSet <>();
1116- Set <String > skip = new HashSet <>();
1116+ Set <String > extra = new LinkedHashSet <>();
1117+ Set <String > skip = new LinkedHashSet <>();
11171118 for (JoinableElementPolicy ep : toJoin ) {
11181119 RelsOnLinksPolicy p = (RelsOnLinksPolicy ) ep ;
11191120 extra .addAll (p .extra );
Original file line number Diff line number Diff line change @@ -252,6 +252,30 @@ public static final void testLinks() {
252252 s .sanitize ("<a name=\" header\" id=\" header\" >Header text</a>" ));
253253 }
254254
255+ @ Test
256+ public static final void testLinksRelAttributeAdditionsOrder () {
257+ // Issue 336.
258+ PolicyFactory pf = Sanitizers .LINKS .and (
259+ new HtmlPolicyBuilder ()
260+ .allowElements ("a" )
261+ .requireRelsOnLinks ("noopener" , "noreferrer" )
262+ .toFactory ());
263+
264+ assertEquals (
265+ "<a href=\" foo.html\" rel=\" nofollow noopener noreferrer\" >Link text</a>" ,
266+ pf .sanitize ("<a href=\" foo.html\" >Link text</a>" ));
267+
268+ pf = Sanitizers .LINKS .and (
269+ new HtmlPolicyBuilder ()
270+ .allowElements ("a" )
271+ .requireRelsOnLinks ("noreferrer" , "noopener" )
272+ .toFactory ());
273+
274+ assertEquals (
275+ "<a href=\" foo.html\" rel=\" nofollow noreferrer noopener\" >Link text</a>" ,
276+ pf .sanitize ("<a href=\" foo.html\" >Link text</a>" ));
277+ }
278+
255279 @ Test
256280 public static final void testExplicitlyAllowedProtocolsAreCaseInsensitive () {
257281 // Issue 24.
@@ -552,7 +576,7 @@ public static final void testStyleGlobally() {
552576 String want = "<h1 style=\" color:green\" >This is some green text</h1>" ;
553577 assertEquals (want , policyBuilder .sanitize (input ));
554578 }
555-
579+
556580 static int fac (int n ) {
557581 int ifac = 1 ;
558582 for (int i = 1 ; i <= n ; ++i ) {
You can’t perform that action at this time.
0 commit comments