Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 2 additions & 2 deletions internal/scan/shodan.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ func truncateBanner(banner string, maxLen int) string {
banner = strings.ReplaceAll(banner, "\r\n", " ")
banner = strings.ReplaceAll(banner, "\n", " ")

if len(banner) > maxLen {
return banner[:maxLen] + "..."
if runes := []rune(banner); len(runes) > maxLen {
return string(runes[:maxLen]) + "..."
}
return banner
}
Expand Down
7 changes: 7 additions & 0 deletions internal/scan/shodan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"net/http/httptest"
"testing"
"time"
"unicode/utf8"
)

func TestResolveHostname_IP(t *testing.T) {
Expand Down Expand Up @@ -50,13 +51,19 @@ func TestTruncateBanner(t *testing.T) {
{"this is a long banner", 10, "this is a ..."},
{"with\nnewlines\r\n", 50, "with newlines"},
{" trimmed ", 50, "trimmed"},
// truncation must land on a rune boundary, not split a multibyte rune
{"aaaé", 4, "aaaé"},
{"日本語テスト", 3, "日本語..."},
}

for _, tt := range tests {
result := truncateBanner(tt.input, tt.maxLen)
if result != tt.expected {
t.Errorf("truncateBanner(%q, %d) = %q, want %q", tt.input, tt.maxLen, result, tt.expected)
}
if !utf8.ValidString(result) {
t.Errorf("truncateBanner(%q, %d) produced invalid UTF-8: %q", tt.input, tt.maxLen, result)
}
}
}

Expand Down
Loading