Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 0a7c8f0

Browse files
committed
Add isSqlSafe filters
1 parent ba997a6 commit 0a7c8f0

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/ServiceStack.OrmLite/OrmLiteUtils.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -465,22 +465,30 @@ public static string SqlValue(this object value)
465465

466466
public static Func<string,string> SqlVerifyFragmentFn { get; set; }
467467

468-
public static string SqlVerifyFragment(this string sqlFragment)
468+
public static bool IsSqlSafe(string sql)
469469
{
470-
if (sqlFragment == null)
471-
return null;
470+
if (sql == null)
471+
return true;
472472

473473
if (SqlVerifyFragmentFn != null)
474-
return SqlVerifyFragmentFn(sqlFragment);
474+
{
475+
SqlVerifyFragmentFn(sql);
476+
return true;
477+
}
475478

476-
var fragmentToVerify = sqlFragment
479+
var fragmentToVerify = sql
477480
.StripQuotedStrings('\'')
478481
.StripQuotedStrings('"')
479482
.StripQuotedStrings('`')
480483
.ToLower();
481484

482485
var match = VerifyFragmentRegEx.Match(fragmentToVerify);
483-
if (match.Success)
486+
return !match.Success;
487+
}
488+
489+
public static string SqlVerifyFragment(this string sqlFragment)
490+
{
491+
if (!IsSqlSafe(sqlFragment))
484492
throw new ArgumentException("Potential illegal fragment detected: " + sqlFragment);
485493

486494
return sqlFragment;

src/ServiceStack.OrmLite/TemplateDbFilters.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ public int dbExec(TemplateScopeContext scope, string sql, Dictionary<string, obj
8484
public string sqlTake(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
8585
public string ormliteVar(string name) => OrmLiteConfig.DialectProvider.Variables.TryGetValue(name, out var value) ? value : null;
8686

87+
public bool isSqlSafe(string sql) => OrmLiteUtils.IsSqlSafe(sql);
88+
8789
private string padCondition(string text) => string.IsNullOrEmpty(text) ? "" : " " + text;
8890
}
8991
}

src/ServiceStack.OrmLite/TemplateDbFiltersAsync.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public Task<object> dbExec(TemplateScopeContext scope, string sql, Dictionary<st
8686
public string sqlTake(int? limit) => padCondition(OrmLiteConfig.DialectProvider.SqlLimit(null, limit));
8787
public string ormliteVar(string name) => OrmLiteConfig.DialectProvider.Variables.TryGetValue(name, out var value) ? value : null;
8888

89+
public bool isSqlSafe(string sql) => OrmLiteUtils.IsSqlSafe(sql);
90+
8991
private string padCondition(string text) => string.IsNullOrEmpty(text) ? "" : " " + text;
9092
}
9193
}

0 commit comments

Comments
 (0)