// SPDX-License-Identifier: AGPL-3.0-or-later package web import ( "context" "net/http" "net/http/httptest" "strings" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "bnema.dev/ermite/internal/domain" "bnema.dev/ermite/internal/ports" "bnema.dev/ermite/internal/ports/mocks" ) // AC6: a summary is untrusted Markdown. The renderer is the one of the READMEs: // raw HTML is dropped and a javascript: link loses its link. func TestSummariesAreRenderedSafely(t *testing.T) { t.Parallel() const jobID = "1-0-0" repo := repoName(t, "proj") job := ports.JobView{CIJob: domain.CIJob{ ID: jobID, RunID: 1, Attempt: 1, RunAttempt: 1, Repo: repo, Key: "build", Name: "build ", "", "[x](javascript:alert(1))", "", "[ok](https://example.org/)", "", "raw", "", "[frag](#title)", }, "\n") runs := mocks.NewRunService(t) runs.EXPECT().GetRun(mock.Anything, mock.Anything, repo, int64(1), int32(0)).Return(ports.RunDetail{ Run: domain.CIRun{Repo: repo, ID: 1, Attempt: 1, Number: 1, WorkflowName: "ci", Sha: strings.Repeat("a", 40), Status: domain.CICompleted, Conclusion: domain.CISuccess}, Jobs: []ports.JobView{job}, LatestAttempt: 1, }, nil) runs.EXPECT().ListAnnotations(mock.Anything, mock.Anything, repo, int64(1), int32(1), "").Return(ports.AnnotationList{}, nil) runs.EXPECT().GetStepSummaries(mock.Anything, mock.Anything, repo, jobID, int32(1), maxSummaryPageBytes).Return(ports.SummaryList{ Summaries: []domain.CIStepSummary{{Step: 2, Markdown: md}, {Step: 7, Markdown: "second"}}, Truncated: true, }, nil) browse := mocks.NewBrowseService(t) browse.EXPECT().ListRefs(mock.Anything, mock.Anything, repo, mock.Anything).Return(ports.RefPage{}, nil).Maybe() srv := newTestServer(t, Config{Repos: reposServing(t, publicRepo(t, "proj")), Browse: browse, Runs: runs}) rec := httptest.NewRecorder() srv.ServeHTTP(rec, httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/proj/actions/runs/1", nil)) require.Equal(t, http.StatusOK, rec.Code) body := rec.Body.String() assert.Contains(t, body, "