Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize AI security guard plugin #1473

Merged
merged 8 commits into from
Nov 11, 2024
Merged

Optimize AI security guard plugin #1473

merged 8 commits into from
Nov 11, 2024

Conversation

rinfx
Copy link
Collaborator

@rinfx rinfx commented Nov 5, 2024

  1. 代码重构:1)gjson解析json string改为json.unmarshal方式;2)denyMessage优先级统一调整为用户自定义 > 阿里云内容安全返回建议 > 兜底内容;3)denyMessage统一进行一次序列化,防止response body错误
  2. 增加拦截等级设置,与内容安全对齐(high,medium,low),用户可以设置拦截阈值,默认只拦截high,同时风险分与风险等级可以在内容安全控制台进行设置
  3. 增加STS Token接入方式,开源用户仍需设置AK/SK,商业化用户可基于角色扮演进行授权而不是设置明文AK/SK,避免AK/SK泄漏风险

@codecov-commenter
Copy link

codecov-commenter commented Nov 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 43.53%. Comparing base (ef31e09) to head (58367a8).
Report is 194 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1473      +/-   ##
==========================================
+ Coverage   35.91%   43.53%   +7.62%     
==========================================
  Files          69       76       +7     
  Lines       11576    12320     +744     
==========================================
+ Hits         4157     5364    +1207     
+ Misses       7104     6621     -483     
- Partials      315      335      +20     

see 69 files with indirect coverage changes

@@ -67,6 +99,7 @@ type AISecurityConfig struct {
denyCode int64
denyMessage string
protocolOriginal bool
riskLevelBar string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文档需要同步更新

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

OpenAIStreamResponseFormat = OpenAIStreamResponseChunk + "\n\n" + OpenAIStreamResponseEnd + "\n\n" + `data: [DONE]`

TracingPrefix = "trace_span_tag."
// TracingPrefix = "trace_span_tag."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

没用的话就直接删掉吧

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

"ServiceParameters": fmt.Sprintf(`{"content": "%s"}`, marshalStr(content, log)),
}
if config.token != "" {
params["SecurityToken"] = config.token
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在官网文档里没有找到这个参数诶,能提供一下出处吗?

另外 config.token 这个字段名字是不是用 securityToken 好一点?

https://help.aliyun.com/document_detail/2671445.html
https://help.aliyun.com/document_detail/108840.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SecurityToken 这个不是内容安全官方文档提供的,属于是角色扮演才会用到的,内容安全那边对外没有提供api文档

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那角色扮演是做什么用的呢,有说明吗?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接在插件里面填用户的AK/SK有风险,会直接暴露在控制台上,角色扮演可以避免用户在控制台填写AK/SK

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的

@johnlanni johnlanni changed the title Ai sec observer Optimize AI security guard plugin Nov 6, 2024
@@ -114,6 +113,8 @@ func (config *AISecurityConfig) incrementCounter(metricName string, inc uint64)

func riskLevelToInt(riskLevel string) int {
switch riskLevel {
case MaxRisk:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,因为有时候内容安全误拦截比较多,设置为max的话,就只记录检测结果,但是不会拦截请求,这样用户可以先设置为max观察一段时间,之后再调整等级

Copy link
Collaborator

@CH3CHO CH3CHO left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rinfx rinfx merged commit 2cb8558 into alibaba:main Nov 11, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants