Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Added AntiForgeryToken to SSVE
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpydev committed Sep 30, 2011
1 parent 9679f5e commit 599133b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Nancy.Tests/Fakes/FakeViewEngineHost.cs
Expand Up @@ -53,5 +53,14 @@ public string ExpandPath(string path)
{
return this.ExpandPathCallBack != null ? this.ExpandPathCallBack.Invoke(path) : path;
}

/// <summary>
/// Get the anti forgery token form element
/// </summary>
/// <returns>String containin the form element</returns>
public string AntiForgeryToken()
{
return "CSRF";
}
}
}
12 changes: 12 additions & 0 deletions src/Nancy.Tests/Unit/ViewEngines/SuperSimpleViewEngineTests.cs
Expand Up @@ -678,6 +678,18 @@ public void Should_call_to_expand_paths()

Assert.Equal("<script src='/BasePath/scripts/test.js'></script>", result);
}

[Fact]
public void Should_expand_anti_forgery_tokens()
{
const string input = "<html><body><form>@AntiForgeryToken</form><body></html>";
var fakeViewEngineHost = new FakeViewEngineHost();
var viewEngine = new SuperSimpleViewEngine();

var result = viewEngine.Render(input, null, fakeViewEngineHost);

Assert.Equal("<html><body><form>CSRF</form><body></html>", result);
}
}

public class User
Expand Down
Expand Up @@ -35,5 +35,11 @@ public interface IViewEngineHost
/// <param name="path">Path to expand</param>
/// <returns>Expanded path</returns>
string ExpandPath(string path);

/// <summary>
/// Get the anti forgery token form element
/// </summary>
/// <returns>String containin the form element</returns>
string AntiForgeryToken();
}
}
11 changes: 11 additions & 0 deletions src/Nancy/ViewEngines/SuperSimpleViewEngine/NancyViewEngineHost.cs
Expand Up @@ -65,5 +65,16 @@ public string ExpandPath(string path)
{
return this.renderContext.ParsePath(path);
}

/// <summary>
/// Get the anti forgery token form element
/// </summary>
/// <returns>String containin the form element</returns>
public string AntiForgeryToken()
{
var tokenKeyValue = this.renderContext.GetCsrfToken();

return string.Format("<input type=\"hidden\" name=\"{0}\" value=\"{1}\"", tokenKeyValue.Key, tokenKeyValue.Value);
}
}
}
Expand Up @@ -59,6 +59,11 @@ public class SuperSimpleViewEngine
/// </summary>
private static readonly Regex PathExpansionRegEx = new Regex(@"(?:@Path\[\'(?<Path>.+?)\'\]);?", RegexOptions.Compiled);

/// <summary>
/// Compiled RegEx for the CSRF anti forgery token
/// </summary>
private static readonly Regex AntiForgeryTokenRegEx = new Regex(@"@AntiForgeryToken;?", RegexOptions.Compiled);

/// <summary>
/// View engine transform processors
/// </summary>
Expand All @@ -75,6 +80,7 @@ public SuperSimpleViewEngine()
this.PerformEachSubstitutions,
this.PerformConditionalSubstitutions,
this.PerformPathSubstitutions,
this.PerformAntiForgeryTokenSubstitutions,
this.PerformPartialSubstitutions,
this.PerformMasterPageSubstitutions,
};
Expand Down Expand Up @@ -428,6 +434,18 @@ private string PerformPathSubstitutions(string template, object model, IViewEngi
return result;
}

/// <summary>
/// Perform CSRF anti forgery token expansions
/// </summary>
/// <param name="template">The template.</param>
/// <param name="model">The model.</param>
/// <param name="host">View engine host</param>
/// <returns>Template with anti forgery tokens expanded</returns>
private string PerformAntiForgeryTokenSubstitutions(string template, object model, IViewEngineHost host)
{
return AntiForgeryTokenRegEx.Replace(template, host.AntiForgeryToken());
}

/// <summary>
/// Perform @Partial partial view expansion
/// </summary>
Expand Down

0 comments on commit 599133b

Please sign in to comment.