Skip to content

Commit

Permalink
Check file is globally accessible (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
joecorall authored Nov 19, 2024
1 parent deb1284 commit 0fb733d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
18 changes: 14 additions & 4 deletions internal/handlers/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,13 @@ func CheckMyWork(w http.ResponseWriter, r *http.Request) {
// make sure the file exists in the filesystem
case "File Path", "Supplemental File":
filename := strings.ReplaceAll(cell, `\`, `/`)
filename = strings.TrimLeft(filename, "/")
if len(filename) > 3 && filename[0:3] != "mnt" {
filename = fmt.Sprintf("/mnt/islandora_staging/%s", filename)
// we're testing with /tmp files
if len(filename) < 6 || filename[0:5] != "/tmp/" {
// but need to make sure we're mapping /mnt/islandora_staging into /data in docker
filename = strings.TrimLeft(filename, "/")
if len(filename) > 3 && filename[0:3] != "mnt" {
filename = fmt.Sprintf("/mnt/islandora_staging/%s", filename)
}
}

filename = strings.ReplaceAll(filename, "/mnt/islandora_staging", "/data")
Expand Down Expand Up @@ -251,7 +255,13 @@ func fileExists(filename string) bool {
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
if !info.Mode().IsRegular() {
return false
}

mode := info.Mode().Perm()
// Check if the file is globally readable
return mode&0004 != 0
}

func authRequest(w http.ResponseWriter, r *http.Request) bool {
Expand Down
53 changes: 53 additions & 0 deletions internal/handlers/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ import (
)

func TestCheckMyWork(t *testing.T) {
files := []struct {
name string
permissions os.FileMode
expectAccess bool
}{
{"/tmp/test_readable.txt", 0644, true}, // Readable globally
{"/tmp/test_writable.txt", 0666, true}, // Writable globally
{"/tmp/test_private.txt", 0600, false}, // Not accessible globally
}

// Create test files
for _, file := range files {
if err := os.WriteFile(file.name, []byte("test content"), file.permissions); err != nil {
t.Fatalf("Failed to create test file %s: %v", file.name, err)
}
}
defer func() {
for _, file := range files {
_ = os.Remove(file.name)
}
}()

tests := []struct {
name string
method string
Expand Down Expand Up @@ -112,6 +134,37 @@ func TestCheckMyWork(t *testing.T) {
statusCode: http.StatusOK,
response: `{"D2":"Missing source file"}`,
},
{
name: "OK file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "/tmp/test_readable.txt"},
},
statusCode: http.StatusOK,
response: `{}`,
},
{
name: "OK file (rw)",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "/tmp/test_writable.txt"},
},
statusCode: http.StatusOK,
response: `{}`,
},

{
name: "Unreadable file",
method: http.MethodPost,
body: [][]string{
{"Title", "Object Model", "Full Title", "File Path"},
{"foo", "Image", "foo", "/tmp/test_private.txt"},
},
statusCode: http.StatusOK,
response: `{"D2":"File does not exist in islandora_staging"}`,
},
{
name: "Missing file OK",
method: http.MethodPost,
Expand Down

0 comments on commit 0fb733d

Please sign in to comment.