access_log_parse_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package service
  2. import "testing"
  3. // Xray access-log lines carry attacker-influenced content (a client's requested
  4. // destination is logged verbatim) and can be truncated. parseAccessLogFields
  5. // must never panic on a short or malformed line, and must still parse a
  6. // well-formed line correctly.
  7. func TestParseAccessLogFields(t *testing.T) {
  8. malformed := []string{
  9. "",
  10. "singletoken",
  11. "2024/01/02",
  12. "2024/01/02 15:04:05.000000 from",
  13. "2024/01/02 15:04:05.000000 accepted",
  14. "2024/01/02 15:04:05.000000 email:",
  15. }
  16. for _, line := range malformed {
  17. func() {
  18. defer func() {
  19. if r := recover(); r != nil {
  20. t.Errorf("parseAccessLogFields panicked on %q: %v", line, r)
  21. }
  22. }()
  23. _ = parseAccessLogFields(line)
  24. }()
  25. }
  26. line := "2024/01/02 15:04:05.123456 from 1.2.3.4:555 accepted tcp:example.com:443 [inbound-tag >> outbound-tag] email: [email protected]"
  27. entry := parseAccessLogFields(line)
  28. if entry.FromAddress != "1.2.3.4:555" {
  29. t.Errorf("FromAddress = %q, want %q", entry.FromAddress, "1.2.3.4:555")
  30. }
  31. if entry.ToAddress != "tcp:example.com:443" {
  32. t.Errorf("ToAddress = %q, want %q", entry.ToAddress, "tcp:example.com:443")
  33. }
  34. if entry.Inbound != "inbound-tag" {
  35. t.Errorf("Inbound = %q, want %q", entry.Inbound, "inbound-tag")
  36. }
  37. if entry.Outbound != "outbound-tag" {
  38. t.Errorf("Outbound = %q, want %q", entry.Outbound, "outbound-tag")
  39. }
  40. if entry.Email != "[email protected]" {
  41. t.Errorf("Email = %q, want %q", entry.Email, "[email protected]")
  42. }
  43. if entry.DateTime.IsZero() {
  44. t.Error("DateTime was not parsed from a well-formed line")
  45. }
  46. }