Skip to content
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
11 changes: 2 additions & 9 deletions pkg/server/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ L:
expectedStatus: http.StatusNotFound, // Not found upstream, but reached it
shouldReachUpstream: true,
},
{
name: "Valid 52-char narinfo hash",
method: http.MethodGet,
path: "/1lid9xrpirkzcpqsxfq02qwiq0yd70chfl860wzsqd1739ih0nri.narinfo",
expectedStatus: http.StatusNotFound,
shouldReachUpstream: true,
},
{
name: "Invalid hash length (31 chars)",
method: http.MethodGet,
Expand All @@ -121,8 +114,8 @@ L:
{
name: "Path traversal attempt (alphanumeric but malicious)",
method: http.MethodGet,
path: "/abcdefghijklmnopqrstuvwxyz0123456789.narinfo", // 44 chars
expectedStatus: http.StatusBadRequest, // Rejected by helper.IsValidHash
path: "/aeou456789abcdfghijklmnpqrsvwxy.narinfo", // contains all 4 chars not allowed aeou
expectedStatus: http.StatusNotFound,
shouldReachUpstream: false,
},
{
Expand Down
8 changes: 2 additions & 6 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ import (
"github.com/kalbasit/ncps/pkg/cache/upstream"
"github.com/kalbasit/ncps/pkg/helper"
"github.com/kalbasit/ncps/pkg/nar"
"github.com/kalbasit/ncps/pkg/narinfo"
"github.com/kalbasit/ncps/pkg/storage"
)

const (
routeIndex = "/"
routeNar = "/nar/{hash:[a-z0-9]{32,52}}.nar"
routeNarCompression = "/nar/{hash:[a-z0-9]{32,52}}.nar.{compression:*}"
routeNarInfo = "/{hash:[a-z0-9]{32,52}}.narinfo"
routeNarInfo = "/{hash:" + narinfo.HashPattern + "}.narinfo"
routeCacheInfo = "/nix-cache-info"
routeCachePublicKey = "/pubkey"

Expand Down Expand Up @@ -291,11 +292,6 @@ func (s *Server) getNixCachePublicKey(w http.ResponseWriter, r *http.Request) {
func (s *Server) getNarInfo(withBody bool) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
hash := chi.URLParam(r, "hash")
if !helper.IsValidHash(hash) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)

return
}

ctx, span := tracer.Start(
r.Context(),
Expand Down
Loading