Author: mikesamuel
Date: Sun Feb 15 12:51:16 2009
New Revision: 3258

Modified:
   trunk/src/com/google/caja/parser/js/Operation.java
   trunk/tests/com/google/caja/parser/js/parsertest6.js
   trunk/tests/com/google/caja/parser/js/rendergolden6.txt
   trunk/tests/com/google/caja/render/JsMinimalPrinterTest.java
   trunk/tests/com/google/caja/render/JsPrettyPrinterTest.java

Log:
Issue 17063  http://codereview.appspot.com/17063
Issue 967: embedded rendering mode should parenthesize post-decrement inside comparisons

The code
  x-- > 3
is valid javascript.

HTML 4 says of the grammar for comments:
    White space is not permitted between the markup declaration open
    delimiter("<!") and the comment open delimiter ("--"), but is
    permitted between the comment close delimiter ("--") and the
    markup declaration close delimiter (">").

So the -- > in the javascript snippet above could end an HTML comment, thus
defeating embedded mode guarantees.  This can be solved by rendering it as
  (x--) > 3.

This has to be done for post-decrement ops as the left hand argument to the
following operators >, >=, >>, >>>, >>=, >>>=.

This may be a security issue if clients use embedding and browsers
implement escaping text spans using the same grammar as HTML4 comments.

HTML5 has a tighter syntax for comments, and so a strict HTML5 parser would
 not lose embedding.

[email protected]


Modified: trunk/src/com/google/caja/parser/js/Operation.java
==============================================================================
--- trunk/src/com/google/caja/parser/js/Operation.java  (original)
+++ trunk/src/com/google/caja/parser/js/Operation.java Sun Feb 15 12:51:16 2009
@@ -274,6 +274,22 @@
           return true;
         }
       }
+      // Make sure that the sequence -- > never appears in embedded mode
+      // rendered source, since according to HTML4,
+      //   White space is not permitted between the markup declaration open
+      //   delimiter("<!") and the comment open delimiter ("--"), but is
+      //   permitted between the comment close delimiter ("--") and the
+      //   markup declaration close delimiter (">").
+      if (childOp == Operator.POST_DECREMENT) {
+        switch (op) {
+          case ASSIGN_RSH: case ASSIGN_USH:
+          case RSHIFT: case RUSHIFT:
+          case GREATER_THAN: case GREATER_EQUALS:
+            return true;
+          default:
+            break;
+        }
+      }
     }

     // Parenthesize based on associativity and precedence

Modified: trunk/tests/com/google/caja/parser/js/parsertest6.js
==============================================================================
--- trunk/tests/com/google/caja/parser/js/parsertest6.js        (original)
+++ trunk/tests/com/google/caja/parser/js/parsertest6.js Sun Feb 15 12:51:16 2009
@@ -24,3 +24,8 @@

// Make sure we don't treat string literals in Object constructors differently.
 var o = { '</script>': 0, ']]>': 1, '<!DOCTYPE': 2 };
+
+// Make sure that comment markers and escaping text spans can't appear in
+// output.  c should be parenthesized since HTML4 allows space between the
+// -- and >
+var p = a <!-- b && c --> (d);

Modified: trunk/tests/com/google/caja/parser/js/rendergolden6.txt
==============================================================================
--- trunk/tests/com/google/caja/parser/js/rendergolden6.txt     (original)
+++ trunk/tests/com/google/caja/parser/js/rendergolden6.txt Sun Feb 15 12:51:16 2009
@@ -15,4 +15,5 @@
     ']]\x3e': 1,
     '\x3c!DOCTYPE': 2
   };
+  var p = a < ! (--b) && (c--) > d;
 }

Modified: trunk/tests/com/google/caja/render/JsMinimalPrinterTest.java
==============================================================================
--- trunk/tests/com/google/caja/render/JsMinimalPrinterTest.java        
(original)
+++ trunk/tests/com/google/caja/render/JsMinimalPrinterTest.java Sun Feb 15 12:51:16 2009
@@ -75,7 +75,7 @@
     // Make sure -->, </script, and ]]> don't show up in rendered output.
     // Preventing these in strings is handled separately.
     assertRendered(
-        "{i-- >j,k< /script>/,[[0]] >0/ / / *x;}",
+        "{(i--)>j,k< /script>/,[[0]] >0/ / / *x;}",
         "i-->j, k</script>/, [[0]]>0 / / / * x;");
   }


Modified: trunk/tests/com/google/caja/render/JsPrettyPrinterTest.java
==============================================================================
--- trunk/tests/com/google/caja/render/JsPrettyPrinterTest.java (original)
+++ trunk/tests/com/google/caja/render/JsPrettyPrinterTest.java Sun Feb 15 12:51:16 2009
@@ -105,7 +105,7 @@
     // Make sure -->, </script, and ]]> don't show up in rendered output.
     // Preventing these in strings is handled separately.
     assertRendered(
-        "{\n  i-- > j, k < /script>/, [ [ 0 ] ] > 0;\n}",
+        "{\n  (i--) > j, k < /script>/, [ [ 0 ] ] > 0;\n}",
         "i-->j, k</script>/, [[0]]>0;");
   }

Reply via email to