fix(im): handle unresolved Feishu resource images (#2761)

* fix(im): handle unresolved Feishu resource images

* fix(im): degrade unresolved Feishu images to plain text

Feishu cards reject non-http(s) markdown links as well as invalid image
keys, so [alt](resource://…) can still fail the whole card update.
Match every ![]() target and keep only http(s) as a fallback link.

* test(im): cover Feishu summary stripping of resource images

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
main
Jones 2026-09-25 18:48:12 +08:00 committed by GitHub
parent e5a301ed3a
commit 5c717ca045
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 93 additions and 9 deletions

View File

@ -1097,8 +1097,11 @@ func (a *Adapter) cardkitUpdateElement(ctx context.Context, accessToken, cardID,
// reaches the card we must download each referenced image and re-upload it to
// Feishu to obtain a usable image_key.
// feishuMarkdownImageRe matches a markdown image whose target is an http(s) URL.
var feishuMarkdownImageRe = regexp.MustCompile(`!\[([^\]]*)\]\((https?://[^)\s]+)\)`)
// feishuMarkdownImageRe matches every markdown image. Only http(s) targets can
// be uploaded to an image_key; unresolved internal handles (resource://,
// local://, minio://, …) are not valid Feishu image keys, so
// resolveMarkdownImages degrades them instead of leaving ![]() in the card.
var feishuMarkdownImageRe = regexp.MustCompile(`!\[([^\]]*)\]\(([^)\s]+)\)`)
// feishuMarkdownLinkRe matches a markdown link whose target is an http(s) URL.
// Applied after image stripping so signed image URLs are not left as links.
@ -1128,28 +1131,49 @@ func imageCacheKey(rawURL string) string {
}
// resolveMarkdownImages replaces the URL inside every ![alt](httpURL) with a
// Feishu image_key. On failure it degrades the image to a plain text link so the
// rest of the card still renders instead of failing the whole update.
// Feishu image_key. On failure, or when the storage resolver left an internal
// handle unresolved, it degrades the image so the rest of the card still
// renders instead of failing the whole update. HTTP(S) targets become a
// markdown link (Feishu accepts only http/https hrefs); other schemes become
// plain text, because [text](resource://…) is also rejected as an illegal link.
func (a *Adapter) resolveMarkdownImages(ctx context.Context, accessToken, content string) string {
if !strings.Contains(content, "![") {
return content
}
fallback := func(alt, rawURL string) string {
if alt == "" {
alt = a.region.ImageFallbackLabel
}
if isHTTPImageURL(rawURL) {
return fmt.Sprintf("[%s](%s)", alt, rawURL)
}
return alt
}
return feishuMarkdownImageRe.ReplaceAllStringFunc(content, func(match string) string {
sub := feishuMarkdownImageRe.FindStringSubmatch(match)
if len(sub) < 3 {
return match
}
alt, rawURL := sub[1], sub[2]
if !isHTTPImageURL(rawURL) {
return fallback(alt, rawURL)
}
imgKey, err := a.imageKeyForURL(ctx, accessToken, rawURL)
if err != nil || imgKey == "" {
logger.Warnf(ctx, "[%s] image upload failed, degrading to link: url=%s err=%v", a.region.Label, rawURL, err)
label := alt
if label == "" {
label = a.region.ImageFallbackLabel
}
return fmt.Sprintf("[%s](%s)", label, rawURL)
return fallback(alt, rawURL)
}
return fmt.Sprintf("![%s](%s)", alt, imgKey)
})
}
// isHTTPImageURL reports whether rawURL is an http(s) URL. Scheme match is
// case-insensitive: IM rewrite may emit HTTPS:// from a configured proxy domain.
func isHTTPImageURL(rawURL string) bool {
lower := strings.ToLower(rawURL)
return strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://")
}
// imageKeyForURL returns a Feishu image_key for the given URL, uploading it if
// not already cached.
func (a *Adapter) imageKeyForURL(ctx context.Context, accessToken, rawURL string) (string, error) {

View File

@ -93,6 +93,61 @@ func TestResolveMarkdownImages_FallbackToLinkOnFailure(t *testing.T) {
}
}
func TestResolveMarkdownImages_InternalHandleFallsBackToPlainText(t *testing.T) {
a := &Adapter{region: RegionFeishu}
tests := []struct {
name string
in string
want string
}{
{
name: "resource handle",
in: "see ![diagram](resource://Zb2IrqfTdlCFkbUqFtmS5A) here",
want: "see diagram here",
},
{
name: "local scheme",
in: "see ![shot](local://10000/exports/a.png) here",
want: "see shot here",
},
{
name: "empty alt uses region label",
in: "![](resource://Zb2IrqfTdlCFkbUqFtmS5A)",
want: "图片",
},
{
name: "mixed internal and failed http",
in: "![diagram](resource://Zb2IrqfTdlCFkbUqFtmS5A) and ![x](http://127.0.0.1/x.png)",
want: "diagram and [x](http://127.0.0.1/x.png)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := a.resolveMarkdownImages(context.Background(), "tok", tt.in)
if got != tt.want {
t.Errorf("fallback = %q, want %q", got, tt.want)
}
if strings.Contains(got, "![") {
t.Errorf("failed image should not remain as image markdown: %q", got)
}
if strings.Contains(got, "resource://") || strings.Contains(got, "local://") {
t.Errorf("internal scheme must not remain in card markdown: %q", got)
}
})
}
}
func TestResolveMarkdownImages_UppercaseHTTPSchemeStillUploads(t *testing.T) {
a := &Adapter{region: RegionFeishu}
// IM rewrite may emit HTTPS://; loopback still fails SSRF and must degrade
// to a link rather than being treated as a non-HTTP handle.
got := a.resolveMarkdownImages(context.Background(), "tok", "![](HTTPS://127.0.0.1/x.png)")
want := "[图片](HTTPS://127.0.0.1/x.png)"
if got != want {
t.Errorf("uppercase scheme fallback = %q, want %q", got, want)
}
}
// An image with no alt text falls back to the region's own label, so Lark users
// do not get a Chinese link label.
func TestResolveMarkdownImages_FallbackLabelFollowsRegion(t *testing.T) {
@ -387,6 +442,11 @@ func TestCardSummaryPreview(t *testing.T) {
}{
{name: "collapse whitespace", in: " 最终\n回答\t✅ ", want: "最终 回答 ✅"},
{name: "image keeps alt only", in: "![架构图](https://cdn.example/a.png?sig=secret) 说明", want: "架构图 说明"},
{
name: "resource image keeps alt only",
in: "![diagram](resource://Zb2IrqfTdlCFkbUqFtmS5A) 说明",
want: "diagram 说明",
},
{name: "link keeps text only", in: "见 [文档](https://cdn.example/a.png?sig=secret) 说明", want: "见 文档 说明"},
{name: "unicode limit", in: strings.Repeat("界", 121), want: strings.Repeat("界", 120)},
{name: "empty", in: " \n\t ", want: ""},