fix(agent): read TOKEN_FILE like KEY_FILE instead of sending the whole file (#2276)

This commit is contained in:
Ryan Chou
2026-08-30 22:16:43 +08:00
committed by GitHub
parent 467f176713
commit 6fe268e463
2 changed files with 57 additions and 1 deletions

View File

@@ -87,7 +87,27 @@ func getToken() (string, error) {
if err != nil {
return "", err
}
return strings.TrimSpace(string(tokenBytes)), nil
return parseTokenFile(string(tokenBytes), tokenFile)
}
// parseTokenFile reads a single token from TOKEN_FILE.
// Blank lines and comments are ignored. Multiple tokens are rejected because
// the agent supports only one outbound hub connection.
func parseTokenFile(contents, path string) (string, error) {
var token string
for line := range strings.Lines(contents) {
line = strings.TrimSpace(line)
if len(line) == 0 || strings.HasPrefix(line, "#") {
continue
}
if token != "" {
return "", fmt.Errorf("%s must contain a single token", path)
}
token = line
}
// An empty file keeps returning an empty token, as before: the caller decides
// what to do about it.
return token, nil
}
// getOptions returns the WebSocket client options, creating them if necessary.