Skip to content

strip control characters from guest cloud-init log lines - #5426

Open
Nashit-h wants to merge 2 commits into
lima-vm:masterfrom
Nashit-h:cloudinit-log-control-chars
Open

strip control characters from guest cloud-init log lines#5426
Nashit-h wants to merge 2 commits into
lima-vm:masterfrom
Nashit-h:cloudinit-log-control-chars

Conversation

@Nashit-h

Copy link
Copy Markdown
Contributor

What This PR Changes

The hostagent reads cloud-init log lines from the guest over SSH and forwards them in CloudInitProgress events, which limactl start and limactl watch print to the operator's terminal with a bare %s. A guest controls those bytes (anything in /var/log/cloud-init-output.log or the cloud-init journald output), so it can emit ANSI/OSC escape sequences and spoof or rewrite the terminal during the default start progress display. This strips non-printable runes at the single emit choke point (emitCloudInitProgressEvent) so every consumer is safe, matching the removeControlChars handling already used for other guest-controlled strings and the %#q quoting used for the sibling guest-error path.

Linked Issue (Required in most cases)

Small self-contained security fix; no separate tracking issue.

How I Tested This

Added a regression test that runs an escape sequence through emitCloudInitProgressEvent and asserts the emitted LogLine has no control characters. It fails on the current code (the \x1b survives) and passes with this change. go test ./pkg/hostagent/... and the repo-pinned golangci-lint run ./pkg/hostagent/ are green.

AI Usage

Signed-off-by: nashit hayyat <nashit@bugqore.com>
Comment thread pkg/hostagent/hostagent.go Outdated

// removeControlChars drops non-printable runes so guest-controlled text is safe
// to print to the operator's terminal.
func removeControlChars(s string) string {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Duplicated code:

lima/pkg/cidata/cidata.go

Lines 499 to 507 in 20dbdc0

func removeControlChars(s string) string {
out := make([]rune, 0, len(s))
for _, r := range s {
if unicode.IsPrint(r) {
out = append(out, r)
}
}
return string(out)
}

Should be moved to pkg/strutil

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. Moved it to strutil.RemoveControlChars and switched both cidata and the hostagent path over to it.

Signed-off-by: nashit hayyat <nashit@bugqore.com>
@AkihiroSuda

Copy link
Copy Markdown
Member

Please squash the commits

@AkihiroSuda AkihiroSuda added this to the v2.3.0 milestone Aug 24, 2026
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