summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Util.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2008-08-29 08:44:43 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2008-08-29 08:44:43 -0700
commit67102dc426e7ca90095b6aa8bee4a60b31117e7c (patch)
tree40dccebe135bf556b38ffe522b8834ad2709888d /src/DotNetOpenId/Util.cs
parent190ba46c2003e5e2b9dc764a4c4c50db31954301 (diff)
downloadDotNetOpenAuth-67102dc426e7ca90095b6aa8bee4a60b31117e7c.zip
DotNetOpenAuth-67102dc426e7ca90095b6aa8bee4a60b31117e7c.tar.gz
DotNetOpenAuth-67102dc426e7ca90095b6aa8bee4a60b31117e7c.tar.bz2
Added support for the Simple Registration extension to be seen in javascript for the ajax login control.
Diffstat (limited to 'src/DotNetOpenId/Util.cs')
-rw-r--r--src/DotNetOpenId/Util.cs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/DotNetOpenId/Util.cs b/src/DotNetOpenId/Util.cs
index 64a1be9..6b0efaf 100644
--- a/src/DotNetOpenId/Util.cs
+++ b/src/DotNetOpenId/Util.cs
@@ -249,6 +249,41 @@ namespace DotNetOpenId {
return true;
}
+ // The characters to escape here are inspired by
+ // http://code.google.com/p/doctype/wiki/ArticleXSSInJavaScript
+ static readonly Dictionary<string, string> javascriptEscaping = new Dictionary<string,string> {
+ {"\t", @"\t" },
+ {"\n", @"\n" },
+ {"\r", @"\r" },
+ {"\u0085", @"\u0085" },
+ {"\u2028", @"\u2028" },
+ {"\u2029", @"\u2029" },
+ {"'", @"\x27" },
+ {"\"", @"\x22" },
+ {"\\", @"\\" }, // perhaps move this to the top so that newline characters still end up as newline characters and not "\n" sequences
+ {"&", @"\x26" },
+ {"<", @"\x3c" },
+ {">", @"\x3e" },
+ {"=", @"\x3d" },
+ };
+
+ /// <summary>
+ /// Prepares what SHOULD be simply a string value for safe injection into Javascript
+ /// by using appropriate character escaping.
+ /// </summary>
+ /// <param name="value">The untrusted string value to be escaped to protected against XSS attacks.</param>
+ /// <returns>The escaped string.</returns>
+ public static string MakeSafeJavascriptValue(string value) {
+ if (value == null) return "null";
+ // We use a StringBuilder because we have potentially many replacements to do,
+ // and we don't want to create a new string for every intermediate replacement step.
+ StringBuilder builder = new StringBuilder(value);
+ foreach (var pair in javascriptEscaping) {
+ builder.Replace(pair.Key, pair.Value);
+ }
+ return builder.ToString();
+ }
+
internal delegate R Func<T, R>(T t);
/// <summary>
/// Scans a list for matches with some element of the OpenID protocol,