larksuite/cli · error

parse content-safety config: %w

Error message

parse content-safety config: %w

What it means

LoadConfig read the content-safety config file successfully but json.Unmarshal rejected it - the file exists yet is not the expected {allowlist, rules:[{id,pattern}]} JSON shape; the wrapped error names the syntax position.

Source

Thrown at internal/security/contentsafety/config.go:43

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 {

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Run the file through a JSON linter (jq or python -m json.tool) to find the syntax error
  2. Restore the default config: delete the file and let EnsureDefaultConfig recreate it
  3. Fix the JSON structure to match rawConfig: {"allowlist": [...], "rules": [{"id":..., "pattern":...}]}

Example fix

// before
{ "rules": [ {"id": "a", "pattern": "x",} ] }  // trailing comma
// after
{ "rules": [ {"id": "a", "pattern": "x"} ] }
Defensive patterns

Strategy: fallback

Validate before calling

if err := json.Valid(data); !err {
	return fmt.Errorf("config file contains invalid JSON")
}

Try / catch

config, err := contentsafety.LoadConfig(dir)
if err != nil {
	if strings.Contains(err.Error(), "parse content-safety config") {
		// back up the broken file, then regenerate defaults
		os.Rename(path, path+".bak")
		contentsafety.EnsureDefaultConfig(dir, os.Stderr)
		config, err = contentsafety.LoadConfig(dir)
	}
	if err != nil { return err }
}

Prevention

When it happens

Trigger: Calling LoadConfig when the config file contains malformed JSON (syntax errors), was truncated mid-write, or has a top-level shape that cannot unmarshal into rawConfig (e.g. an array instead of an object).

Common situations: Hand-editing the config and introducing a syntax error (trailing comma, missing quote); a partially written file from a crashed process; accidental overwrite of the config with logs or HTML (captive portal).

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/3eaa88b32d21f8ee. Report an issue: GitHub.