Skip to content

Commit bc32f1c

Browse files
Add timezone validation when reading ClientTimeZoneId (#857)
* Add validation of timezone when reading ClientTimeZoneId from headers and cookies. * When reading cookie, try reading from undecoded cookie, otherwise, timezone "Etc/GMT+3" cookie value is read as "Etc/GMT 3"
1 parent 649f58e commit bc32f1c

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

dotnet/src/dotnetframework/GxClasses/Core/GXApplication.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,6 +2319,20 @@ public string GetCookie(string name)
23192319
}
23202320
return cookieVal;
23212321
}
2322+
internal string GetUndecodedCookie(string name)
2323+
{
2324+
string cookieVal = string.Empty;
2325+
HttpCookie cookie = TryGetCookie(localCookies, name);
2326+
if (cookie == null && _HttpContext != null)
2327+
{
2328+
cookie = TryGetCookie(_HttpContext.Request.GetCookies(), name);
2329+
}
2330+
if (cookie != null && cookie.Value != null)
2331+
{
2332+
cookieVal = cookie.Value;
2333+
}
2334+
return cookieVal;
2335+
}
23222336

23232337
private HttpCookie TryGetCookie(HttpCookieCollection cookieColl, string name)
23242338
{
@@ -3595,9 +3609,15 @@ internal string ClientTimeZoneId
35953609
sTZ = (string)GetCookie(GX_REQUEST_TIMEZONE);
35963610
GXLogging.Debug(log, "ClientTimeZone GX_REQUEST_TIMEZONE cookie:", sTZ);
35973611
}
3612+
if (!DateTimeUtil.ValidTimeZone(sTZ))
3613+
{
3614+
sTZ = (string)GetUndecodedCookie(GX_REQUEST_TIMEZONE);
3615+
GXLogging.Debug(log, "Try reading undecoded ClientTimeZone GX_REQUEST_TIMEZONE cookie:", sTZ);
3616+
}
35983617
try
35993618
{
3600-
_currentTimeZoneId = String.IsNullOrEmpty(sTZ) ? DateTimeZoneProviders.Tzdb.GetSystemDefault().Id : sTZ;
3619+
_currentTimeZoneId = !DateTimeUtil.ValidTimeZone(sTZ) ? DateTimeZoneProviders.Tzdb.GetSystemDefault().Id : sTZ;
3620+
36013621
}
36023622
catch (Exception e1)
36033623
{
@@ -3642,14 +3662,15 @@ public String GetTimeZone()
36423662
public Boolean SetTimeZone(String sTZ)
36433663
{
36443664
sTZ = StringUtil.RTrim(sTZ);
3645-
Boolean ret = false;
3665+
bool ret = false;
36463666
#if NODATIME
36473667
string tzId;
36483668
try
36493669
{
3650-
if (DateTimeZoneProviders.Tzdb[sTZ] != null)
3670+
DateTimeZone zone = DateTimeZoneProviders.Tzdb.GetZoneOrNull(sTZ);
3671+
if (zone != null)
36513672
{
3652-
tzId = DateTimeZoneProviders.Tzdb[sTZ].Id;
3673+
tzId = zone.Id;
36533674
}
36543675
else
36553676
{

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,9 +2430,9 @@ static public short CurrentOffset(IGxContext context)
24302430

24312431
static private TimeSpan CurrentOffset(string clientTimeZone)
24322432
{
2433-
DateTimeZone clientTimeZoneObj = DateTimeZoneProviders.Tzdb[clientTimeZone];
2433+
DateTimeZone clientTimeZoneObj = DateTimeZoneProviders.Tzdb.GetZoneOrNull(clientTimeZone);
24342434
if (clientTimeZoneObj == null)
2435-
clientTimeZoneObj = DateTimeZoneProviders.Tzdb[LocalTimeZoneId];
2435+
clientTimeZoneObj = DateTimeZoneProviders.Tzdb.GetZoneOrNull(LocalTimeZoneId);
24362436
Instant now = SystemClock.Instance.GetCurrentInstant();
24372437

24382438
try
@@ -3246,7 +3246,7 @@ private static DateTime ConvertDateTime(DateTime dt, string fromTimezone, string
32463246
DateTime ret = toUniversalTime(dt, fromTimezone);
32473247

32483248

3249-
if (string.IsNullOrEmpty(toTimezone) || DateTimeZoneProviders.Tzdb[toTimezone]==null)
3249+
if (!ValidTimeZone(toTimezone))
32503250
toTimezone = DateTimeZoneProviders.Tzdb.GetSystemDefault().Id;
32513251

32523252
if (ret < OlsonMinTime)
@@ -3260,6 +3260,11 @@ private static DateTime ConvertDateTime(DateTime dt, string fromTimezone, string
32603260
}
32613261
return dtconverted.AddMilliseconds(milliSeconds);
32623262
}
3263+
internal static bool ValidTimeZone(string timeZone)
3264+
{
3265+
return !string.IsNullOrEmpty(timeZone) && DateTimeZoneProviders.Tzdb.GetZoneOrNull(timeZone) != null;
3266+
}
3267+
32633268
internal static DateTime Local2DBserver(DateTime dt, string clientTimezone)
32643269
{
32653270
try
@@ -3562,7 +3567,10 @@ static public DateTime toUniversalTime(DateTime dt, IGxContext context)
35623567
static public DateTime FromTimeZone(DateTime dt, String sTZ, IGxContext context)
35633568
{
35643569
#if NODATIME
3565-
return ConvertDateTime(dt, sTZ, context.GetTimeZone());
3570+
if (ValidTimeZone(sTZ))
3571+
return ConvertDateTime(dt, sTZ, context.GetTimeZone());
3572+
else
3573+
return dt;
35663574
#else
35673575
OlsonTimeZone fromTimeZone = TimeZoneUtil.GetInstanceFromOlsonName(sTZ);
35683576
if (fromTimeZone != null)

dotnet/test/DotNetUnitTest/Domain/TimeZoneTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using GeneXus.Application;
23
using GeneXus.Utils;
34
using TZ4Net;
45
using Xunit;

0 commit comments

Comments
 (0)