Skip to content
New issue

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

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

Already on GitHub? # to your account

AI内容安全插件 panic 错误 #1368

Closed
1 task done
mrhaoji opened this issue Oct 8, 2024 · 3 comments
Closed
1 task done

AI内容安全插件 panic 错误 #1368

mrhaoji opened this issue Oct 8, 2024 · 3 comments
Assignees
Labels

Comments

@mrhaoji
Copy link
Contributor

mrhaoji commented Oct 8, 2024

If you are reporting any crash or any potential security issue, do not
open an issue in this repo. Please report the issue via ASRC(Alibaba Security Response Center) where the issue will be triaged appropriately.

  • I have searched the issues of this repository and believe that this is not a duplicate.

Ⅰ. Issue Description

请求上游阿里云内容安全服务时,插件报 panic: runtime error: index out of range 错误

Ⅱ. Describe what happened

If there is an exception, please attach the exception trace:

2024-10-08T09:03:08.776386Z     debug   envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1384 wasm log higress-system.ai-security-guard-1.0.0: http call end, id: 7438aa55-1860-42d9-a3e8-92f4c52a2036, code: 200, normal: true, body: {"RequestId":"289A0546-6BCB-5239-A039-B00B77C38C94","Message":"OK","Data":{"RiskLevel":"high","Result":[{"Description":"命中自定义库","CustomizedHit":[{"LibName":"暴恐","KeyWords":"法~抡,法~仑,法~掄,法~沦,法~伦,法~侖,法~倫"},{"LibName":"政治","KeyWords":"政府,多党,共产党"},{"LibName":"反动","KeyWords":"主权"},{"LibName":"违禁词","KeyWords":"民主,多党,共产党"},{"LibName":"短信违禁词","KeyWords":"民主,共产,共产党"}],"Confidence":100.0,"Label":"customized"}]},"Code":200}       thread=276
2024-10-08T09:03:08.776440Z     info    envoy wasm external/envoy/source/extensions/common/wasm/context.cc:1387 wasm log higress-system.ai-security-guard-1.0.0: panic: runtime error: index out of range       thread=276
2024-10-08T09:03:08.776544Z     error   envoy wasm external/envoy/source/extensions/common/wasm/wasm_vm.cc:41   Function: proxy_on_http_call_response failed: Uncaught RuntimeError: unreachable\nProxy-Wasm plugin in-VM backtrace:\n  0:  0x2e25b - runtime.runtimePanicAt\n  1:  0x1cd8b - runtime.lookupPanic\n  2:  0x5fd3c - main.onHttpResponseBody$1\n  3:  0x5c94d - github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper.HttpCall$1\n  4:  0x4daa9 - proxy_on_http_call_response    thread=276
2024-10-08T09:03:11.195680Z     info    xdsproxy        connected to upstream XDS server: 127.0.0.1:15012

Ⅲ. Describe what you expected to happen

Ⅳ. How to reproduce it (as minimally and precisely as possible)

  1. xxx
  2. xxx
  3. xxx

Ⅴ. Anything else we need to know?

Ⅵ. Environment:

  • Higress version:
  • OS :
  • Others:
@johnlanni
Copy link
Collaborator

cc @rinfx

@johnlanni johnlanni added type/bug Something isn't working sig/wasm labels Oct 8, 2024
@CH3CHO
Copy link
Collaborator

CH3CHO commented Oct 8, 2024

原因已经定位,问题代码如下:

https://github.com/alibaba/higress/blob/main/plugins/wasm-go/extensions/ai-security-guard/main.go#L403

if config.protocolOriginal {
	jsonData = []byte(denyMessage)
} else if strings.Contains(strings.Join(hdsMap["content-type"], ";"), "event-stream") {
	randomID := generateRandomID()
	jsonData = []byte(fmt.Sprintf(OpenAIStreamResponseFormat, randomID, model, respAdvice.Array()[0].Get("Answer").String(), randomID, model))
} else {
	randomID := generateRandomID()
	jsonData = []byte(fmt.Sprintf(OpenAIResponseFormat, randomID, model, respAdvice.Array()[0].Get("Answer").String()))
}

前面的代码都对 respAdvice 进行了判定,这里没有。整个插件里对各种 array 是否为空都没有做比较严格的判定。建议对代码进行重构,精简冗余代码,完善边界判定。

@rinfx
Copy link
Collaborator

rinfx commented Oct 18, 2024

已修复 #1394

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants