From 020281d932c566476222e6c825ada3affff239a6 Mon Sep 17 00:00:00 2001
From: Andy Staudacher
world
", + $safe_string->unescaped()); + } + + public function toString_escapes_for_html_test() { + $safe_string = new SafeString("helloworld
"); + $this->assert_equal("hello <p>world</p>", + $safe_string); + } + + public function toString_for_safe_string_test() { + $safe_string = new SafeString("helloworld
"); + $safe_string->mark_html_safe(); + $this->assert_equal("helloworld
", + $safe_string); + } + + public function for_html_test() { + $safe_string = new SafeString("helloworld
"); + $this->assert_equal("hello <p>world</p>", + $safe_string->for_html()); + } + + public function safestring_of_safestring_test() { + $safe_string = new SafeString("helloworld
"); + $safe_string_2 = new SafeString($safe_string); + $this->assert_true($safe_string_2 instanceof SafeString); + $raw_string = $safe_string_2->unescaped(); + $this->assert_false(is_object($raw_string)); + $this->assert_equal("helloworld
", $raw_string); + $this->assert_equal("hello <p>world</p>", $safe_string_2); + } + + public function for_js_test() { + $safe_string = new SafeString('"Foo\'s bar"'); + $js_string = $safe_string->for_js(); + $this->assert_equal('\\"Foo<\\/em>\\\'s bar\\"', + $js_string); + } + + public function string_safestring_equality_test() { + $safe_string = new SafeString("helloworld
"); + $this->assert_equal("helloworld
", + $safe_string->unescaped()); + $escaped_string = "hello <p>world</p>"; + $this->assert_equal($escaped_string, $safe_string); + + $this->assert_true($escaped_string == $safe_string); + $this->assert_false($escaped_string === $safe_string); + $this->assert_false("meow" == $safe_string); + } + + public function of_test() { + $safe_string = SafeString::of("helloworld
"); + $this->assert_equal("helloworld
", $safe_string->unescaped()); + } + + public function of_safe_html_test() { + $safe_string = SafeString::of("helloworld
")->mark_html_safe(); + $this->assert_equal("helloworld
", $safe_string->for_html()); + } + + public function of_fluid_api_test() { + $escaped_string = SafeString::of("Foo's bar")->for_js(); + $this->assert_equal("Foo\\'s bar", $escaped_string); + } + + public function safestring_of_safestring_preserves_safe_status_test() { + $safe_string = SafeString::of("hello'sworld
")->mark_html_safe(); + $safe_string_2 = new SafeString($safe_string); + $this->assert_equal("hello'sworld
", $safe_string_2); + $this->assert_equal("hello\\'sworld<\\/p>", $safe_string_2->for_js()); + } + + public function safestring_of_safestring_preserves_html_safe_status_test() { + $safe_string = SafeString::of("hello's
world
") + ->mark_html_safe(); + $safe_string_2 = new SafeString($safe_string); + $this->assert_equal("hello'sworld
", $safe_string_2); + $this->assert_equal("hello\\'sworld<\\/p>", $safe_string_2->for_js()); + } + + public function safestring_of_safestring_safe_status_override_test() { + $safe_string = new SafeString("hello
world
"); + $safe_string_2 = SafeString::of($safe_string)->mark_html_safe(); + $this->assert_equal("helloworld
", $safe_string_2); + } +} -- cgit v1.2.3 From 7adb9ea2e3a42e1c5472024a1699912ae26eacb3 Mon Sep 17 00:00:00 2001 From: Andy Staudacher+ * ; + * + *+ * @return the string escaped for use in HTML attributes. + */ + function for_html_attr() { + $string = (string) $this->for_html(); + return strtr($string, + array("'"=>"'", + '"'=>'"')); + } + /** * Safe for use HTML (purified HTML) * diff --git a/modules/gallery/tests/SafeString_Test.php b/modules/gallery/tests/SafeString_Test.php index cdae3e99..73d82c34 100644 --- a/modules/gallery/tests/SafeString_Test.php +++ b/modules/gallery/tests/SafeString_Test.php @@ -18,13 +18,6 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class SafeString_Test extends Unit_Test_Case { - public function p_clean_returns_safestring_instance_test() { - $safe_string = p::clean("hello
world
"); - $this->assert_true($safe_string instanceof SafeString); - $this->assert_equal("helloworld
", - $safe_string->unescaped()); - } - public function toString_escapes_for_html_test() { $safe_string = new SafeString("helloworld
"); $this->assert_equal("hello <p>world</p>", @@ -61,6 +54,20 @@ class SafeString_Test extends Unit_Test_Case { $js_string); } + public function for_html_attr_test() { + $safe_string = new SafeString('"Foo\'s bar"'); + $attr_string = $safe_string->for_html_attr(); + $this->assert_equal('"<em>Foo</em>'s bar"', + $attr_string); + } + + public function for_html_attr_with_safe_html_test() { + $safe_string = SafeString::of('"Foo\'s bar"')->mark_html_safe(); + $attr_string = $safe_string->for_html_attr(); + $this->assert_equal('"Foo's bar"', + $attr_string); + } + public function string_safestring_equality_test() { $safe_string = new SafeString("helloworld
"); $this->assert_equal("helloworld
", -- cgit v1.2.3 From a10063ff68cf5988297dcad889384ab2080c3850 Mon Sep 17 00:00:00 2001 From: Andy Staudacherworld
"); - $safe_string->mark_html_safe(); + $safe_string = SafeString::of_safe_html("helloworld
"); $this->assert_equal("helloworld
", $safe_string); } @@ -62,7 +61,7 @@ class SafeString_Test extends Unit_Test_Case { } public function for_html_attr_with_safe_html_test() { - $safe_string = SafeString::of('"Foo\'s bar"')->mark_html_safe(); + $safe_string = SafeString::of_safe_html('"Foo\'s bar"'); $attr_string = $safe_string->for_html_attr(); $this->assert_equal('"Foo's bar"', $attr_string); @@ -86,25 +85,29 @@ class SafeString_Test extends Unit_Test_Case { } public function of_safe_html_test() { - $safe_string = SafeString::of("helloworld
")->mark_html_safe(); + $safe_string = SafeString::of_safe_html("helloworld
"); $this->assert_equal("helloworld
", $safe_string->for_html()); } + public function purify_test() { + $safe_string = SafeString::purify("helloworld
"); + $this->assert_equal("helloworld
", $safe_string); + } + public function of_fluid_api_test() { $escaped_string = SafeString::of("Foo's bar")->for_js(); $this->assert_equal("Foo\\'s bar", $escaped_string); } public function safestring_of_safestring_preserves_safe_status_test() { - $safe_string = SafeString::of("hello'sworld
")->mark_html_safe(); + $safe_string = SafeString::of_safe_html("hello'sworld
"); $safe_string_2 = new SafeString($safe_string); $this->assert_equal("hello'sworld
", $safe_string_2); $this->assert_equal("hello\\'sworld<\\/p>", $safe_string_2->for_js()); } public function safestring_of_safestring_preserves_html_safe_status_test() { - $safe_string = SafeString::of("hello's
world
") - ->mark_html_safe(); + $safe_string = SafeString::of_safe_html("hello'sworld
"); $safe_string_2 = new SafeString($safe_string); $this->assert_equal("hello'sworld
", $safe_string_2); $this->assert_equal("hello\\'sworld<\\/p>", $safe_string_2->for_js()); @@ -112,7 +115,7 @@ class SafeString_Test extends Unit_Test_Case { public function safestring_of_safestring_safe_status_override_test() { $safe_string = new SafeString("hello
world
"); - $safe_string_2 = SafeString::of($safe_string)->mark_html_safe(); + $safe_string_2 = SafeString::of_safe_html($safe_string); $this->assert_equal("helloworld
", $safe_string_2); } } diff --git a/modules/gallery/tests/Xss_Security_Test.php b/modules/gallery/tests/Xss_Security_Test.php index e0e5bb86..fd596c69 100644 --- a/modules/gallery/tests/Xss_Security_Test.php +++ b/modules/gallery/tests/Xss_Security_Test.php @@ -110,10 +110,13 @@ class Xss_Security_Test extends Unit_Test_Case { } else if ($token[1] == "SafeString") { // Looking for SafeString::of(... if (self::_token_matches(array(T_DOUBLE_COLON, "::"), $tokens, $token_number + 1) && - self::_token_matches(array(T_STRING, "of"), $tokens, $token_number + 2) && + self::_token_matches(array(T_STRING), $tokens, $token_number + 2) && + in_array($tokens[$token_number + 2][1], array("of", "of_safe_html", "purify")) && self::_token_matches("(", $tokens, $token_number + 3)) { $frame->is_safestring(true); - $frame->expr_append("::of("); + + $method = $tokens[$token_number + 2][1]; + $frame->expr_append("::$method("); $token_number += 3; $token = $tokens[$token_number]; -- cgit v1.2.3 From beb711d6a0fedac0d4ca3b9bae162a6ce9d6cdeb Mon Sep 17 00:00:00 2001 From: Andy Staudacher
*
*
*/
- static function clean_js($string) {
+ static function js_string($string) {
return SafeString::of($string)->for_js();
}
diff --git a/modules/gallery/libraries/SafeString.php b/modules/gallery/libraries/SafeString.php
index 9614a213..0767a665 100644
--- a/modules/gallery/libraries/SafeString.php
+++ b/modules/gallery/libraries/SafeString.php
@@ -92,17 +92,17 @@ class SafeString_Core {
}
/**
- * Safe for use in JavaScript.
+ * Safe for use as JavaScript string.
*
* Example:
*
*
* @return the string escaped for use in JavaScript.
*/
function for_js() {
- return self::_escape_for_js($this->_raw_string);
+ return json_encode((string) $this->_raw_string);
}
/**
@@ -152,14 +152,6 @@ class SafeString_Core {
return html::specialchars($dirty_html);
}
- // Escapes special chars (quotes, backslash, etc.) with a backslash sequence.
- private static function _escape_for_js($string) {
- // From Smarty plugins/modifier.escape.php
- // Might want to be stricter here.
- return strtr($string,
- array('\\'=>'\\\\',"'"=>"\\'",'"'=>'\\"',"\r"=>'\\r',"\n"=>'\\n',''=>'<\/'));
- }
-
// Purifies the string, removing any potentially malicious or unsafe HTML / JavaScript.
private static function _purify_for_html($dirty_html) {
if (empty(self::$_purifier)) {
diff --git a/modules/gallery/tests/Html_Helper_Test.php b/modules/gallery/tests/Html_Helper_Test.php
index a9903256..f5ce7fa4 100644
--- a/modules/gallery/tests/Html_Helper_Test.php
+++ b/modules/gallery/tests/Html_Helper_Test.php
@@ -40,9 +40,9 @@ class Html_Helper_Test extends Unit_Test_Case {
$safe_string_2);
}
- public function clean_js_test() {
- $string = html::clean_js("hello's world
"); - $this->assert_equal("hello\\'sworld<\\/p>", + public function js_string_test() { + $string = html::js_string("hello's
world
"); + $this->assert_equal('"hello\'sworld<\\/p>"',
$string);
}
diff --git a/modules/gallery/tests/SafeString_Test.php b/modules/gallery/tests/SafeString_Test.php
index 0fc7f6f3..ede55240 100644
--- a/modules/gallery/tests/SafeString_Test.php
+++ b/modules/gallery/tests/SafeString_Test.php
@@ -49,7 +49,7 @@ class SafeString_Test extends Unit_Test_Case {
public function for_js_test() {
$safe_string = new SafeString('"Foo\'s bar"');
$js_string = $safe_string->for_js();
- $this->assert_equal('\\"Foo<\\/em>\\\'s bar\\"',
+ $this->assert_equal('"\\"Foo<\\/em>\'s bar\\""',
$js_string);
}
@@ -96,21 +96,21 @@ class SafeString_Test extends Unit_Test_Case {
public function of_fluid_api_test() {
$escaped_string = SafeString::of("Foo's bar")->for_js();
- $this->assert_equal("Foo\\'s bar", $escaped_string);
+ $this->assert_equal('"Foo\'s bar"', $escaped_string);
}
public function safestring_of_safestring_preserves_safe_status_test() {
$safe_string = SafeString::of_safe_html("hello's world world world<\\/p>", $safe_string_2->for_js());
+ $this->assert_equal('"hello\'s world<\\/p>"', $safe_string_2->for_js());
}
public function safestring_of_safestring_preserves_html_safe_status_test() {
$safe_string = SafeString::of_safe_html("hello's world world world<\\/p>", $safe_string_2->for_js());
+ $this->assert_equal('"hello\'s world<\\/p>"', $safe_string_2->for_js());
}
public function safestring_of_safestring_safe_status_override_test() {
diff --git a/modules/gallery/tests/Xss_Security_Test.php b/modules/gallery/tests/Xss_Security_Test.php
index b385580d..3a22afc1 100644
--- a/modules/gallery/tests/Xss_Security_Test.php
+++ b/modules/gallery/tests/Xss_Security_Test.php
@@ -188,7 +188,7 @@ class Xss_Security_Test extends Unit_Test_Case {
if (self::_token_matches(array(T_DOUBLE_COLON, "::"), $tokens, $token_number + 1) &&
self::_token_matches(array(T_STRING), $tokens, $token_number + 2) &&
in_array($tokens[$token_number + 2][1],
- array("clean", "purify", "clean_js", "clean_attribute")) &&
+ array("clean", "purify", "js_string", "clean_attribute")) &&
self::_token_matches("(", $tokens, $token_number + 3)) {
// Not checking for mark_safe(). We want such calls to be marked dirty (thus reviewed).
@@ -198,7 +198,7 @@ class Xss_Security_Test extends Unit_Test_Case {
$token_number += 3;
$token = $tokens[$token_number];
- if ("clean_js" == $method) {
+ if ("js_string" == $method) {
$frame->is_safe_js(true);
} else {
$frame->is_safe_html(true);
--
cgit v1.2.3
From df38a890a64dd33eafe3aed51ce8fde732cf8b8b Mon Sep 17 00:00:00 2001
From: Andy Staudacher world world world world world world<\\/p>"',
- $string);
+ $string);
}
public function clean_attribute_test() {
$safe_string = SafeString::of_safe_html("hello's world world world world world world world world