From 1d633457c4482ab96bf936e9951ded2d5ebc8c74 Mon Sep 17 00:00:00 2001
From: Andy Staudacher
Date: Sat, 29 Aug 2009 11:31:00 -0700
Subject: Have url::site() and other methods return a SafeString, just as t()
and t2().
Benefits:
- url::site() is often used in views and we can ensure in the url class that returned strings are indeed safe for use in HTML. Makes the list of vars of unknown safety status shorter.
- url::site() is often used as message parameter to t() and t2(). The parameter would be HTML-escaped if it wasn't marked as safe HTML already. Makes the usage simpler / shorter.
---
modules/gallery/helpers/MY_url.php | 24 +++++++++++++++++++++++-
1 file changed, 23 insertions(+), 1 deletion(-)
(limited to 'modules/gallery/helpers/MY_url.php')
diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php
index c4967c52..b4b7f352 100644
--- a/modules/gallery/helpers/MY_url.php
+++ b/modules/gallery/helpers/MY_url.php
@@ -30,7 +30,8 @@ class url extends url_Core {
if ($parts[0] == "albums" || $parts[0] == "photos") {
$uri = model_cache::get("item", $parts[1])->relative_path();
}
- return parent::site($uri . $query, $protocol);
+ $url = parent::site($uri . $query, $protocol);
+ return SafeString::of($url)->mark_html_safe();
}
static function parse_url() {
@@ -99,4 +100,25 @@ class url extends url_Core {
static function abs_current($qs=false) {
return self::abs_site(url::current($qs));
}
+
+ public static function base($index=false, $protocol=false) {
+ $url = parent::base($index, $protocol);
+ return SafeString::of($url)->mark_html_safe();
+ }
+
+ public static function current($qs=false) {
+ $url = parent::current($qs);
+ return SafeString::of($url)->mark_html_safe();
+ }
+
+ public static function file($file, $index=false) {
+ $url = parent::file($file, $index);
+ return SafeString::of($url)->mark_html_safe();
+ }
+
+ public static function merge(array $arguments) {
+ $url = parent::merge($arguments);
+ return SafeString::of($url)->mark_html_safe();
+ }
+
}
--
cgit v1.2.3
From a10063ff68cf5988297dcad889384ab2080c3850 Mon Sep 17 00:00:00 2001
From: Andy Staudacher
Date: Sat, 29 Aug 2009 12:34:09 -0700
Subject: Add more factory methods for convenience: SafeString::purify() and
SafeString::of_safe_html().
Removing SafeString::mark_html_safe() since it's no longer needed.
---
modules/gallery/helpers/MY_url.php | 10 +++++-----
modules/gallery/libraries/I18n.php | 2 +-
modules/gallery/libraries/SafeString.php | 27 +++++++++++++++++++++------
modules/gallery/tests/SafeString_Test.php | 19 +++++++++++--------
modules/gallery/tests/Xss_Security_Test.php | 7 +++++--
5 files changed, 43 insertions(+), 22 deletions(-)
(limited to 'modules/gallery/helpers/MY_url.php')
diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php
index b4b7f352..6092a9d8 100644
--- a/modules/gallery/helpers/MY_url.php
+++ b/modules/gallery/helpers/MY_url.php
@@ -31,7 +31,7 @@ class url extends url_Core {
$uri = model_cache::get("item", $parts[1])->relative_path();
}
$url = parent::site($uri . $query, $protocol);
- return SafeString::of($url)->mark_html_safe();
+ return SafeString::of_safe_html($url);
}
static function parse_url() {
@@ -103,22 +103,22 @@ class url extends url_Core {
public static function base($index=false, $protocol=false) {
$url = parent::base($index, $protocol);
- return SafeString::of($url)->mark_html_safe();
+ return SafeString::of_safe_html($url);
}
public static function current($qs=false) {
$url = parent::current($qs);
- return SafeString::of($url)->mark_html_safe();
+ return SafeString::of_safe_html($url);
}
public static function file($file, $index=false) {
$url = parent::file($file, $index);
- return SafeString::of($url)->mark_html_safe();
+ return SafeString::of_safe_html($url);
}
public static function merge(array $arguments) {
$url = parent::merge($arguments);
- return SafeString::of($url)->mark_html_safe();
+ return SafeString::of_safe_html($url);
}
}
diff --git a/modules/gallery/libraries/I18n.php b/modules/gallery/libraries/I18n.php
index 8dc42e04..a53d5ae9 100644
--- a/modules/gallery/libraries/I18n.php
+++ b/modules/gallery/libraries/I18n.php
@@ -116,7 +116,7 @@ class I18n_Core {
$entry = $this->interpolate($locale, $entry, $values);
- return SafeString::of($entry)->mark_html_safe();
+ return SafeString::of_safe_html($entry);
}
private function lookup($locale, $message) {
diff --git a/modules/gallery/libraries/SafeString.php b/modules/gallery/libraries/SafeString.php
index 709ab5f6..9a269ed4 100644
--- a/modules/gallery/libraries/SafeString.php
+++ b/modules/gallery/libraries/SafeString.php
@@ -24,6 +24,7 @@
class SafeString_Core {
private $_raw_string;
protected $_is_safe_html = false;
+ protected $_is_purified_html = false;
private static $_purifier = null;
@@ -44,11 +45,25 @@ class SafeString_Core {
}
/**
- * Marks this string as safe to be used in HTML without any escaping.
+ * Factory method returning a new SafeString instance after HTML purifying
+ * the given string.
*/
- function mark_html_safe() {
- $this->_is_safe_html = true;
- return $this;
+ static function purify($string) {
+ if ($string instanceof SafeString) {
+ $string = $string->unescaped();
+ }
+ $safe_string = self::of_safe_html(self::_purify_for_html($string));
+ $safe_string->_is_purified_html = true;
+ return $safe_string;
+ }
+
+ /**
+ * Factory method returning a new SafeString instance which won't HTML escape.
+ */
+ static function of_safe_html($string) {
+ $safe_string = new SafeString($string);
+ $safe_string->_is_safe_html = true;
+ return $safe_string;
}
/**
@@ -117,10 +132,10 @@ class SafeString_Core {
* @return the string escaped for use in HTML.
*/
function purified_html() {
- if ($this->_is_safe_html) {
+ if ($this->_is_purified_html) {
return $this;
} else {
- return SafeString::of(self::_purify_for_html($this->_raw_string), true);
+ return self::purify($this);
}
}
diff --git a/modules/gallery/tests/SafeString_Test.php b/modules/gallery/tests/SafeString_Test.php
index 73d82c34..0fc7f6f3 100644
--- a/modules/gallery/tests/SafeString_Test.php
+++ b/modules/gallery/tests/SafeString_Test.php
@@ -25,8 +25,7 @@ class SafeString_Test extends Unit_Test_Case {
}
public function toString_for_safe_string_test() {
- $safe_string = new SafeString("hello world
");
- $safe_string->mark_html_safe();
+ $safe_string = SafeString::of_safe_html("hello world
");
$this->assert_equal("hello world
",
$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("hello world
")->mark_html_safe();
+ $safe_string = SafeString::of_safe_html("hello world
");
$this->assert_equal("hello world
", $safe_string->for_html());
}
+ public function purify_test() {
+ $safe_string = SafeString::purify("hello world
");
+ $this->assert_equal("hello world
", $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's world
")->mark_html_safe();
+ $safe_string = SafeString::of_safe_html("hello's world
");
$safe_string_2 = new SafeString($safe_string);
$this->assert_equal("hello's world
", $safe_string_2);
$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("hello's
world
")
- ->mark_html_safe();
+ $safe_string = SafeString::of_safe_html("hello's world
");
$safe_string_2 = new SafeString($safe_string);
$this->assert_equal("hello's world
", $safe_string_2);
$this->assert_equal("hello\\'s world<\\/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("hello world
", $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 b4b638be44375c93f5222c7b48ed547845d6d7e5 Mon Sep 17 00:00:00 2001
From: Andy Staudacher
Date: Sat, 29 Aug 2009 16:28:30 -0700
Subject: Undo url helper changes - url methods no longer return a SafeString.
Adding SafeString::of_safe_html() calls where urls are passed as parameters to t() and t2().
---
modules/akismet/helpers/akismet.php | 2 +-
modules/digibug/views/admin_digibug.html.php | 2 +-
modules/exif/helpers/exif.php | 2 +-
modules/g2_import/views/admin_g2_import.html.php | 10 ++++-----
modules/gallery/helpers/MY_url.php | 24 +---------------------
modules/gallery/helpers/graphics.php | 2 +-
modules/gallery/tests/Xss_Security_Test.php | 15 +++++++++++---
modules/gallery/views/admin_block_welcome.html.php | 10 ++++-----
modules/gallery/views/upgrader.html.php | 2 +-
modules/recaptcha/helpers/recaptcha.php | 2 +-
modules/search/helpers/search.php | 2 +-
modules/server_add/helpers/server_add.php | 2 +-
modules/user/views/reset_password.html.php | 4 +++-
system/helpers/request.php | 2 +-
14 files changed, 35 insertions(+), 46 deletions(-)
(limited to 'modules/gallery/helpers/MY_url.php')
diff --git a/modules/akismet/helpers/akismet.php b/modules/akismet/helpers/akismet.php
index db45a6ab..abca78d2 100644
--- a/modules/akismet/helpers/akismet.php
+++ b/modules/akismet/helpers/akismet.php
@@ -94,7 +94,7 @@ class akismet_Core {
if (empty($api_key)) {
site_status::warning(
t("Akismet is not quite ready! Please provide an API Key",
- array("url" => url::site("admin/akismet"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/akismet")))),
"akismet_config");
} else {
site_status::clear("akismet_config");
diff --git a/modules/digibug/views/admin_digibug.html.php b/modules/digibug/views/admin_digibug.html.php
index 7e4436ff..5f27a3fd 100644
--- a/modules/digibug/views/admin_digibug.html.php
+++ b/modules/digibug/views/admin_digibug.html.php
@@ -16,7 +16,7 @@
= t("You don't need an account with Digibug, but if you register with Digibug and enter your Digibug id in the Advanced Settings page you can make money off of your photos!",
array("signup_url" => "http://www.digibug.com/signup.php",
- "advanced_settings_url" => url::site("admin/advanced_settings"))) ?>
+ "advanced_settings_url" => SafeString::of_safe_html(url::site("admin/advanced_settings")))) ?>
diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php
index 20ecd0cb..d4e60338 100644
--- a/modules/exif/helpers/exif.php
+++ b/modules/exif/helpers/exif.php
@@ -164,7 +164,7 @@ class exif_Core {
if ($remaining) {
site_status::warning(
t('Your Exif index needs to be updated. Fix this now',
- array("url" => url::site("admin/maintenance/start/exif_task::update_index?csrf=__CSRF__"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/maintenance/start/exif_task::update_index?csrf=__CSRF__")))),
"exif_index_out_of_date");
}
}
diff --git a/modules/g2_import/views/admin_g2_import.html.php b/modules/g2_import/views/admin_g2_import.html.php
index da2bb5d1..f53510f6 100644
--- a/modules/g2_import/views/admin_g2_import.html.php
+++ b/modules/g2_import/views/admin_g2_import.html.php
@@ -37,9 +37,9 @@
if ($g2_sizes["thumb"]["size"] && $thumb_size != $g2_sizes["thumb"]["size"]): ?>
= t("Your most common thumbnail size in Gallery 2 is %g2_pixels pixels, but your Gallery 3 thumbnail size is set to %g3_pixels pixels. Using the same value will speed up your import.",
- array("g2_pixels" => $g2_sizes["thumb"]["size"],
- "g3_pixels" => $thumb_size,
- "url" => url::site("admin/theme_options"))) ?>
+ array("g2_pixels" => $g2_sizes["thumb"]["size"],
+ "g3_pixels" => $thumb_size,
+ "url" => SafeString::of_safe_html(url::site("admin/theme_options")))) ?>
endif ?>
@@ -47,8 +47,8 @@
= t("Your most common intermediate size in Gallery 2 is %g2_pixels pixels, but your Gallery 3 thumbnail size is set to %g3_pixels pixels. Using the same value will speed up your import.",
array("g2_pixels" => $g2_sizes["resize"]["size"],
- "g3_pixels" => $resize_size,
- "url" => url::site("admin/theme_options"))) ?>
+ "g3_pixels" => $resize_size,
+ "url" => SafeString::of_safe_html(url::site("admin/theme_options")))) ?>
endif ?>
diff --git a/modules/gallery/helpers/MY_url.php b/modules/gallery/helpers/MY_url.php
index 6092a9d8..c4967c52 100644
--- a/modules/gallery/helpers/MY_url.php
+++ b/modules/gallery/helpers/MY_url.php
@@ -30,8 +30,7 @@ class url extends url_Core {
if ($parts[0] == "albums" || $parts[0] == "photos") {
$uri = model_cache::get("item", $parts[1])->relative_path();
}
- $url = parent::site($uri . $query, $protocol);
- return SafeString::of_safe_html($url);
+ return parent::site($uri . $query, $protocol);
}
static function parse_url() {
@@ -100,25 +99,4 @@ class url extends url_Core {
static function abs_current($qs=false) {
return self::abs_site(url::current($qs));
}
-
- public static function base($index=false, $protocol=false) {
- $url = parent::base($index, $protocol);
- return SafeString::of_safe_html($url);
- }
-
- public static function current($qs=false) {
- $url = parent::current($qs);
- return SafeString::of_safe_html($url);
- }
-
- public static function file($file, $index=false) {
- $url = parent::file($file, $index);
- return SafeString::of_safe_html($url);
- }
-
- public static function merge(array $arguments) {
- $url = parent::merge($arguments);
- return SafeString::of_safe_html($url);
- }
-
}
diff --git a/modules/gallery/helpers/graphics.php b/modules/gallery/helpers/graphics.php
index 7dc46eeb..fbb85bec 100644
--- a/modules/gallery/helpers/graphics.php
+++ b/modules/gallery/helpers/graphics.php
@@ -442,7 +442,7 @@ class graphics_Core {
if (!module::get_var("gallery", "graphics_toolkit")) {
site_status::warning(
t("Graphics toolkit missing! Please choose a toolkit",
- array("url" => url::site("admin/graphics"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/graphics")))),
"missing_graphics_toolkit");
}
}
diff --git a/modules/gallery/tests/Xss_Security_Test.php b/modules/gallery/tests/Xss_Security_Test.php
index 690dc760..a2d3d59b 100644
--- a/modules/gallery/tests/Xss_Security_Test.php
+++ b/modules/gallery/tests/Xss_Security_Test.php
@@ -130,14 +130,14 @@ class Xss_Security_Test extends Unit_Test_Case {
$token = $tokens[$token_number];
}
} else if ($token[1] == "url") {
- // url methods return a SafeString
+ // url methods return safe HTML
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("site", "current", "base", "file", "abs_site", "abs_current",
"abs_file", "merge")) &&
self::_token_matches("(", $tokens, $token_number + 3)) {
- $frame->is_safestring(true);
+ $frame->is_safe_html(true);
$method = $tokens[$token_number + 2][1];
$frame->expr_append("::$method(");
@@ -203,7 +203,8 @@ class Xss_Security_Test extends Unit_Test_Case {
$state = "CLEAN";
}
} else {
- if ($frame->is_safestring() || $frame->purified_html_called() || $frame->for_html_called()) {
+ if ($frame->is_safe_html() || $frame->is_safestring() ||
+ $frame->purified_html_called() || $frame->for_html_called()) {
$state = "CLEAN";
}
}
@@ -259,6 +260,7 @@ class Xss_Security_Test_Frame {
private $_for_html_called = false;
private $_purified_html_called = false;
private $_json_encode_called = false;
+ private $_is_safe_html = false;
private $_line;
function __construct($line_number, $in_script_block) {
@@ -288,6 +290,13 @@ class Xss_Security_Test_Frame {
return $this->_is_safestring;
}
+ function is_safe_html($new_val=NULL) {
+ if ($new_val !== NULL) {
+ $this->_is_safe_html = (bool) $new_val;
+ }
+ return $this->_is_safe_html;
+ }
+
function json_encode_called($new_val=NULL) {
if ($new_val !== NULL) {
$this->_json_encode_called = (bool) $new_val;
diff --git a/modules/gallery/views/admin_block_welcome.html.php b/modules/gallery/views/admin_block_welcome.html.php
index 38d2bd56..c6ccdbf3 100644
--- a/modules/gallery/views/admin_block_welcome.html.php
+++ b/modules/gallery/views/admin_block_welcome.html.php
@@ -5,16 +5,16 @@
-
= t("General Settings - choose your graphics and language settings.",
- array("graphics_url" => url::site("admin/graphics"),
- "language_url" => url::site("admin/languages"))) ?>
+ array("graphics_url" => SafeString::of_safe_html(url::site("admin/graphics")),
+ "language_url" => SafeString::of_safe_html(url::site("admin/languages")))) ?>
-
= t("Appearance - choose a theme, or customize the way it looks.",
- array("theme_url" => url::site("admin/themes"),
- "theme_options_url" => url::site("admin/theme_options"))) ?>
+ array("theme_url" => SafeString::of_safe_html(url::site("admin/themes")),
+ "theme_options_url" => SafeString::of_safe_html(url::site("admin/theme_options")))) ?>
-
= t("Customize - install modules to add cool features!",
- array("modules_url" => url::site("admin/modules"))) ?>
+ array("modules_url" => SafeString::of_safe_html(url::site("admin/modules")))) ?>
diff --git a/modules/gallery/views/upgrader.html.php b/modules/gallery/views/upgrader.html.php
index 37578855..ccc86da8 100644
--- a/modules/gallery/views/upgrader.html.php
+++ b/modules/gallery/views/upgrader.html.php
@@ -18,7 +18,7 @@
= t("That's it!") ?>
= t("Your Gallery is up to date.",
- array("url" => url::site("albums/1"))) ?>
+ array("url" => SafeString::of_safe_html(url::site("albums/1")))) ?>
diff --git a/modules/recaptcha/helpers/recaptcha.php b/modules/recaptcha/helpers/recaptcha.php
index 501dd972..35d9febd 100644
--- a/modules/recaptcha/helpers/recaptcha.php
+++ b/modules/recaptcha/helpers/recaptcha.php
@@ -43,7 +43,7 @@ class recaptcha_Core {
if (empty($public_key) || empty($private_key)) {
site_status::warning(
t("reCAPTCHA is not quite ready! Please configure the reCAPTCHA Keys",
- array("url" => url::site("admin/recaptcha"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/recaptcha")))),
"recaptcha_config");
} else {
site_status::clear("recaptcha_config");
diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php
index 355c4493..4be04039 100644
--- a/modules/search/helpers/search.php
+++ b/modules/search/helpers/search.php
@@ -58,7 +58,7 @@ class search_Core {
if ($remaining) {
site_status::warning(
t('Your search index needs to be updated. Fix this now',
- array("url" => url::site("admin/maintenance/start/search_task::update_index?csrf=__CSRF__"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/maintenance/start/search_task::update_index?csrf=__CSRF__")))),
"search_index_out_of_date");
}
}
diff --git a/modules/server_add/helpers/server_add.php b/modules/server_add/helpers/server_add.php
index 74f51ad9..57afac12 100644
--- a/modules/server_add/helpers/server_add.php
+++ b/modules/server_add/helpers/server_add.php
@@ -25,7 +25,7 @@ class server_add_Core {
if (empty($paths)) {
site_status::warning(
t("Server Add needs configuration. Configure it now!",
- array("url" => url::site("admin/server_add"))),
+ array("url" => SafeString::of_safe_html(url::site("admin/server_add")))),
"server_add_configuration");
} else {
site_status::clear("server_add_configuration");
diff --git a/modules/user/views/reset_password.html.php b/modules/user/views/reset_password.html.php
index 3dc7aebf..6fa92d54 100644
--- a/modules/user/views/reset_password.html.php
+++ b/modules/user/views/reset_password.html.php
@@ -9,7 +9,9 @@
= t("Hello, %name,", array("name" => $user->full_name ? $user->full_name : $user->name)) ?>
- = t("We received a request to reset your password for %site_url. If you made this request, you can confirm it by clicking this link. If you didn't request this password reset, it's ok to ignore this mail.", array("site_url" => url::base(false, "http"), "confirm_url" => $confirm_url)) ?>
+ = t("We received a request to reset your password for %site_url. If you made this request, you can confirm it by clicking this link. If you didn't request this password reset, it's ok to ignore this mail.",
+ array("site_url" => SafeString::of_safe_html(url::base(false, "http")),
+ "confirm_url" => $confirm_url)) ?>