{"record":{"id":"a7469cce8b866312","repo":"larksuite/cli","slug":"header-q-is-protected-rerun-with-allow-protected","errorCode":null,"errorMessage":"header %q is protected; rerun with allow_protected_header_edits","messagePattern":"header %q is protected; rerun with allow_protected_header_edits","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/mail/draft/patch.go","lineNumber":152,"sourceCode":"\t\t}\n\t\treturn removeInline(snapshot, partID)\n\tcase \"insert_signature\":\n\t\treturn insertSignatureOp(snapshot, op)\n\tcase \"remove_signature\":\n\t\treturn removeSignatureOp(snapshot)\n\tcase \"set_calendar\":\n\t\treturn applyCalendarSet(snapshot, op.CalendarICS)\n\tcase \"remove_calendar\":\n\t\treturn applyCalendarRemove(snapshot)\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported patch op %q\", op.Op)\n\t}\n\treturn nil\n}\n\nfunc ensureHeaderEditable(name string, options PatchOptions) error {\n\tif protectedHeaders[strings.ToLower(strings.TrimSpace(name))] && !options.AllowProtectedHeaderEdits {\n\t\treturn fmt.Errorf(\"header %q is protected; rerun with allow_protected_header_edits\", name)\n\t}\n\treturn nil\n}\n\nfunc setRecipients(snapshot *DraftSnapshot, field string, addrs []Address) error {\n\tfield = strings.ToLower(strings.TrimSpace(field))\n\tif !isRecipientField(field) {\n\t\treturn fmt.Errorf(\"recipient field must be one of to/cc/bcc\")\n\t}\n\tnormalized := make([]Address, 0, len(addrs))\n\tseen := map[string]bool{}\n\tfor _, addr := range addrs {\n\t\tif strings.TrimSpace(addr.Address) == \"\" {\n\t\t\treturn fmt.Errorf(\"recipient address is empty\")\n\t\t}\n\t\tkey := strings.ToLower(strings.TrimSpace(addr.Address))\n\t\tif seen[key] {\n\t\t\tcontinue","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/mail/draft/patch.go#L134-L170","documentation":"ensureHeaderEditable rejects edits to headers listed in protectedHeaders unless PatchOptions.AllowProtectedHeaderEdits is true. The library protects structural headers (like Subject handling, MIME headers, or transport headers) from accidental modification because changing them can corrupt the draft's semantics.","triggerScenarios":"Calling Apply with a 'set_header' or 'remove_header' op whose Name (case-insensitive, trimmed) is in the protected set, without setting PatchOptions.AllowProtectedHeaderEdits.","commonSituations":"Trying to override headers such as MIME-Version, Content-Type, or similar structural headers via a generic header op; migrating scripts that previously rewrote these headers directly.","solutions":["If the edit is intentional, set PatchOptions{AllowProtectedHeaderEdits: true}.","Use the dedicated ops instead of generic set_header (e.g. set_subject for Subject).","Check the protectedHeaders set in patch.go to see which names require the flag.","Restrict AllowProtectedHeaderEdits to trusted admin flows; keep it false by default."],"exampleFix":"// before\nerr := Apply(ctx, dctx, ops, PatchOptions{})\n// after\nerr := Apply(ctx, dctx, ops, PatchOptions{AllowProtectedHeaderEdits: true})","handlingStrategy":"validation","validationCode":"var protectedHeaders = map[string]bool{ /* mirror patch.go set */ }\nfunc needsProtectedFlag(name string) bool {\n    return protectedHeaders[strings.ToLower(strings.TrimSpace(name))]\n}","typeGuard":"func isEditableHeader(name string, opts PatchOptions) bool {\n    return opts.AllowProtectedHeaderEdits || !protectedHeaders[strings.ToLower(strings.TrimSpace(name))]\n}","tryCatchPattern":"if err := Apply(ctx, dctx, ops, opts); err != nil {\n    if strings.Contains(err.Error(), \"is protected\") {\n        return fmt.Errorf(\"use dedicated op or set AllowProtectedHeaderEdits=true: %w\", err)\n    }\n    return err\n}","preventionTips":["Use dedicated ops (e.g. set_subject) instead of generic set_header for known fields.","Enable AllowProtectedHeaderEdits only in trusted/admin flows.","Check the protectedHeaders map before writing header-editing code paths.","Keep the flag false by default and pass it explicitly where needed."],"tags":["email","draft-patch","protected-header"],"backgroundTag":"protected-header-modification","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}