Skip to content

Commit

Permalink
Fix Content Length Writer
Browse files Browse the repository at this point in the history
that sends the HTTP Headers even if no data has to be written.
  • Loading branch information
mpass99 committed Aug 16, 2024
1 parent 06deab0 commit 7efee3d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/api/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,12 @@ func (r *RunnerController) fileContent(writer http.ResponseWriter, request *http
err = targetRunner.GetFileContent(ctx, path, writer, privilegedExecution)
})
if errors.Is(err, runner.ErrFileNotFound) {
writer.Header().Del("Content-Length")
writeClientError(request.Context(), writer, err, http.StatusFailedDependency)
return
} else if err != nil {
// Otherwise, might assume that GetFileContent already wrote something and, therefore, the HTTP headers
// cannot be modified anymore.
log.WithContext(request.Context()).WithError(err).Error("Could not retrieve the requested file.")
writeInternalServerError(request.Context(), writer, err, dto.ErrorUnknown)
return
Expand Down
3 changes: 3 additions & 0 deletions pkg/nullio/content_length.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (w *ContentLengthWriter) Write(p []byte) (count int, err error) {
}

func (w *ContentLengthWriter) handleDataForwarding(p []byte) (int, error) {
if len(p) == 0 {
return 0, nil
}
count, err := w.Target.Write(p)
if err != nil {
err = fmt.Errorf("could not write to target: %w", err)
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/runners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/gorilla/websocket"
"github.com/openHPI/poseidon/internal/api"
"github.com/openHPI/poseidon/internal/runner"
"github.com/openHPI/poseidon/pkg/dto"
"github.com/openHPI/poseidon/tests"
"github.com/openHPI/poseidon/tests/helpers"
Expand Down Expand Up @@ -429,6 +430,18 @@ func (s *E2ETestSuite) TestGetFileContent_Nomad() {
s.Require().NoError(err)
s.Equal(newFileContent, content)
})

s.Run("Permission Denied", func() {
getFileURL, err := url.Parse(helpers.BuildURL(api.BasePath, api.RunnersPath, runnerID, api.FileContentRawPath))
s.Require().NoError(err)
getFileURL.RawQuery = fmt.Sprintf("%s=%s", api.PathKey, "/proc/1/environ")
response, err := http.Get(getFileURL.String())
s.Require().NoError(err)
s.Equal(http.StatusFailedDependency, response.StatusCode)
content, err := io.ReadAll(response.Body)
s.Require().NoError(err)
s.Contains(string(content), runner.ErrFileNotFound.Error())
})
}

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

0 comments on commit 7efee3d

Please sign in to comment.