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

Serve robots.txt to prevent crawlers from accessing UI #97

Merged
merged 1 commit into from
Jul 21, 2024
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- The UI now serves a `/robots.txt` that instructs crawlers to not crawl any part an installation. (You should still use an authentication layer though.) [PR #97](https://github.com/riverqueue/riverui/pull/97).

## [0.2.0] - 2024-07-02

### Added
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN go mod download
COPY *.go internal docs/README.md LICENSE ./
COPY cmd/ cmd/
COPY internal/ internal/
COPY public/ public/
COPY ui/*.go ./ui/
COPY --from=build-ui /app/dist ./ui/dist

Expand Down
51 changes: 51 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package riverui

import (
"context"
"embed"
"errors"
"fmt"
"io/fs"
Expand Down Expand Up @@ -99,6 +100,11 @@ func NewHandler(opts *HandlerOpts) (http.Handler, error) {
apiendpoint.Mount(mux, opts.Logger, &queueResumeEndpoint{apiBundle: apiBundle})
apiendpoint.Mount(mux, opts.Logger, &stateAndCountGetEndpoint{apiBundle: apiBundle})
apiendpoint.Mount(mux, opts.Logger, &workflowGetEndpoint{apiBundle: apiBundle})

if err := mountStaticFiles(opts.Logger, mux); err != nil {
return nil, err
}

mux.HandleFunc("/api", http.NotFound)
mux.Handle("/", intercept404(fileServer, serveIndex))

Expand All @@ -111,6 +117,51 @@ func NewHandler(opts *HandlerOpts) (http.Handler, error) {
return middlewareStack.Mount(mux), nil
}

//go:embed public
var publicFS embed.FS

const publicPrefix = "public/"

// Walks the embedded filesystem in publicFS and mounts each file as a route on
// the given serve mux. Content type is determined by `http.DetectContentType`.
func mountStaticFiles(logger *slog.Logger, mux *http.ServeMux) error {
return fs.WalkDir(publicFS, ".", func(path string, dirEntry fs.DirEntry, err error) error {
if err != nil {
return err
}

if dirEntry.IsDir() {
return nil
}

servePath := strings.TrimPrefix(path, publicPrefix)

mux.HandleFunc("GET /"+servePath, func(w http.ResponseWriter, r *http.Request) {
runWithError := func() error {
data, err := publicFS.ReadFile(path)
if err != nil {
return err
}

contentType := http.DetectContentType(data)
w.Header().Add("Content-Type", contentType)

if _, err := w.Write(data); err != nil {
return err
}

return nil
}

if err := runWithError(); err != nil {
logger.ErrorContext(r.Context(), "Error writing static file", "err", err)
}
})

return nil
})
}

// Go's http.StripPrefix can sometimes result in an empty path. For example,
// when removing a prefix like "/foo" from path "/foo", the result is "". This
// does not get handled by the ServeMux correctly (it results in a redirect to
Expand Down
30 changes: 30 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,34 @@ func TestNewHandlerIntegration(t *testing.T) {
makeAPICall(t, "QueueResume", http.MethodPut, makeURL("/api/queues/%s/resume", queuePaused.Name), nil)
makeAPICall(t, "StateAndCountGet", http.MethodGet, makeURL("/api/states"), nil)
makeAPICall(t, "WorkflowGet", http.MethodGet, makeURL("/api/workflows/%s", workflowID), nil)

//
// Static files
//

makeAPICall(t, "RobotsTxt", http.MethodGet, makeURL("/robots.txt"), nil)
}

func TestMountStaticFiles(t *testing.T) {
t.Parallel()

var (
logger = riverinternaltest.Logger(t)
mux = http.NewServeMux()
)

require.NoError(t, mountStaticFiles(logger, mux))

var (
recorder = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, "/robots.txt", nil)
)

mux.ServeHTTP(recorder, req)

status := recorder.Result().StatusCode //nolint:bodyclose
require.Equal(t, http.StatusOK, status)

require.Equal(t, "text/plain; charset=utf-8", recorder.Header().Get("Content-Type"))
require.Contains(t, recorder.Body.String(), "User-Agent")
}
11 changes: 11 additions & 0 deletions internal/apiendpoint/api_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func executeAPIEndpoint[TReq any, TResp any](w http.ResponseWriter, r *http.Requ
return err
}

if rawExtractor, ok := any(resp).(RawResponder); ok {
return rawExtractor.RespondRaw(w)
}

respData, err := json.Marshal(resp)
if err != nil {
return fmt.Errorf("error marshaling response JSON: %w", err)
Expand Down Expand Up @@ -189,6 +193,13 @@ type RawExtractor interface {
ExtractRaw(r *http.Request) error
}

// RawResponder is an interface that can be implemented by response structs that
// allow them to respond directly to a ResponseWriter instead of emitting the
// normal JSON format.
type RawResponder interface {
RespondRaw(w http.ResponseWriter) error
}

// Make some broad categories of internal error back into something public
// facing because in some cases they can be a vast help for debugging.
func maybeInterpretInternalError(err error) error {
Expand Down
2 changes: 2 additions & 0 deletions public/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-Agent: *
Disallow: /
Loading