Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some checks to ensure that the matched substring matches the expectations, thereby avoiding potential errors #4922

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions tools/horizon-cmp/getPathFromAccessLog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import "testing"

func TestGetPathFromAccessLog(t *testing.T) {
validLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /page.html HTTP/1.0\" 200 2326"
invalidLengthLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET /page.html HTTP/1.0\" 200"
invalidPathLine := "127.0.0.1 - - [10/Oct/2000:13:55:36 -0700] \"GET //page.html HTTP/1.0\" 200 2326"

expectedPath := "/page.html"

// Test valid line
path, err := getPathFromAccessLog(validLine)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
if path != expectedPath {
t.Errorf("Expected path %q, but got %q", expectedPath, path)
}

// Test invalid length line
_, err = getPathFromAccessLog(invalidLengthLine)
if err == nil {
t.Error("Expected error, but got nil")
}

// Test invalid path line
_, err = getPathFromAccessLog(invalidPathLine)
if err == nil {
t.Error("Expected error, but got nil")
}
}
11 changes: 11 additions & 0 deletions tools/horizon-cmp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,10 +294,21 @@ func getPathFromAccessLog(line string) (string, error) {
return "", errors.Errorf("Can't find match: %s", line)
}

// Check for expected length
if len(matches[0]) != len(line) {
return "", errors.Errorf("Found invalid match length in line: %s", line)
}

if matches[1] != "GET" {
return "", nil
}

// Check for expected path value
newpath := matches[2]
if newpath == "" || strings.Contains(newpath, "//") {
return "", errors.Errorf("Found invalid match value in line: %s", line)
}

// Remove duplicate /
path := "/" + strings.TrimLeft(matches[2], "/")
return path, nil
Expand Down