Author: mikesamuel
Date: Thu Dec 11 11:05:21 2008
New Revision: 3125

Modified:
    trunk/src/com/google/caja/plugin/domita.js
    trunk/tests/com/google/caja/plugin/domita_test.html
    trunk/tests/com/google/caja/plugin/domita_test_untrusted.html

Log:
Issue 8896  http://codereview.appspot.com/8896
Add window.getComputedStyle to DOMita

This adds window.getComputedStyle to DOMita for a subset of CSS
properties needed by libraries for doing layout and containment
calculations.

See also discussion in BUG 533

BUG=773

R=ihab.awad


Modified: trunk/src/com/google/caja/plugin/domita.js
==============================================================================
--- trunk/src/com/google/caja/plugin/domita.js  (original)
+++ trunk/src/com/google/caja/plugin/domita.js  Thu Dec 11 11:05:21 2008
@@ -2325,6 +2325,42 @@
      }
      TameStyle.prototype.toString = function () { return '[Fake Style]'; };

+    /**
+     * Set of properties accessible on computed style.
+     * This list is a conservative one compiled by looking at what  
prototype.js
+     * needs to be able to do visibility, containment, and layout  
calculations.
+     * If expanded, it should not allow an attacker to probe the user's  
history
+     * as described at https://bugzilla.mozilla.org/show_bug.cgi?id=147777
+     */
+    var COMPUTED_STYLE_WHITELIST = {
+      'display': true,
+      'filter': true,
+      'float': true,
+      'height': true,
+      'opacity': true,
+      'overflow': true,
+      'position': true,
+      'visibility': true,
+      'width': true
+    };
+    function TameComputedStyle(style) {
+      this.style___ = style;
+    }
+    cajita.forOwnKeys(
+        COMPUTED_STYLE_WHITELIST,
+        ___.func(
+            function (propertyName, _) {
+              ___.useGetHandler(
+                  TameComputedStyle.prototype, propertyName,
+                  function () {
+                    if (!this.style___) { return void 0; }
+                    return String(this.style___[propertyName] || '');
+                  });
+            }));
+    TameComputedStyle.prototype.toString = function () {
+      return '[Fake Computed Style]';
+    };
+
      nodeClasses.XMLHttpRequest = domitaModules.TameXMLHttpRequest(
          domitaModules.XMLHttpRequestCtor(
              window.XMLHttpRequest,
@@ -2432,6 +2468,17 @@
        userAgent: 'Caja/1.0'
        });

+    /**
+     * Set of allowed pseudo elements as described at
+     * http://www.w3.org/TR/CSS2/selector.html#q20
+     */
+    var PSEUDO_ELEMENT_WHITELIST = {
+        // after and before disallowed since they can leak information  
about
+        // arbitrary ancestor nodes.
+        'first-letter': true,
+        'first-line': true
+    };
+
      // See  
http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#window
  
for the full API.
      // TODO(mikesamuel): This implements only the parts needed by  
prototype.
      // The rest can be added on an as-needed basis as long as DOMado rules  
are
@@ -2466,6 +2513,34 @@
        dispatchEvent: ___.frozenFunc(
            function (evt) {
              // TODO(ihab.awad): Implement
+          }),
+      /** A partial implementation of getComputedStyle. */
+      getComputedStyle: ___.frozenFunc(
+          // Pseudo elements are suffixes like :first-line which constrain  
to
+          // a portion of the element's content as defined at
+          // http://www.w3.org/TR/CSS2/selector.html#q20
+          function (tameElement, pseudoElement) {
+            cajita.guard(tameNodeTrademark, tameElement);
+            pseudoElement = (pseudoElement === null || pseudoElement ===  
void 0)
+                ? void 0 : String(pseudoElement).toLowerCase();
+            if (pseudoElement !== void 0
+                 
&& !PSEUDO_ELEMENT_WHITELIST.hasOwnProperty(pseudoElement)) {
+              throw new Error('Bad pseudo class ' + pseudoElement);
+            }
+            // No need to check editable since computed styles are  
readonly.
+
+            var rawNode = tameElement.node___;
+            if (rawNode.currentStyle && pseudoElement === void 0) {
+              return new TameComputedStyle(rawNode.currentStyle);
+            } else if (window.getComputedStyle) {
+              var rawStyleNode = window.getComputedStyle(
+                  tameElement.node___, pseudoElement);
+              return new TameComputedStyle(rawStyleNode);
+            } else {
+              throw new Error(
+                  'Computed style not available for pseudo element '
+                  + pseudoElement);
+            }
            })

        // NOT PROVIDED

