mirror of
https://github.com/larksuite/cli.git
synced 2026-07-06 00:06:28 +08:00
* feat(contentsafety): add extension interface layer with Provider, Alert, and registry Change-Id: Ibeac6366c7201293057bc3b063f75ac34565bcd5 * feat(contentsafety): add normalize utility for JSON type conversion Change-Id: I7d4729a5ddcab2553abc110f8f6ecc88435ae921 * feat(contentsafety): add tree walker and regex scanner Change-Id: I215dad7cf3072711d05e45f7d384162e1f8752d4 * feat(contentsafety): add config loading with lazy creation, default rules, and allowlist matching Change-Id: I75e10df28f1f8d4f433cb2b469a0ff317af3bf70 * feat(contentsafety): add regex provider with config-driven scanning and allowlist Change-Id: I658889b3647cbbbde6881e0c5f7c13887a1eb1d4 * feat(contentsafety): add output core with mode parsing, path normalization, and scan orchestration Change-Id: I1cb9df75f1a4d176d660e2e7a9561314c3787191 * feat(contentsafety): add ScanForSafety entry point and Envelope alert field Change-Id: I5fdb311e1c8d983a35a58667970b9fd3ac729a5c * feat(contentsafety): integrate scanning into shortcut Out() and OutFormat() Change-Id: I33eef1dba14c8a9bd1998857311bdd611f33b916 * feat(contentsafety): integrate scanning into API/service output paths and register provider Change-Id: Ic3981db6c546a19eadea095d82175f92f4783bec * fix(contentsafety): emit stderr notice when lazy-creating default config Change-Id: Ia2491f7a17caceea3125ff9fb58d750dc196d7e7 * style: gofmt factory_default and exitcode Change-Id: I86c5afdfbbdb68d8137f0ca09ef3b5a1139f4b4e * fix(contentsafety): vfs for config I/O, mutex for lazy-create, sort matched rules, emit warn on --output path Change-Id: Ib4982cd54e1bfe0580a0eb03368e6ca818304e1b * fix(contentsafety): isolate scan goroutine errOut to prevent race on timeout Change-Id: Ia5a770d7387ba6d3b7fa318fc5f1384214ea10b7 * fix(contentsafety): deep-normalize typed slices so scanner can walk shortcut data Change-Id: I641e89113d1a2f2285ac6109bd3d7264f5845ea7 * fix(contentsafety): file perms 0600/0700, no result mutation, timeout test, scanTimeout comment Change-Id: Ie45a2e365ee7098e214e94f8871026cc12029d83
219 lines
7.0 KiB
Go
219 lines
7.0 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package client
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"mime"
|
|
"strings"
|
|
|
|
larkcore "github.com/larksuite/oapi-sdk-go/v3/core"
|
|
|
|
"github.com/larksuite/cli/extension/fileio"
|
|
"github.com/larksuite/cli/internal/output"
|
|
"github.com/larksuite/cli/internal/util"
|
|
)
|
|
|
|
// ── Response routing ──
|
|
|
|
// ResponseOptions configures how HandleResponse routes a raw API response.
|
|
type ResponseOptions struct {
|
|
OutputPath string // --output flag; "" = auto-detect
|
|
Format output.Format // output format for JSON responses
|
|
JqExpr string // if set, apply jq filter instead of Format
|
|
Out io.Writer // stdout
|
|
ErrOut io.Writer // stderr
|
|
FileIO fileio.FileIO // file transfer abstraction; required when saving files (--output or binary response)
|
|
CommandPath string // raw cobra CommandPath() for content safety scanning
|
|
// CheckError is called on parsed JSON results. Nil defaults to CheckLarkResponse.
|
|
CheckError func(interface{}) error
|
|
}
|
|
|
|
// HandleResponse routes a raw *larkcore.ApiResp to the appropriate output:
|
|
// 1. If Content-Type is JSON, check for business errors first (even with --output).
|
|
// 2. If --output is set and response is not a JSON error, save to file.
|
|
// 3. If Content-Type is non-JSON and no --output, auto-save binary to file.
|
|
func HandleResponse(resp *larkcore.ApiResp, opts ResponseOptions) error {
|
|
ct := resp.Header.Get("Content-Type")
|
|
check := opts.CheckError
|
|
if check == nil {
|
|
check = CheckLarkResponse
|
|
}
|
|
|
|
// Non-JSON error responses (e.g. 404 text/plain from gateway): return error directly
|
|
// instead of falling through to the binary-save path.
|
|
if resp.StatusCode >= 400 && !IsJSONContentType(ct) && ct != "" {
|
|
body := util.TruncateStrWithEllipsis(strings.TrimSpace(string(resp.RawBody)), 500)
|
|
return output.Errorf(httpExitCode(resp.StatusCode), "http_error", "HTTP %d: %s", resp.StatusCode, body)
|
|
}
|
|
|
|
// JSON responses: always check for business errors before saving.
|
|
if IsJSONContentType(ct) || ct == "" {
|
|
result, err := ParseJSONResponse(resp)
|
|
if err != nil {
|
|
return WrapJSONResponseParseError(err, resp.RawBody)
|
|
}
|
|
if apiErr := check(result); apiErr != nil {
|
|
return apiErr
|
|
}
|
|
// Content safety scanning
|
|
scanResult := output.ScanForSafety(opts.CommandPath, result, opts.ErrOut)
|
|
if scanResult.Blocked {
|
|
return scanResult.BlockErr
|
|
}
|
|
if opts.OutputPath != "" {
|
|
if scanResult.Alert != nil {
|
|
output.WriteAlertWarning(opts.ErrOut, scanResult.Alert)
|
|
}
|
|
return saveAndPrint(opts.FileIO, resp, opts.OutputPath, opts.Out)
|
|
}
|
|
if scanResult.Alert != nil {
|
|
output.WriteAlertWarning(opts.ErrOut, scanResult.Alert)
|
|
}
|
|
if opts.JqExpr != "" {
|
|
return output.JqFilter(opts.Out, result, opts.JqExpr)
|
|
}
|
|
output.FormatValue(opts.Out, result, opts.Format)
|
|
return nil
|
|
}
|
|
|
|
// Non-JSON (binary) responses.
|
|
if opts.JqExpr != "" {
|
|
return output.ErrValidation("--jq requires a JSON response (got Content-Type: %s)", ct)
|
|
}
|
|
if opts.OutputPath != "" {
|
|
return saveAndPrint(opts.FileIO, resp, opts.OutputPath, opts.Out)
|
|
}
|
|
|
|
// No --output: auto-save with derived filename.
|
|
meta, err := SaveResponse(opts.FileIO, resp, ResolveFilename(resp))
|
|
if err != nil {
|
|
return output.Errorf(output.ExitInternal, "file_error", "%s", err)
|
|
}
|
|
fmt.Fprintf(opts.ErrOut, "binary response detected (Content-Type: %s), saved to file\n", ct)
|
|
output.PrintJson(opts.Out, meta)
|
|
return nil
|
|
}
|
|
|
|
func saveAndPrint(fio fileio.FileIO, resp *larkcore.ApiResp, path string, w io.Writer) error {
|
|
meta, err := SaveResponse(fio, resp, path)
|
|
if err != nil {
|
|
return output.Errorf(output.ExitInternal, "file_error", "%s", err)
|
|
}
|
|
output.PrintJson(w, meta)
|
|
return nil
|
|
}
|
|
|
|
// ── JSON helpers ──
|
|
|
|
// IsJSONContentType reports whether the Content-Type header indicates a JSON response.
|
|
func IsJSONContentType(ct string) bool {
|
|
return strings.Contains(ct, "application/json") || strings.Contains(ct, "text/json")
|
|
}
|
|
|
|
// ParseJSONResponse decodes a raw SDK response body as JSON.
|
|
// CallAPI and HandleResponse both delegate to this function.
|
|
func ParseJSONResponse(resp *larkcore.ApiResp) (interface{}, error) {
|
|
var result interface{}
|
|
dec := json.NewDecoder(bytes.NewReader(resp.RawBody))
|
|
dec.UseNumber()
|
|
if err := dec.Decode(&result); err != nil {
|
|
return nil, fmt.Errorf("response parse error: %w (body: %s)", err, util.TruncateStr(string(resp.RawBody), 500))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ── File saving ──
|
|
|
|
// SaveResponse writes an API response body to the given outputPath and returns metadata.
|
|
// It delegates to FileIO.Save for path validation and atomic write; fio must not be nil.
|
|
func SaveResponse(fio fileio.FileIO, resp *larkcore.ApiResp, outputPath string) (map[string]interface{}, error) {
|
|
result, err := fio.Save(outputPath, fileio.SaveOptions{
|
|
ContentType: resp.Header.Get("Content-Type"),
|
|
ContentLength: int64(len(resp.RawBody)),
|
|
}, bytes.NewReader(resp.RawBody))
|
|
if err != nil {
|
|
var me *fileio.MkdirError
|
|
var we *fileio.WriteError
|
|
switch {
|
|
case errors.Is(err, fileio.ErrPathValidation):
|
|
return nil, fmt.Errorf("unsafe output path: %s", err)
|
|
case errors.As(err, &me):
|
|
return nil, fmt.Errorf("create directory: %s", err)
|
|
case errors.As(err, &we):
|
|
return nil, fmt.Errorf("cannot write file: %s", err)
|
|
default:
|
|
return nil, fmt.Errorf("cannot write file: %s", err)
|
|
}
|
|
}
|
|
|
|
resolvedPath, err := fio.ResolvePath(outputPath)
|
|
if err != nil || resolvedPath == "" {
|
|
resolvedPath = outputPath
|
|
}
|
|
return map[string]interface{}{
|
|
"saved_path": resolvedPath,
|
|
"size_bytes": result.Size(),
|
|
"content_type": resp.Header.Get("Content-Type"),
|
|
}, nil
|
|
}
|
|
|
|
// ResolveFilename picks a filename from the response headers.
|
|
// Priority: Content-Disposition filename > Content-Type extension > "download.bin".
|
|
func ResolveFilename(resp *larkcore.ApiResp) string {
|
|
if name := larkcore.FileNameByHeader(resp.Header); name != "" {
|
|
return name
|
|
}
|
|
return "download" + mimeToExt(resp.Header.Get("Content-Type"))
|
|
}
|
|
|
|
// mimeToExt maps a Content-Type to a file extension (with leading dot).
|
|
func mimeToExt(ct string) string {
|
|
if ct == "" {
|
|
return ".bin"
|
|
}
|
|
mediaType, _, _ := mime.ParseMediaType(ct)
|
|
switch mediaType {
|
|
case "application/pdf":
|
|
return ".pdf"
|
|
case "image/png":
|
|
return ".png"
|
|
case "image/jpeg":
|
|
return ".jpg"
|
|
case "image/gif":
|
|
return ".gif"
|
|
case "text/plain":
|
|
return ".txt"
|
|
case "text/csv":
|
|
return ".csv"
|
|
case "text/html":
|
|
return ".html"
|
|
case "application/zip":
|
|
return ".zip"
|
|
case "application/xml", "text/xml":
|
|
return ".xml"
|
|
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
|
return ".xlsx"
|
|
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
|
|
return ".docx"
|
|
case "application/vnd.openxmlformats-officedocument.presentationml.presentation":
|
|
return ".pptx"
|
|
default:
|
|
return ".bin"
|
|
}
|
|
}
|
|
|
|
// httpExitCode maps HTTP status ranges to CLI exit codes:
|
|
// 5xx → ExitNetwork (server error), 4xx → ExitAPI (client error).
|
|
func httpExitCode(status int) int {
|
|
if status >= 500 {
|
|
return output.ExitNetwork
|
|
}
|
|
return output.ExitAPI
|
|
}
|