-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(base): require delete approval auth code for destructive shortcuts #1816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package base | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| const deleteApprovalValidatePath = "/space/api/base/delete/validate-auth-code" | ||
| const deleteApprovalRequestPath = "/space/api/base/delete/requests" | ||
|
|
||
| type deleteApprovalSpec struct { | ||
| Action string | ||
| BaseToken string | ||
| ResourceType string | ||
| ResourceID string | ||
| } | ||
|
|
||
| func deleteApprovalFlags() []common.Flag { | ||
| return []common.Flag{ | ||
| {Name: "auth-code", Desc: "one-time delete authorization code returned by the Base delete approval page"}, | ||
| {Name: "prepare-approval", Type: "bool", Desc: "create a delete approval request instead of executing the delete"}, | ||
| } | ||
| } | ||
|
|
||
| func appendDeleteApprovalFlags(flags ...common.Flag) []common.Flag { | ||
| return append(flags, deleteApprovalFlags()...) | ||
| } | ||
|
|
||
| func handleDeleteApproval(runtime *common.RuntimeContext, spec deleteApprovalSpec) (bool, error) { | ||
| if runtime.Bool("prepare-approval") { | ||
| return true, prepareDeleteApproval(runtime, spec) | ||
| } | ||
| if strings.TrimSpace(runtime.Str("auth-code")) == "" { | ||
| return false, baseFlagErrorf("--auth-code is required for %s; run with --prepare-approval first to create an approval URL", spec.Action) | ||
| } | ||
| return false, validateDeleteApproval(runtime, spec) | ||
| } | ||
|
|
||
| func prepareDeleteApproval(runtime *common.RuntimeContext, spec deleteApprovalSpec) error { | ||
| data, err := runtime.CallAPITyped(http.MethodPost, deleteApprovalRequestPath, nil, map[string]interface{}{ | ||
| "action": spec.Action, | ||
| "base_token": spec.BaseToken, | ||
| "resource_type": spec.ResourceType, | ||
| "resource_id": spec.ResourceID, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| runtime.Out(map[string]interface{}{ | ||
| "approval_required": true, | ||
| "request_id": common.GetString(data, "request_id"), | ||
| "approval_url": common.GetString(data, "approval_url"), | ||
| "expires_at": data["expires_at"], | ||
| "request_digest": common.GetString(data, "request_digest"), | ||
| "action": spec.Action, | ||
| "base_token": spec.BaseToken, | ||
| "resource_type": spec.ResourceType, | ||
| "resource_id": spec.ResourceID, | ||
| }, nil) | ||
| return nil | ||
| } | ||
|
|
||
| func validateDeleteApproval(runtime *common.RuntimeContext, spec deleteApprovalSpec) error { | ||
| data, err := runtime.CallAPITyped(http.MethodPost, deleteApprovalValidatePath, nil, map[string]interface{}{ | ||
| "auth_code": strings.TrimSpace(runtime.Str("auth-code")), | ||
| "action": spec.Action, | ||
| "base_token": spec.BaseToken, | ||
| "resource_type": spec.ResourceType, | ||
| "resource_id": spec.ResourceID, | ||
| "request_digest": deleteApprovalDigest(spec), | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if code := common.GetString(data, "error_code"); code != "" { | ||
| msg := common.GetString(data, "error_message") | ||
| if msg == "" { | ||
| msg = code | ||
| } | ||
| return baseValidationErrorf("delete authorization failed: %s", msg) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func deleteApprovalDigest(spec deleteApprovalSpec) string { | ||
| sum := sha256.Sum256([]byte(fmt.Sprintf("%s|%s|%s|%s", spec.Action, spec.BaseToken, spec.ResourceType, spec.ResourceID))) | ||
| return hex.EncodeToString(sum[:]) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: larksuite/cli
Length of output: 151
🏁 Script executed:
Repository: larksuite/cli
Length of output: 22113
🏁 Script executed:
Repository: larksuite/cli
Length of output: 440
🏁 Script executed:
Repository: larksuite/cli
Length of output: 24431
🏁 Script executed:
Repository: larksuite/cli
Length of output: 12405
🏁 Script executed:
Repository: larksuite/cli
Length of output: 6173
🏁 Script executed:
Repository: larksuite/cli
Length of output: 577
Add targeted tests for the delete-approval helper branches.
base_execute_test.gocovers the delete commands’ happy paths, butdelete_approval.gostill needs direct coverage for--prepare-approval, missing--auth-code, validationerror_coderesponses, anddeleteApprovalDigest.🤖 Prompt for AI Agents
Source: Coding guidelines