package dingtalk import ( "bytes" "context" "encoding/base64" "encoding/json" "errors" "fmt" "image" _ "image/gif" _ "image/jpeg" _ "image/png" "io" "net" "net/http" "net/netip" "net/url" "strings" "sync" "time" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" ) const ( oauthTokenURL = "https://api.dingtalk.com/v1.0/oauth2/accessToken" messageFileDownloadURL = "https://api.dingtalk.com/v1.0/robot/messageFiles/download" maxAPIResponseBytes = 1 << 20 maxImageBytes = 8 << 20 maxImagePixels = 40_000_000 ) var blockedDownloadRanges = []netip.Prefix{ netip.MustParsePrefix("100.64.0.0/10"), netip.MustParsePrefix("192.0.0.0/24"), netip.MustParsePrefix("192.0.2.0/24"), netip.MustParsePrefix("198.18.0.0/15"), netip.MustParsePrefix("198.51.100.0/24"), netip.MustParsePrefix("203.0.113.0/24"), netip.MustParsePrefix("2001:db8::/32"), } type DingTalkImageDownloader struct { clientID string clientSecret string tokenURL string fileURL string apiClient *http.Client downloadHTTP *http.Client allowTestURL bool mu sync.Mutex token string tokenTill time.Time } func NewImageDownloader(clientID, clientSecret string, client *http.Client) (*DingTalkImageDownloader, error) { return newImageDownloader(clientID, clientSecret, client, oauthTokenURL, messageFileDownloadURL, false) } func newImageDownloaderForTest(clientID, clientSecret string, client *http.Client, tokenURL, fileURL string) (*DingTalkImageDownloader, error) { return newImageDownloader(clientID, clientSecret, client, tokenURL, fileURL, true) } func newImageDownloader(clientID, clientSecret string, client *http.Client, tokenURL, fileURL string, allowTest bool) (*DingTalkImageDownloader, error) { clientID = strings.TrimSpace(clientID) clientSecret = strings.TrimSpace(clientSecret) if clientID == "" { return nil, errors.New("dingtalk image downloader: clientID is required") } if clientSecret == "" { return nil, errors.New("dingtalk image downloader: clientSecret is required") } base := &http.Client{Timeout: 30 * time.Second} if client != nil { copyClient := *client base = ©Client if base.Timeout <= 0 { base.Timeout = 30 * time.Second } } apiClient := *base apiClient.CheckRedirect = func(*http.Request, []*http.Request) error { return errors.New("dingtalk API redirect refused") } downloadClient := *base if !allowTest { transport := http.DefaultTransport.(*http.Transport).Clone() transport.Proxy = nil transport.DialContext = safeDialContext apiClient.Transport = transport downloadClient.Transport = transport.Clone() } d := &DingTalkImageDownloader{ clientID: clientID, clientSecret: clientSecret, tokenURL: tokenURL, fileURL: fileURL, apiClient: &apiClient, downloadHTTP: &downloadClient, allowTestURL: allowTest, } d.downloadHTTP.CheckRedirect = func(req *http.Request, via []*http.Request) error { if len(via) >= 3 { return errors.New("dingtalk image redirect limit exceeded") } req.Header = make(http.Header) if !d.allowTestURL { if err := validateDownloadURL(req.URL); err != nil { return err } } return nil } return d, nil } func (d *DingTalkImageDownloader) Download(ctx context.Context, downloadCode string) (core.ImageInput, error) { downloadCode = strings.TrimSpace(downloadCode) if downloadCode == "" { return core.ImageInput{}, errors.New("dingtalk image download: downloadCode is required") } token, err := d.accessToken(ctx) if err != nil { return core.ImageInput{}, err } payload, err := d.postJSON(ctx, d.fileURL, map[string]string{ "downloadCode": downloadCode, "robotCode": d.clientID, }, token) if err != nil { return core.ImageInput{}, err } var fileResponse struct { DownloadURL string `json:"downloadUrl"` } if json.Unmarshal(payload, &fileResponse) != nil || strings.TrimSpace(fileResponse.DownloadURL) == "" { return core.ImageInput{}, errors.New("dingtalk image download: invalid message file response") } downloadURL := strings.TrimSpace(fileResponse.DownloadURL) if !d.allowTestURL { downloadURL, err = normalizeInitialDownloadURL(downloadURL) if err != nil { return core.ImageInput{}, err } parsed, parseErr := url.Parse(downloadURL) if parseErr != nil || validateDownloadURL(parsed) != nil { return core.ImageInput{}, errors.New("dingtalk image download: unsafe download URL") } } req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil) if err != nil { return core.ImageInput{}, errors.New("dingtalk image download: invalid download request") } resp, err := d.downloadHTTP.Do(req) if err != nil { if ctx.Err() != nil { return core.ImageInput{}, ctx.Err() } return core.ImageInput{}, errors.New("dingtalk image download: request failed") } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return core.ImageInput{}, fmt.Errorf("dingtalk image download: HTTP %d", resp.StatusCode) } if resp.ContentLength > maxImageBytes { return core.ImageInput{}, errors.New("dingtalk image download: image exceeds size limit") } data, err := io.ReadAll(io.LimitReader(resp.Body, maxImageBytes+1)) if err != nil { return core.ImageInput{}, errors.New("dingtalk image download: read failed") } if len(data) > maxImageBytes { return core.ImageInput{}, errors.New("dingtalk image download: image exceeds size limit") } return validateImageBytes(data) } func (d *DingTalkImageDownloader) accessToken(ctx context.Context) (string, error) { d.mu.Lock() defer d.mu.Unlock() if d.token != "" && time.Now().Before(d.tokenTill) { return d.token, nil } payload, err := d.postJSON(ctx, d.tokenURL, map[string]string{ "appKey": d.clientID, "appSecret": d.clientSecret, }, "") if err != nil { return "", err } var response struct { AccessToken string `json:"accessToken"` ExpireIn int64 `json:"expireIn"` } if json.Unmarshal(payload, &response) != nil || strings.TrimSpace(response.AccessToken) == "" { return "", errors.New("dingtalk image download: invalid OAuth response") } ttl := time.Duration(response.ExpireIn) * time.Second refreshAfter := ttl - 5*time.Minute if refreshAfter <= 0 { refreshAfter = ttl / 2 } if refreshAfter <= 0 { refreshAfter = time.Minute } d.token = response.AccessToken d.tokenTill = time.Now().Add(refreshAfter) return d.token, nil } func (d *DingTalkImageDownloader) postJSON(ctx context.Context, endpoint string, value any, token string) ([]byte, error) { body, err := json.Marshal(value) if err != nil { return nil, errors.New("dingtalk image download: encode API request failed") } req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body)) if err != nil { return nil, errors.New("dingtalk image download: create API request failed") } req.Header.Set("Content-Type", "application/json") if token != "" { req.Header.Set("x-acs-dingtalk-access-token", token) } resp, err := d.apiClient.Do(req) if err != nil { if ctx.Err() != nil { return nil, ctx.Err() } return nil, errors.New("dingtalk image download: API request failed") } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return nil, fmt.Errorf("dingtalk image download: API HTTP %d", resp.StatusCode) } return readBoundedAPIResponse(resp) } func readBoundedAPIResponse(resp *http.Response) ([]byte, error) { if resp.ContentLength > maxAPIResponseBytes { return nil, errors.New("dingtalk image download: API response exceeds size limit") } payload, err := io.ReadAll(io.LimitReader(resp.Body, maxAPIResponseBytes+1)) if err != nil { return nil, errors.New("dingtalk image download: API response read failed") } if len(payload) > maxAPIResponseBytes { return nil, errors.New("dingtalk image download: API response exceeds size limit") } return payload, nil } func validateImageBytes(data []byte) (core.ImageInput, error) { if len(data) == 0 || len(data) > maxImageBytes { return core.ImageInput{}, errors.New("dingtalk image download: invalid image size") } mime := http.DetectContentType(data) expectedFormat := map[string]string{ "image/png": "png", "image/jpeg": "jpeg", "image/gif": "gif", }[mime] if expectedFormat == "" { return core.ImageInput{}, errors.New("dingtalk image download: unsupported image format") } config, format, err := image.DecodeConfig(bytes.NewReader(data)) if err != nil || format != expectedFormat || config.Width <= 0 || config.Height <= 0 { return core.ImageInput{}, errors.New("dingtalk image download: invalid image data") } if int64(config.Width)*int64(config.Height) > maxImagePixels { return core.ImageInput{}, errors.New("dingtalk image download: image dimensions exceed limit") } return core.ImageInput{ DataURI: "data:" + mime + ";base64," + base64.StdEncoding.EncodeToString(data), MIMEType: mime, SizeBytes: int64(len(data)), Width: config.Width, Height: config.Height, }, nil } func normalizeInitialDownloadURL(raw string) (string, error) { parsed, err := url.Parse(strings.TrimSpace(raw)) if err != nil || parsed.Hostname() == "" || parsed.User != nil { return "", errors.New("dingtalk image download: unsafe download URL") } switch parsed.Scheme { case "http": parsed.Scheme = "https" case "https": default: return "", errors.New("dingtalk image download: unsafe download URL") } return parsed.String(), nil } func validateDownloadURL(parsed *url.URL) error { if parsed == nil || parsed.Scheme != "https" || parsed.Hostname() == "" || parsed.User != nil { return errors.New("dingtalk image download: unsafe download URL") } return nil } func safeDialContext(ctx context.Context, network, address string) (net.Conn, error) { host, port, err := net.SplitHostPort(address) if err != nil { return nil, errors.New("dingtalk image download: invalid destination") } var ips []net.IP if literal := net.ParseIP(host); literal != nil { ips = []net.IP{literal} } else { resolved, resolveErr := net.DefaultResolver.LookupIPAddr(ctx, host) if resolveErr != nil || len(resolved) == 0 { return nil, errors.New("dingtalk image download: destination resolution failed") } for _, item := range resolved { ips = append(ips, item.IP) } } for _, ip := range ips { if !isSafeDownloadIP(ip) { return nil, errors.New("dingtalk image download: destination is not public") } } dialer := &net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second} for _, ip := range ips { conn, dialErr := dialer.DialContext(ctx, network, net.JoinHostPort(ip.String(), port)) if dialErr == nil { return conn, nil } } return nil, errors.New("dingtalk image download: destination connection failed") } func isSafeDownloadIP(ip net.IP) bool { if ip == nil || !ip.IsGlobalUnicast() || ip.IsPrivate() || ip.IsLoopback() || ip.IsLinkLocalUnicast() || ip.IsLinkLocalMulticast() || ip.IsMulticast() || ip.IsUnspecified() { return false } addr, ok := netip.AddrFromSlice(ip) if !ok { return false } addr = addr.Unmap() for _, prefix := range blockedDownloadRanges { if prefix.Contains(addr) { return false } } return true }