Files
wehub-resource-sync bf9395e022
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:22:54 +08:00

110 lines
2.8 KiB
Go

// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package contentsafety
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"path/filepath"
"regexp"
"strings"
"github.com/larksuite/cli/internal/vfs"
)
const configFileName = "content-safety.json"
type Config struct {
Allowlist []string
Rules []rule
}
type rawConfig struct {
Allowlist []string `json:"allowlist"`
Rules []rawRule `json:"rules"`
}
type rawRule struct {
ID string `json:"id"`
Pattern string `json:"pattern"`
}
func LoadConfig(configDir string) (*Config, error) {
path := filepath.Join(configDir, configFileName)
data, err := vfs.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read content-safety config: %w", err)
}
var raw rawConfig
if err := json.Unmarshal(data, &raw); err != nil {
return nil, fmt.Errorf("parse content-safety config: %w", err)
}
rules := make([]rule, 0, len(raw.Rules))
for _, r := range raw.Rules {
compiled, err := regexp.Compile(r.Pattern)
if err != nil {
return nil, fmt.Errorf("compile rule %q pattern: %w", r.ID, err)
}
rules = append(rules, rule{ID: r.ID, Pattern: compiled})
}
return &Config{Allowlist: raw.Allowlist, Rules: rules}, nil
}
func EnsureDefaultConfig(configDir string, errOut io.Writer) error {
path := filepath.Join(configDir, configFileName)
if _, err := vfs.Stat(path); err == nil {
return nil
}
if err := vfs.MkdirAll(configDir, 0700); err != nil {
return fmt.Errorf("create config dir: %w", err)
}
data, err := json.MarshalIndent(defaultRawConfig(), "", " ")
if err != nil {
return fmt.Errorf("marshal default config: %w", err)
}
if err := vfs.WriteFile(path, append(data, '\n'), fs.FileMode(0600)); err != nil {
return err
}
fmt.Fprintf(errOut, "notice: created default content-safety config at %s\n", path)
return nil
}
func defaultRawConfig() rawConfig {
return rawConfig{
Allowlist: []string{"all"},
Rules: []rawRule{
{
ID: "instruction_override",
Pattern: `(?i)ignore\s+(all\s+|any\s+|the\s+)?(previous|prior|above|earlier)\s+(instructions?|prompts?|directives?)`,
},
{
ID: "role_injection",
Pattern: `(?i)<\s*/?\s*(system|assistant|tool|user|developer)\s*>`,
},
{
ID: "system_prompt_leak",
Pattern: `(?i)\b(reveal|print|show|output|display|repeat)\s+(your|the|all)\s+(system\s+|initial\s+|original\s+)?(prompt|instructions?|rules?)`,
},
{
ID: "delimiter_smuggle",
Pattern: `<\|im_(start|end|sep)\|>|<\|endoftext\|>|###\s*(system|assistant|user)\s*:`,
},
},
}
}
func IsAllowlisted(cmdPath string, allowlist []string) bool {
for _, entry := range allowlist {
if strings.EqualFold(entry, "all") {
return true
}
if cmdPath == entry || strings.HasPrefix(cmdPath, entry+".") {
return true
}
}
return false
}