mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-07-03 10:12:50 +00:00
primitive-diff: validate input
This commit is contained in:
parent
9a55a520c0
commit
b15b2da6a0
@ -1,6 +1,7 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@ -54,26 +55,34 @@ func handlerMedia(w http.ResponseWriter, rq *http.Request) {
|
|||||||
|
|
||||||
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
||||||
util.PrepareRq(rq)
|
util.PrepareRq(rq)
|
||||||
|
shorterURL := strings.TrimPrefix(rq.URL.Path, "/primitive-diff/")
|
||||||
|
revHash, slug, found := strings.Cut(shorterURL, "/")
|
||||||
|
if !found || len(revHash) < 7 || len(slug) < 1 {
|
||||||
|
http.Error(w, "403 bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
paddedRevHash := revHash
|
||||||
|
if len(paddedRevHash)%2 != 0 {
|
||||||
|
paddedRevHash = paddedRevHash[:len(paddedRevHash)-1]
|
||||||
|
}
|
||||||
|
if _, err := hex.DecodeString(paddedRevHash); err != nil {
|
||||||
|
http.Error(w, "403 bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
shorterURL = strings.TrimPrefix(rq.URL.Path, "/primitive-diff/")
|
hyphaName = util.CanonicalName(slug)
|
||||||
firstSlashIndex = strings.IndexRune(shorterURL, '/')
|
h = hyphae.ByName(hyphaName)
|
||||||
revHash = shorterURL[:firstSlashIndex]
|
user = user.FromRequest(rq)
|
||||||
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
|
locale = l18n.FromRequest(rq)
|
||||||
h = hyphae.ByName(hyphaName)
|
|
||||||
u = user.FromRequest(rq)
|
|
||||||
lc = l18n.FromRequest(rq)
|
|
||||||
)
|
)
|
||||||
switch h := h.(type) {
|
switch h := h.(type) {
|
||||||
case *hyphae.EmptyHypha:
|
case *hyphae.EmptyHypha:
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
_, _ = io.WriteString(w, "404 not found")
|
io.WriteString(w, "404 not found")
|
||||||
case hyphae.ExistingHypha:
|
case hyphae.ExistingHypha:
|
||||||
util.HTTP200Page(w,
|
util.HTTP200Page(w, views.Base(
|
||||||
views.Base(
|
locale.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
|
||||||
lc.Get("ui.diff_title", &l18n.Replacements{"name": util.BeautifulName(hyphaName), "rev": revHash}),
|
views.PrimitiveDiff(rq, h, user, revHash), locale, user))
|
||||||
views.PrimitiveDiff(rq, h, u, revHash),
|
|
||||||
lc,
|
|
||||||
u))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user