~sircmpwn/godocs.io

gddo: gddo-server: Disallow specifying pseudo-versions v1 APPLIED

Adnan Maolood: 1
 gddo-server: Disallow specifying pseudo-versions

 3 files changed, 20 insertions(+), 1 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~sircmpwn/godocs.io/patches/23391/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gddo] gddo-server: Disallow specifying pseudo-versions Export this patch

---
Don't allow specifying pseudo-versions as they can be abused to fill up
the database (and they don't show up in the list of versions anyways).

Later on, we should also automatically remove pseudo-versions from the
database but keep the latest pseudo-version for packages with no release
versions available.

 gddo-server/crawl.go  |  1 +
 gddo-server/gemini.go |  7 +++++++
 gddo-server/http.go   | 13 ++++++++++++-
 3 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/gddo-server/crawl.go b/gddo-server/crawl.go
index f59c461..4e527e8 100644
--- a/gddo-server/crawl.go
+++ b/gddo-server/crawl.go
@@ -27,6 +27,7 @@ var (
	ErrBlocked    = errors.New("blocked import path")
	ErrMismatch   = errors.New("import paths don't match")
	ErrNoPackages = errors.New("no packages found")
	ErrPseudo     = errors.New("pseudo version not allowed")
)

// byVersion sorts versions from latest to oldest.
diff --git a/gddo-server/gemini.go b/gddo-server/gemini.go
index 9c725e4..4988b98 100644
--- a/gddo-server/gemini.go
+++ b/gddo-server/gemini.go
@@ -14,6 +14,7 @@ import (
	"git.sr.ht/~sircmpwn/gddo/internal/proxy"
	"git.sr.ht/~sircmpwn/gddo/internal/source"
	"git.sr.ht/~sircmpwn/gddo/internal/stdlib"
	versionpkg "git.sr.ht/~sircmpwn/gddo/internal/version"
)

func (s *Server) GeminiHandler() (gemini.Handler, error) {
@@ -94,6 +95,10 @@ func (s *Server) serveGeminiPackage(ctx context.Context, w gemini.ResponseWriter
		importPath = importPath[:at]
	}

	if versionpkg.IsPseudo(version) {
		return ErrPseudo
	}

	mod, pkg, pdoc, err := s.GetDoc(ctx, importPath, version)
	if err != nil {
		return err
@@ -246,6 +251,8 @@ func geminiErrorHandler(fn func(ctx context.Context, w gemini.ResponseWriter, r
			w.WriteHeader(gemini.StatusNotFound, "The provided import path doesn't match the module path present in the go.mod file.")
		case errors.Is(err, ErrNoPackages):
			w.WriteHeader(gemini.StatusNotFound, "The requested module doesn't contain any packages.")
		case errors.Is(err, ErrPseudo):
			w.WriteHeader(gemini.StatusNotFound, "Pseudo-versions are not allowed.")
		default:
			w.WriteHeader(gemini.StatusTemporaryFailure, "Internal server error")
		}
diff --git a/gddo-server/http.go b/gddo-server/http.go
index 4e02413..18eaf8c 100644
--- a/gddo-server/http.go
+++ b/gddo-server/http.go
@@ -20,6 +20,7 @@ import (
	"git.sr.ht/~sircmpwn/gddo/internal/proxy"
	"git.sr.ht/~sircmpwn/gddo/internal/source"
	"git.sr.ht/~sircmpwn/gddo/internal/stdlib"
	versionpkg "git.sr.ht/~sircmpwn/gddo/internal/version"
)

const (
@@ -97,6 +98,10 @@ func (s *Server) servePackage(resp http.ResponseWriter, req *http.Request) error
		importPath = importPath[:at]
	}

	if versionpkg.IsPseudo(version) {
		return ErrPseudo
	}

	mod, pkg, pdoc, err := s.GetDoc(req.Context(), importPath, version)
	if err != nil {
		return err
@@ -365,6 +370,11 @@ func errorMessages(err error) []flashMessage {
			ID:   "error",
			Args: []string{"Error fetching module: The requested module doesn't contain any packages."},
		}}
	case errors.Is(err, ErrPseudo):
		return []flashMessage{{
			ID:   "error",
			Args: []string{"Error fetching module: Pseudo-versions are not allowed."},
		}}
	}
	return nil
}
@@ -393,7 +403,8 @@ func (s *Server) errorHandler(fn func(http.ResponseWriter, *http.Request) error)
			errors.Is(err, context.DeadlineExceeded) ||
			errors.Is(err, ErrBlocked) ||
			errors.Is(err, ErrMismatch) ||
			errors.Is(err, ErrNoPackages) {
			errors.Is(err, ErrNoPackages) ||
			errors.Is(err, ErrPseudo) {

			msgs := errorMessages(err)
			s.templates.Execute(resp, "notfound.html",
-- 
2.32.0
Thanks!

To git@git.sr.ht:~sircmpwn/gddo
   df74f2d..5909268  master -> master