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
b0fef222
Commit
b0fef222
authored
Feb 16, 2023
by
DESKTOP-115LT8U\admin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加白名单
parent
ef67265d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
23 deletions
+43
-23
performance/Performance.Api/Filters/AntiSqlInjectFilter.cs
+37
-23
performance/Performance.Api/appsettings.Localhost.json
+6
-0
No files found.
performance/Performance.Api/Filters/AntiSqlInjectFilter.cs
View file @
b0fef222
...
...
@@ -7,6 +7,7 @@
using
Microsoft.AspNetCore.Mvc.Filters
;
using
Microsoft.Extensions.Configuration
;
using
Performance.DtoModels
;
using
Performance.DtoModels.AppSettings
;
namespace
Performance.Api
{
...
...
@@ -95,6 +96,7 @@ public class AntiSqlInjectFilter : IAsyncActionFilter
public
AntiSqlInjectFilter
(
IConfiguration
configuration
)
{
_configuration
=
configuration
;
}
/// <inheritdoc />
...
...
@@ -103,39 +105,51 @@ public AntiSqlInjectFilter(IConfiguration configuration)
var
openAntiSqlInject
=
_configuration
.
GetValue
(
"Application:OpenAntiSqlInject"
,
false
);
if
(
openAntiSqlInject
)
{
foreach
(
var
value
in
context
.
ActionArguments
.
Where
(
w
=>
w
.
Value
!=
null
).
Select
(
w
=>
w
.
Value
))
bool
judgmentWhitelist
=
true
;
var
path
=
string
.
Format
(
context
.
RouteData
.
Values
[
"Controller"
]
+
@"/"
+
context
.
RouteData
.
Values
[
"action"
]).
ToLower
();
var
routeWhiteLists
=
_configuration
.
GetSection
(
"RouteWhiteList"
).
AsEnumerable
();
foreach
(
var
item
in
routeWhiteLists
)
{
//如果不是值类型或接口,不需要过滤
var
pType
=
value
.
GetType
();
if
(!
pType
.
IsClass
)
continu
e
;
if
(!
string
.
IsNullOrEmpty
(
item
.
Value
))
if
(
item
.
Value
.
Equals
(
path
))
judgmentWhitelist
=
fals
e
;
//对string类型过滤
if
(
value
is
string
&&
!
AntiSqlInject
.
Instance
.
IsSafetySql
(
value
.
ToString
()))
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
}
else
}
if
(
judgmentWhitelist
)
foreach
(
var
value
in
context
.
ActionArguments
.
Where
(
w
=>
w
.
Value
!=
null
).
Select
(
w
=>
w
.
Value
))
{
//是一个class,对class的属性中,string类型的属性进行过滤
var
properties
=
pType
.
GetProperties
();
foreach
(
var
pp
in
properties
)
{
var
temp
=
pp
.
GetValue
(
value
);
if
(
temp
==
null
)
continue
;
//如果不是值类型或接口,不需要过滤
var
pType
=
value
.
GetType
();
if
(!
pType
.
IsClass
)
continue
;
if
(
temp
is
string
&&
!
AntiSqlInject
.
Instance
.
IsSafetySql
(
temp
.
ToString
()))
//对string类型过滤
if
(
value
is
string
&&
!
AntiSqlInject
.
Instance
.
IsSafetySql
(
value
.
ToString
()))
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
}
else
{
//是一个class,对class的属性中,string类型的属性进行过滤
var
properties
=
pType
.
GetProperties
();
foreach
(
var
pp
in
properties
)
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
var
temp
=
pp
.
GetValue
(
value
);
if
(
temp
==
null
)
continue
;
if
(
temp
is
string
&&
!
AntiSqlInject
.
Instance
.
IsSafetySql
(
temp
.
ToString
()))
{
var
response
=
new
ApiResponse
(
ResponseType
.
Dangerous
,
"危险操作已拦截"
);
context
.
Result
=
new
ObjectResult
(
response
);
return
;
}
}
}
}
}
}
await
next
();
}
}
}
performance/Performance.Api/appsettings.Localhost.json
View file @
b0fef222
...
...
@@ -46,5 +46,11 @@
],
"Period"
:
"1"
,
//
单位为秒
"Limit"
:
1
},
"RouteWhiteList"
:
{
"Route"
:
[
"account/logins"
,
"account/quick/login"
]
}
}
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