Skip to content

Commit

Permalink
Merge pull request #44 from PxTools/fix/misc-injections
Browse files Browse the repository at this point in the history
Misc injection fixes
  • Loading branch information
JohannesFinsveen authored Feb 14, 2024
2 parents cb7f9f5 + 86572b3 commit bb7ce71
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
2 changes: 0 additions & 2 deletions PxWeb/Code/Api2/Cache/PxCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public void Set(object key, object value, TimeSpan lifetime)
{
if (_cache.Get(key) is null)
{
_logger.LogDebug("Adding key={0} to Cache", key);

lock (_cacheLock)
{
if (_cache.Get(key) is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public Dictionary<string, ItemSelection> GetMenuLookup(string language)
{
var menuLookup = new Dictionary<string, ItemSelection>();

if (!LanguageUtil.HasValidLanguageCodePattern(language))
{
_logger.LogWarning($"Unsupported language: {LanguageUtil.SanitizeLangueCode(language)}");
return menuLookup;
}

try
{
string webRootPath = _hostingEnvironment.RootPath;
Expand All @@ -51,7 +57,7 @@ public Dictionary<string, ItemSelection> GetMenuLookup(string language)

catch (Exception e)
{
_logger.LogError($"Error loading MenuLookup table for language {language}", e);
_logger.LogError($"Error loading MenuLookup table for language {LanguageUtil.SanitizeLangueCode(language)}", e);
}

return menuLookup;
Expand Down
7 changes: 6 additions & 1 deletion PxWeb/Code/Api2/DataSource/PxFile/TablePathResolverPxFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private Dictionary<string, string> GetPxTableLookup(string language)
{
var tableLookup = new Dictionary<string, string>();

if (!LanguageUtil.HasValidLanguageCodePattern(language)) {
_logger.LogWarning($"Unsupported language: {LanguageUtil.SanitizeLangueCode(language)}");
return tableLookup;
}

try
{
string webRootPath = _hostingEnvironment.RootPath;
Expand Down Expand Up @@ -92,7 +97,7 @@ private Dictionary<string, string> GetPxTableLookup(string language)

catch (Exception e)
{
_logger.LogError($"Error loading TablePathLookup table for language {language}", e);
_logger.LogError(e, $"Error loading TablePathLookup table for language {LanguageUtil.SanitizeLangueCode(language)}");
}

return tableLookup;
Expand Down
20 changes: 20 additions & 0 deletions PxWeb/Code/LanguageUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Text.RegularExpressions;

namespace PxWeb.Code
{
public class LanguageUtil
{
public static bool HasValidLanguageCodePattern(string languageCode)
{
//Language code are either XX or XX-XX
var pattern = @"^[a-z]{2}-[a-z]{2}$|^[a-z]{2}$";
return Regex.IsMatch(languageCode, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
}

public static string SanitizeLangueCode(string languageCode)
{
return Regex.Replace(languageCode, @"[^a-zA-Z-]+", "?");
}
}
}

0 comments on commit bb7ce71

Please sign in to comment.