Modified: trunk/tests/com/google/caja/plugin/domita_test.html
==============================================================================
--- trunk/tests/com/google/caja/plugin/domita_test.html (original)
+++ trunk/tests/com/google/caja/plugin/domita_test.html Thu Dec 11 11:05:21  
2008
@@ -148,7 +148,7 @@
                    return letter.toUpperCase();
                  })];
            } else if (window.getComputedStyle) {
-            return document.defaultView.getComputedStyle(node, null)
+            return window.getComputedStyle(node, null)
                  .getPropertyValue(styleProp);
            }
          })

Modified: trunk/tests/com/google/caja/plugin/domita_test_untrusted.html
==============================================================================
--- trunk/tests/com/google/caja/plugin/domita_test_untrusted.html       
(original)
+++ trunk/tests/com/google/caja/plugin/domita_test_untrusted.html       Thu Dec 
 
11 11:05:21 2008
@@ -17,6 +17,7 @@
  <style type="text/css">
    .testcontainer { background: yellow; border: 2px solid yellow }
    .testcontainer.passed { background: #c0ffc0; border: 2px solid #c0ffc0 }
+  .invisible { display: none }
  </style>

  <p class="testcontainer" id="test-timeout-and-interval">
@@ -52,7 +53,8 @@
  </p>

  <p class="testcontainer" id="test-nonexistent-style-property">
-  <span id="test-nonexistent-style-property-label">Hide me</span><br>
+  <span id="test-nonexistent-style-property-label">I ain't got no  
style</span>
+  <br>
  </p>

  <div id="test-get-element-by-id" class="testcontainer"></div>
@@ -150,6 +152,13 @@

  <div class="testcontainer" id="test-xhr-async">XHR async</div>

+<div class="testcontainer" id="test-computed-style">
+  <div id="test-computed-style-block">Block</div>
+  <span id="test-computed-style-inline">Inline</span>
+  <div id="test-computed-style-fakie" style="display: inline">???</div>
+  <div id="test-computed-style-invisible" class="invisible">Not here</div>
+</div>
+
  <p class="testcontainer" id="test-document-element">Test Document  
Element</p>

  <p class="testcontainer" id="test-document-body-appendChild"
@@ -1075,6 +1084,33 @@
      asyncFlag = event.target.readyState;
    };
    xhr.send();
+});
+
+jsunitRegister('testComputedStyle',
+               function testComputedStyle() {
+  var block = document.getElementById('test-computed-style-block');
+  var inline = document.getElementById('test-computed-style-inline');
+  var fakie = document.getElementById('test-computed-style-fakie');
+  var invisible = document.getElementById('test-computed-style-invisible');
+  assertEquals('block', window.getComputedStyle(block, null).display);
+  assertEquals('inline', window.getComputedStyle(inline, null).display);
+  assertEquals('inline', window.getComputedStyle(fakie, null).display);
+  var NO_RESULT = {};
+  var firstLineDisplay = NO_RESULT;
+  try {
+    firstLineDisplay =  
window.getComputedStyle(fakie, 'first-line').display;
+  } catch (ex) {
+    // Not supported on all browsers
+  }
+  if (firstLineDisplay !== NO_RESULT) {
+    assertEquals(firstLineDisplay, 'inline');
+  }
+  assertEquals('none', window.getComputedStyle(invisible, null).display);
+  // Make sure we don't expose information that we haven't yet sorted out  
how
+  // to safely expose.
+  assertEquals(undefined, window.getComputedStyle(block, null).color);
+
+  pass('test-computed-style');
  });

  jsunitRegister('testDocumentElement',

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to 
http://groups.google.com/group/google-caja-discuss
To unsubscribe, email [email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to