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:
@@ -33,6 +33,7 @@ import (
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/im"
|
||||
"github.com/Tencent/WeKnora/internal/logger"
|
||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -500,6 +501,13 @@ func (a *WebhookAdapter) DownloadFile(ctx context.Context, msg *im.IncomingMessa
|
||||
// 2. Content-Type → extension mapping (fallback for platforms like WeCom that
|
||||
// don't provide the original filename in the callback JSON)
|
||||
func downloadFromURL(ctx context.Context, rawURL, fileName string) (io.ReadCloser, string, error) {
|
||||
// SSRF protection: reject internal/private URLs unless on the WeCom API allowlist.
|
||||
if !isAllowedIMAPIHost(rawURL) {
|
||||
if safe, reason := secutils.IsSSRFSafeURL(rawURL); !safe {
|
||||
return nil, "", fmt.Errorf("URL rejected for security reasons: %s", reason)
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("create request: %w", err)
|
||||
@@ -573,6 +581,30 @@ func downloadFromURL(ctx context.Context, rawURL, fileName string) (io.ReadClose
|
||||
return resp.Body, fileName, nil
|
||||
}
|
||||
|
||||
// allowedIMAPIHosts lists IM platform API hosts that are trusted for file downloads.
|
||||
// URLs pointing to these hosts bypass IsSSRFSafeURL checks because the WeCom API
|
||||
// itself returns these URLs in callback payloads (e.g. temporary media links).
|
||||
var allowedIMAPIHosts = []string{
|
||||
"qyapi.weixin.qq.com",
|
||||
"api.weixin.qq.com",
|
||||
"open.work.weixin.qq.com",
|
||||
}
|
||||
|
||||
// isAllowedIMAPIHost returns true if rawURL points to a known IM platform API host.
|
||||
func isAllowedIMAPIHost(rawURL string) bool {
|
||||
u, err := url.Parse(rawURL)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
hostname := strings.ToLower(u.Hostname())
|
||||
for _, allowed := range allowedIMAPIHosts {
|
||||
if hostname == allowed {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// contentTypeToExt maps common Content-Type values to file extensions.
|
||||
func contentTypeToExt(ct string) string {
|
||||
// Normalize: take only the media type, ignore parameters like charset
|
||||
|
||||
Reference in New Issue
Block a user