feat(security): implement SSRF and path traversal protections in image handling and file downloads

- Added SSRF protection by stripping client-supplied URL and Caption fields from image attachments in the QA request handler.
- Introduced a validation function to ensure Feishu API path parameters contain only safe characters, preventing path traversal attacks.
- Enhanced the WeCom webhook adapter to reject internal/private URLs unless they are on an allowlist, improving security during file downloads.
- Implemented a mechanism to bypass SSRF checks for trusted IM platform API hosts.
This commit is contained in:
wizardchen
2026-03-23 13:11:11 +08:00
committed by lyingbug
parent 052be53c42
commit b2c009e61c
3 changed files with 57 additions and 0 deletions
+17
View File
@@ -476,6 +476,18 @@ func (a *Adapter) SendReply(ctx context.Context, incoming *im.IncomingMessage, r
// File download support via Feishu GetMessageResource API
// ──────────────────────────────────────────────────────────────────────
// feishuSafePathParam checks that a Feishu API path parameter contains only
// safe characters (alphanumeric, hyphen, underscore). This prevents path
// traversal attacks via crafted callback payloads.
func feishuSafePathParam(s string) bool {
for _, c := range s {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '-' || c == '_') {
return false
}
}
return len(s) > 0
}
// DownloadFile downloads a file or image attachment from a Feishu message.
// Uses the GetMessageResource API: GET /open-apis/im/v1/messages/:message_id/resources/:file_key?type={file|image}
func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io.ReadCloser, string, error) {
@@ -483,6 +495,11 @@ func (a *Adapter) DownloadFile(ctx context.Context, msg *im.IncomingMessage) (io
return nil, "", fmt.Errorf("file_key and message_id are required")
}
// SSRF/path-traversal protection: validate path parameters contain only safe characters
if !feishuSafePathParam(msg.MessageID) || !feishuSafePathParam(msg.FileKey) {
return nil, "", fmt.Errorf("invalid message_id or file_key format")
}
accessToken, err := a.getTenantAccessToken(ctx)
if err != nil {
return nil, "", fmt.Errorf("get access token: %w", err)