Skip to content

Commit

Permalink
Fix missing link target
Browse files Browse the repository at this point in the history
for listing the file system with a link, the executing user has not enough privileges to read the target.
  • Loading branch information
mpass99 committed Aug 20, 2024
1 parent 8390b90 commit 5758a0e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
9 changes: 6 additions & 3 deletions pkg/nullio/ls2json.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,14 @@ func (w *Ls2JsonWriter) parseFileHeader(matches [][]byte) ([]byte, error) {
if entryType == dto.EntryTypeLink {
parts := strings.Split(string(name), " -> ")
const NumberOfPartsInALink = 2
if len(parts) == NumberOfPartsInALink {
switch len(parts) {
case NumberOfPartsInALink:
name = dto.FilePath(parts[0])
linkTarget = dto.FilePath(parts[1])
} else {
log.WithContext(w.sentrySpan.Context()).Error("could not split link into name and target")
case 1:
// This case happens when a user tries to read the target of a link without permission. See #596.
default:
log.WithContext(w.sentrySpan.Context()).WithField("name", name).Error("could not split link into name and target")

Check warning on line 173 in pkg/nullio/ls2json.go

View check run for this annotation

Codecov / codecov/patch

pkg/nullio/ls2json.go#L172-L173

Added lines #L172 - L173 were not covered by tests
}
}

Expand Down
41 changes: 37 additions & 4 deletions tests/e2e/runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (s *E2ETestSuite) TestListFileSystem_Nomad() {
ExecutionEnvironmentID: tests.DefaultEnvironmentIDAsInteger,
})
s.Require().NoError(err)
getFileURL, err := url.Parse(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath))
s.Require().NoError(err)

s.Run("No files", func() {
getFileURL, err := url.Parse(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath))
s.Require().NoError(err)
response, err := http.Get(getFileURL.String())
s.Require().NoError(err)
s.Equal(http.StatusOK, response.StatusCode)
Expand All @@ -120,8 +120,6 @@ func (s *E2ETestSuite) TestListFileSystem_Nomad() {
s.Require().NoError(err)
s.Equal(http.StatusNoContent, resp.StatusCode)

getFileURL, err := url.Parse(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.UpdateFileSystemPath))
s.Require().NoError(err)
response, err := http.Get(getFileURL.String())
s.Require().NoError(err)
s.Equal(http.StatusOK, response.StatusCode)
Expand All @@ -138,6 +136,41 @@ func (s *E2ETestSuite) TestListFileSystem_Nomad() {
s.Equal("root", fileHeader.Group)
s.Equal("rwxr--r--", fileHeader.Permissions)
})

s.Run("With links", func() {
s.Run("allowed", func() {
path := "/proc/self/cwd"
getFileURL.RawQuery = url.Values{api.PathKey: []string{path}}.Encode()

response, err := http.Get(getFileURL.String())
s.Require().NoError(err)
s.Equal(http.StatusOK, response.StatusCode)
listFilesResponse := new(dto.ListFileSystemResponse)
err = json.NewDecoder(response.Body).Decode(listFilesResponse)
s.Require().NoError(err)
s.Require().Equal(1, len(listFilesResponse.Files))
fileHeader := listFilesResponse.Files[0]
s.Equal(dto.FilePath(path), fileHeader.Name)
s.Equal(dto.EntryTypeLink, fileHeader.EntryType)
s.Equal("/workspace", fileHeader.LinkTarget)
})
s.Run("denied", func() {
path := "/proc/1/cwd"
getFileURL.RawQuery = url.Values{api.PathKey: []string{path}}.Encode()

response, err := http.Get(getFileURL.String())
s.Require().NoError(err)
s.Equal(http.StatusOK, response.StatusCode)
listFilesResponse := new(dto.ListFileSystemResponse)
err = json.NewDecoder(response.Body).Decode(listFilesResponse)
s.Require().NoError(err)
s.Require().Equal(1, len(listFilesResponse.Files))
fileHeader := listFilesResponse.Files[0]
s.Equal(dto.FilePath(path), fileHeader.Name)
s.Equal(dto.EntryTypeLink, fileHeader.EntryType)
s.Empty(fileHeader.LinkTarget)
})
})
}

func (s *E2ETestSuite) TestCopyFilesRoute() {
Expand Down

0 comments on commit 5758a0e

Please sign in to comment.