-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bind CSRF token to session id for .NET.
Move VerificationToken service to root path (rest/)
- Loading branch information
1 parent
4959fe1
commit d4886c3
Showing
7 changed files
with
174 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
|
||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using GeneXus.Configuration; | ||
using GeneXus.Http; | ||
using log4net; | ||
using Microsoft.AspNetCore.Antiforgery; | ||
using Microsoft.AspNetCore.Http; | ||
namespace GeneXus.Application | ||
{ | ||
public class ValidateAntiForgeryTokenMiddleware | ||
{ | ||
static readonly ILog log = log4net.LogManager.GetLogger(typeof(ValidateAntiForgeryTokenMiddleware)); | ||
|
||
private readonly RequestDelegate _next; | ||
private readonly IAntiforgery _antiforgery; | ||
private string _basePath; | ||
|
||
public ValidateAntiForgeryTokenMiddleware(RequestDelegate next, IAntiforgery antiforgery, String basePath) | ||
{ | ||
_next = next; | ||
_antiforgery = antiforgery; | ||
_basePath = "/" + basePath; | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
if (context.Request.Path.HasValue && context.Request.Path.Value.StartsWith(_basePath)) | ||
{ | ||
if (HttpMethods.IsPost(context.Request.Method) || | ||
HttpMethods.IsDelete(context.Request.Method) || | ||
HttpMethods.IsPut(context.Request.Method)) | ||
{ | ||
string cookieToken = context.Request.Cookies[HttpHeader.X_CSRF_TOKEN_COOKIE]; | ||
string headerToken = context.Request.Headers[HttpHeader.X_CSRF_TOKEN_HEADER]; | ||
GXLogging.Debug(log, $"Antiforgery validation, cookieToken:{cookieToken}, headerToken:{headerToken}"); | ||
|
||
await _antiforgery.ValidateRequestAsync(context); | ||
GXLogging.Debug(log, $"Antiforgery validation OK"); | ||
} | ||
else if (HttpMethods.IsGet(context.Request.Method)) | ||
{ | ||
SetAntiForgeryTokens(_antiforgery, context); | ||
} | ||
} | ||
if (!context.Request.Path.Value.EndsWith(_basePath)) //VerificationToken | ||
await _next(context); | ||
} | ||
internal static void SetAntiForgeryTokens(IAntiforgery _antiforgery, HttpContext context) | ||
{ | ||
AntiforgeryTokenSet tokenSet = _antiforgery.GetAndStoreTokens(context); | ||
string sameSite; | ||
CookieOptions cookieOptions = new CookieOptions { HttpOnly = false, Secure = GxContext.GetHttpSecure(context) == 1 }; | ||
SameSiteMode sameSiteMode = SameSiteMode.Unspecified; | ||
if (Config.GetValueOf("SAMESITE_COOKIE", out sameSite) && Enum.TryParse(sameSite, out sameSiteMode)) | ||
{ | ||
cookieOptions.SameSite = sameSiteMode; | ||
} | ||
context.Response.Cookies.Append(HttpHeader.X_CSRF_TOKEN_COOKIE, tokenSet.RequestToken, cookieOptions); | ||
GXLogging.Debug(log, $"Setting cookie ", HttpHeader.X_CSRF_TOKEN_COOKIE, "=", tokenSet.RequestToken, " samesite:" + sameSiteMode); | ||
} | ||
|
||
} | ||
public class SessionIdAntiforgeryAdditionalDataProvider : IAntiforgeryAdditionalDataProvider | ||
{ | ||
static readonly ILog log = log4net.LogManager.GetLogger(typeof(SessionIdAntiforgeryAdditionalDataProvider)); | ||
public string GetAdditionalData(HttpContext context) | ||
{ | ||
context.NewSessionCheck(); | ||
GXLogging.Debug(log, $"Setting session id as additional CSRF token data:", context.Session.Id); | ||
return context.Session.Id.Trim(); | ||
} | ||
|
||
public bool ValidateAdditionalData(HttpContext context, string additionalData) | ||
{ | ||
bool validSession = context.Session.Id.Trim().CompareTo(additionalData.Trim()) == 0 ? true : false; | ||
GXLogging.Warn(log, $"Session id in CSRF token ({additionalData}) does not match the current session id ({context.Session.Id})"); | ||
return validSession; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
dotnet/src/dotnetframework/GxClasses/Helpers/CsrfHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.Net.Http; | ||
using System.Security; | ||
using System.Web; | ||
using System.Web.Helpers; | ||
using GeneXus.Application; | ||
using GeneXus.Utils; | ||
using log4net; | ||
|
||
namespace GeneXus.Http | ||
{ | ||
internal class CSRFHelper | ||
{ | ||
//AntiForgeryConfig.AdditionalDataProvider = new SessionIdAntiforgeryAdditionalDataProvider(); | ||
|
||
[SecuritySafeCritical] | ||
internal static void ValidateAntiforgery(HttpContext context) | ||
{ | ||
if (RestAPIHelpers.ValidateCsrfToken()) | ||
{ | ||
ValidateAntiforgeryImpl(context); | ||
} | ||
} | ||
[SecurityCritical] | ||
static void ValidateAntiforgeryImpl(HttpContext context) | ||
{ | ||
string cookieToken, formToken; | ||
string httpMethod = context.Request.HttpMethod; | ||
string tokens = context.Request.Cookies[HttpHeader.X_CSRF_TOKEN_COOKIE]?.Value; | ||
string internalCookieToken = context.Request.Cookies[HttpHeader.X_CSRF_TOKEN_COOKIE]?.Value; | ||
if (httpMethod == HttpMethod.Get.Method && (string.IsNullOrEmpty(tokens) || string.IsNullOrEmpty(internalCookieToken))) | ||
{ | ||
AntiForgery.GetTokens(null, out cookieToken, out formToken); | ||
#pragma warning disable SCS0009 // The cookie is missing security flag HttpOnly | ||
HttpCookie cookie = new HttpCookie(HttpHeader.X_CSRF_TOKEN_COOKIE, formToken) | ||
{ | ||
HttpOnly = false, | ||
Secure = GxContext.GetHttpSecure(context) == 1, | ||
}; | ||
#pragma warning restore SCS0009 // The cookie is missing security flag HttpOnly | ||
HttpCookie internalCookie = new HttpCookie(AntiForgeryConfig.CookieName, cookieToken) | ||
{ | ||
HttpOnly = true, | ||
Secure = GxContext.GetHttpSecure(context) == 1, | ||
}; | ||
context.Response.SetCookie(cookie); | ||
context.Response.SetCookie(internalCookie); | ||
} | ||
if (httpMethod == HttpMethod.Delete.Method || httpMethod == HttpMethod.Post.Method || httpMethod == HttpMethod.Put.Method) | ||
{ | ||
cookieToken = context.Request.Cookies[AntiForgeryConfig.CookieName]?.Value; | ||
string headerToken = context.Request.Headers[HttpHeader.X_CSRF_TOKEN_HEADER]; | ||
AntiForgery.Validate(cookieToken, headerToken); | ||
} | ||
} | ||
} | ||
|
||
|
||
public class SessionIdAntiforgeryAdditionalDataProvider : IAntiForgeryAdditionalDataProvider | ||
{ | ||
static readonly ILog log = log4net.LogManager.GetLogger(typeof(SessionIdAntiforgeryAdditionalDataProvider)); | ||
public string GetAdditionalData(HttpContextBase context) | ||
{ | ||
GXLogging.Debug(log, $"Setting session id as additional CSRF token data:", context.Session.SessionID); | ||
return context.Session.SessionID.Trim(); | ||
} | ||
[SecuritySafeCritical] | ||
public bool ValidateAdditionalData(HttpContextBase context, string additionalData) | ||
{ | ||
bool validSession = context.Session.SessionID.Trim().CompareTo(additionalData.Trim()) == 0 ? true : false; | ||
GXLogging.Warn(log, $"Session id in CSRF token ({additionalData}) does not match the current session id ({context.Session.SessionID})"); | ||
return validSession; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters