Skip to content

Conversation

@prkalle
Copy link
Contributor

@prkalle prkalle commented Oct 21, 2025

Description of the Change

Problem:
When cf cli receives binary data from the API, such as when downloading a droplet with the command

cf curl /v3/droplets/48155813-038b-4fbd-912d-7f8856c39f9c/download > /tmp/droplet
it crashes while trying to process the binary data.

Users can provide the

 --output ./binary/file 

parameter as a workaround.

e.g:

❯ cf curl  /v3/droplets/42c4ddb3-9d3e-4cce-a818-27bdb68876d2/download > /tmp/droplet

		Something unexpected happened. This is a bug in cf.

		Please re-run the command that caused this exception with the environment
		variable CF_TRACE set to true.

		Also, please update to the latest cli and try the command again:
		https://code.cloudfoundry.org/cli/releases

		Please create an issue at: https://code.cloudfoundry.org/cli/issues

		Include the below information when creating the issue:

		Command
		cf curl /v3/droplets/42c4ddb3-9d3e-4cce-a818-27bdb68876d2/download

		CLI Version
		99.0.0-dev-7b6f41e+build1061000

		Error
		template: Display Text:656: unrecognized character in action: U+FFFD '�'

		Stack Trace
			goroutine 1 [running]:
	code.cloudfoundry.org/cli/util/panichandler.HandlePanic()
		/Users/pkalle/go/pkg/mod/code.cloudfoundry.org/cli@v0.0.0-20250924014223-4a50d59e60cf/util/panichandler/handler.go:19 +0x4b
	panic({0x51d35a0?, 0xc00080c090?})
		/Users/pkalle/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.0.darwin-amd64/src/runtime/panic.go:783 +0x136
	text/template.Must(0xc00055c000?, {0x5466a68?, 0xc00080c090?})
		/Users/pkalle/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.25.0.darwin-amd64/src/text/template/helper.go:26 +0x2f
	code.cloudfoundry.org/cli/util/ui.generateTranslationFunc.func1({0xc000a82000, 0x68c8e0}, {0xc00080c020, 0x1, 0x3fe9295?})
		/Users/pkalle/go/pkg/mod/code.cloudfoundry.org/cli@v0.0.0-20250924014223-4a50d59e60cf/util/ui/i18n.go:119 +0xde
	code.cloudfoundry.org/cli/util/ui.(*UI).TranslateText(0xc0001bab00, {0xc000a82000, 0x68c8e0}, {0x0?, 0x0?, 0x3f7d500?})
		/Users/pkalle/go/pkg/mod/code.cloudfoundry.org/cli@v0.0.0-20250924014223-4a50d59e60cf/util/ui/ui.go:406 +0x95
	code.cloudfoundry.org/cli/util/ui.(*UI).DisplayText(0xc0001bab00, {0xc000a82000, 0x68c8e0}, {0x0, 0x0, 0x0})
		/Users/pkalle/go/pkg/mod/code.cloudfoundry.org/cli@v0.0.0-20250924014223-4a50d59e60cf/util/ui/ui.go:248 +0xaa
	code.cloudfoundry.org/cli/command/v7.CurlCommand.Execute({{{0x5490fd0, 0xc0001bab00}, {0x54947a0, 0xc000634400}, {0x547ea48, 0xc000612560}, {0x549b880, 0xc0006f8a10}, 0xc000774f08, 0xc0005e2720}, ...}, ...)
		/Users/pkalle/go/pkg/mod/code.cloudfoundry.org/cli@v0.0.0-20250924014223-4a50d59e60cf/command/v7/curl_command.go:58 +0x29a
	github.gwd.broadcom.net/TNZ/tanzu-cf-cli-runtime/command_parser.(*CommandParser).getExecutionWrapper.func1({0x4e471480, 0x5fd94d0}, {0xc000612400, 0x0, 0x2})

Solution:

The fix is to detect when the response contains binary data and by writing the raw bytes directly to stdout instead of converting to string, we avoid data corruption.

Why Is This PR Valuable?

This PR would help users trying to download the binary data and trying to redirect the output using a pipe

Applicable Issues

List any applicable GitHub Issues here

How Urgent Is The Change?

Medium

Copy link
Member

@gururajsh gururajsh left a comment

Choose a reason for hiding this comment

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

Mostly LGTM. Just one question.

}

// Check if the response contains binary data
if IsBinary(responseBodyBytes) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use utf8.Valid(bytesToWrite)? Avoids having custom check.

Copy link
Contributor Author

@prkalle prkalle Oct 23, 2025

Choose a reason for hiding this comment

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

Neither checking for presnece of null bytes in the response data nor checking for valid utf8 encoding data is sufficient/foolproof. I chose the cheking for presence of null bytes because curl library does the same check to determine the binary data. However , I updated the isBinary() function to rely on Content-Type for known binary MIME types and only use the above check ( presence of null bytes to determine the binray data) for other MIME types. Thanks

Copy link
Member

Choose a reason for hiding this comment

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

Thank you!

@prkalle prkalle force-pushed the fix/curl_binary_output_v8 branch from ee84d28 to d45bbe1 Compare October 23, 2025 21:55
Copy link
Member

@gururajsh gururajsh left a comment

Choose a reason for hiding this comment

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

LGTM

@gururajsh gururajsh merged commit b37f66e into cloudfoundry:v8 Oct 27, 2025
18 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants