Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
performance
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zry
performance
Commits
1041a2bf
Commit
1041a2bf
authored
Feb 23, 2023
by
ryun
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/SQL注入拦截' into release/v23.2.19高县版
parents
d38321e2
e2058872
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
1 deletions
+47
-1
performance/Performance.Api/Filters/AntiSqlInjectFilter.cs
+47
-1
No files found.
performance/Performance.Api/Filters/AntiSqlInjectFilter.cs
View file @
1041a2bf
using
System
;
using
System
;
using
System.Collections
;
using
System.Collections.Generic
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq
;
using
System.Text.RegularExpressions
;
using
System.Text.RegularExpressions
;
...
@@ -7,6 +8,7 @@
...
@@ -7,6 +8,7 @@
using
Microsoft.AspNetCore.Http
;
using
Microsoft.AspNetCore.Http
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.AspNetCore.Mvc.Filters
;
using
Microsoft.AspNetCore.Mvc.Filters
;
using
Microsoft.Extensions.Logging
;
using
Microsoft.Extensions.Options
;
using
Microsoft.Extensions.Options
;
using
Performance.DtoModels
;
using
Performance.DtoModels
;
using
Performance.DtoModels.AppSettings
;
using
Performance.DtoModels.AppSettings
;
...
@@ -94,10 +96,12 @@ public string GetSafetySql(string input)
...
@@ -94,10 +96,12 @@ public string GetSafetySql(string input)
public
class
AntiSqlInjectFilter
:
IAsyncActionFilter
public
class
AntiSqlInjectFilter
:
IAsyncActionFilter
{
{
private
readonly
Application
_application
;
private
readonly
Application
_application
;
private
readonly
ILogger
<
AntiSqlInjectFilter
>
_logger
;
public
AntiSqlInjectFilter
(
IOptions
<
Application
>
options
)
public
AntiSqlInjectFilter
(
I
Logger
<
AntiSqlInjectFilter
>
logger
,
I
Options
<
Application
>
options
)
{
{
_application
=
options
.
Value
;
_application
=
options
.
Value
;
_logger
=
logger
;
}
}
/// <inheritdoc />
/// <inheritdoc />
...
@@ -106,6 +110,8 @@ public AntiSqlInjectFilter(IOptions<Application> options)
...
@@ -106,6 +110,8 @@ public AntiSqlInjectFilter(IOptions<Application> options)
if
(
_application
.
OpenAntiSqlInject
==
true
)
if
(
_application
.
OpenAntiSqlInject
==
true
)
{
{
var
routePath
=
context
.
HttpContext
.
Request
.
Path
.
ToString
();
var
routePath
=
context
.
HttpContext
.
Request
.
Path
.
ToString
();
try
{
if
(
_application
.
AntiSqlInjectRouteWhite
?.
Any
(
route
=>
Regex
.
IsMatch
(
routePath
,
route
,
RegexOptions
.
IgnoreCase
))
!=
true
)
if
(
_application
.
AntiSqlInjectRouteWhite
?.
Any
(
route
=>
Regex
.
IsMatch
(
routePath
,
route
,
RegexOptions
.
IgnoreCase
))
!=
true
)
{
{
foreach
(
var
value
in
context
.
ActionArguments
.
Where
(
w
=>
w
.
Value
!=
null
).
Select
(
w
=>
w
.
Value
))
foreach
(
var
value
in
context
.
ActionArguments
.
Where
(
w
=>
w
.
Value
!=
null
).
Select
(
w
=>
w
.
Value
))
...
@@ -126,6 +132,40 @@ public AntiSqlInjectFilter(IOptions<Application> options)
...
@@ -126,6 +132,40 @@ public AntiSqlInjectFilter(IOptions<Application> options)
}
}
else
if
(
value
is
not
IFormCollection
)
else
if
(
value
is
not
IFormCollection
)
{
{
if
(
value
is
string
)
{
if
(!
AntiSqlInject
.
Instance
.
IsSafetySql
(
value
.
ToString
()))
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
}
}
else
if
(
value
is
IEnumerable
objects
)
{
foreach
(
var
item
in
objects
)
{
//是一个class,对class的属性中,string类型的属性进行过滤
var
properties
=
pType
.
GetProperties
();
foreach
(
var
pp
in
properties
)
{
var
temp
=
pp
.
GetValue
(
item
);
if
(
temp
==
null
)
continue
;
if
(
temp
is
string
)
{
if
(!
AntiSqlInject
.
Instance
.
IsSafetySql
(
temp
.
ToString
()))
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
}
}
}
}
}
else
{
//是一个class,对class的属性中,string类型的属性进行过滤
//是一个class,对class的属性中,string类型的属性进行过滤
var
properties
=
pType
.
GetProperties
();
var
properties
=
pType
.
GetProperties
();
foreach
(
var
pp
in
properties
)
foreach
(
var
pp
in
properties
)
...
@@ -147,6 +187,12 @@ public AntiSqlInjectFilter(IOptions<Application> options)
...
@@ -147,6 +187,12 @@ public AntiSqlInjectFilter(IOptions<Application> options)
}
}
}
}
}
}
}
catch
(
Exception
ex
)
{
_logger
.
LogError
(
$"SQL注入过滤器出现异常:
{
routePath
}
;
{
ex
}
"
);
}
}
await
next
();
await
next
();
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment