Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ func (rc *ResourceCommand) Create(ctx context.Context, client *authclient.Client

var reader io.Reader
if rc.filename == "" {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) != 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Handle stdin Stat errors before using FileInfo

When tctl create is invoked without a filename and stdin is closed or otherwise invalid, os.Stdin.Stat() returns a nil FileInfo with an error; ignoring that error makes the next line call stat.Mode() and panic instead of returning a normal CLI error. This can happen in automation that closes fd 0, and it is a regression from the decoder path returning an error rather than crashing. Please check and return/wrap the Stat error before inspecting the mode.

AGENTS.md reference: AGENTS.md:L16-L16

Useful? React with 👍 / 👎.

return trace.BadParameter("no file specified or input via stdin")
}
reader = os.Stdin
} else {
f, err := utils.OpenFileAllowingUnsafeLinks(rc.filename)
Expand Down
13 changes: 13 additions & 0 deletions tool/tctl/common/resource_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,10 @@ func TestCreateResources(t *testing.T) {
kind: "empty-doc",
create: testCreateWithEmptyDocument,
},
{
kind: "no-file-or-input",
create: testCreateWithNoFileOrInput,
},
{
kind: types.KindDatabaseObjectImportRule,
create: testCreateDatabaseObjectImportRule,
Expand Down Expand Up @@ -2780,6 +2784,15 @@ spec:
require.NoError(t, err)
}

func testCreateWithNoFileOrInput(t *testing.T, clt *authclient.Client) {
prevStdin := os.Stdin
os.Stdin, _ = os.Open(os.DevNull)
t.Cleanup(func() { os.Stdin = prevStdin })

_, err := runResourceCommand(t, clt, []string{"create"})
require.ErrorContains(t, err, "no file specified or input via stdin")
}

func testCreateUser(t *testing.T, clt *authclient.Client) {
// Ensure that our test user does not exist
_, err := runResourceCommand(t, clt, []string{"get", types.KindUser + "/llama", "--format=json"})
Expand Down
Loading