Skip to content

Conversation

@Tinyblargon
Copy link
Collaborator

Fix issue where we parsed the raw username when using an API token, causing a panic.

@Tinyblargon Tinyblargon requested a review from Copilot September 17, 2025 20:42
@Tinyblargon Tinyblargon self-assigned this Sep 17, 2025
@Tinyblargon Tinyblargon added type/bug modifies/go Pull requests that update Go code size/S Denotes a PR that changes 10-29 lines, ignoring generated files test/done This PR has been tested and the result was succesfull labels Sep 17, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a panic that occurs when parsing usernames for API token authentication in the Proxmox client. The issue was caused by attempting to parse a raw username when using an API token, which has a different format than regular usernames.

  • Replaces strings.SplitN with strings.IndexRune for more robust parsing
  • Adds conditional logic to handle both username and API token scenarios
  • Extracts user and realm information from API tokens when username is not set

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

var user UserID
if c.Username != "" {
index := strings.IndexRune(c.Username, '@')
user = UserID{Name: c.Username[:index], Realm: c.Username[index+1:]}
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

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

If '@' is not found in c.Username, IndexRune returns -1, which will cause a panic when used for slicing. Add a check to ensure index >= 0 before slicing.

Suggested change
user = UserID{Name: c.Username[:index], Realm: c.Username[index+1:]}
if index >= 0 {
user = UserID{Name: c.Username[:index], Realm: c.Username[index+1:]}
} else {
// Handle missing '@' gracefully; fallback to entire username as Name, empty Realm
user = UserID{Name: c.Username, Realm: ""}
}

Copilot uses AI. Check for mistakes.
Comment on lines +157 to +158
indexEx := strings.IndexRune(token[indexAt+1:], '!')
user = UserID{Name: token[:indexAt], Realm: token[indexAt+1 : indexAt+indexEx+1]}
Copy link

Copilot AI Sep 17, 2025

Choose a reason for hiding this comment

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

If '@' or '!' characters are not found in the token, IndexRune returns -1, which will cause panics during slicing operations. Add bounds checking for both indexAt and indexEx before using them for slicing.

Suggested change
indexEx := strings.IndexRune(token[indexAt+1:], '!')
user = UserID{Name: token[:indexAt], Realm: token[indexAt+1 : indexAt+indexEx+1]}
if indexAt == -1 {
user = UserID{Name: "", Realm: ""}
} else {
indexEx := strings.IndexRune(token[indexAt+1:], '!')
if indexEx == -1 {
user = UserID{Name: "", Realm: ""}
} else {
user = UserID{Name: token[:indexAt], Realm: token[indexAt+1 : indexAt+indexEx+1]}
}
}

Copilot uses AI. Check for mistakes.
@Tinyblargon Tinyblargon merged commit 080d9b1 into Telmate:master Sep 17, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

modifies/go Pull requests that update Go code size/S Denotes a PR that changes 10-29 lines, ignoring generated files test/done This PR has been tested and the result was succesfull type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant