mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-11-26 03:54:52 +00:00
Validate the revision hash on /rev{,-text}/ pages
This commit is contained in:
committed by
Timur Ismagilov
parent
9b4b225525
commit
4e6adec81a
@@ -3,7 +3,6 @@ package histweb
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bouncepaw/mycorrhiza/cfg"
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
||||||
"github.com/bouncepaw/mycorrhiza/files"
|
"github.com/bouncepaw/mycorrhiza/files"
|
||||||
@@ -39,15 +38,7 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
|
|||||||
util.PrepareRq(rq)
|
util.PrepareRq(rq)
|
||||||
shorterURL := strings.TrimPrefix(rq.URL.Path, "/primitive-diff/")
|
shorterURL := strings.TrimPrefix(rq.URL.Path, "/primitive-diff/")
|
||||||
revHash, slug, found := strings.Cut(shorterURL, "/")
|
revHash, slug, found := strings.Cut(shorterURL, "/")
|
||||||
if !found || len(revHash) < 7 || len(slug) < 1 {
|
if !found || !util.IsRevHash(revHash) || 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)
|
http.Error(w, "403 bad request", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
15
util/util.go
15
util/util.go
@@ -135,3 +135,18 @@ func (f FormData) Get(key string) string {
|
|||||||
func (f FormData) Put(key, value string) {
|
func (f FormData) Put(key, value string) {
|
||||||
f.fields[key] = value
|
f.fields[key] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsRevHash checks if the revision hash is valid.
|
||||||
|
func IsRevHash(revHash string) bool {
|
||||||
|
if len(revHash) < 7 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
paddedRevHash := revHash
|
||||||
|
if len(paddedRevHash)%2 != 0 {
|
||||||
|
paddedRevHash = paddedRevHash[:len(paddedRevHash)-1]
|
||||||
|
}
|
||||||
|
if _, err := hex.DecodeString(paddedRevHash); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,11 +59,14 @@ func handlerMedia(w http.ResponseWriter, rq *http.Request) {
|
|||||||
// /rev-text/<revHash>/<hyphaName>
|
// /rev-text/<revHash>/<hyphaName>
|
||||||
func handlerRevisionText(w http.ResponseWriter, rq *http.Request) {
|
func handlerRevisionText(w http.ResponseWriter, rq *http.Request) {
|
||||||
util.PrepareRq(rq)
|
util.PrepareRq(rq)
|
||||||
|
shorterURL := strings.TrimPrefix(rq.URL.Path, "/rev-text/")
|
||||||
|
revHash, slug, found := strings.Cut(shorterURL, "/")
|
||||||
|
if !found || !util.IsRevHash(revHash) || len(slug) < 1 {
|
||||||
|
http.Error(w, "403 bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
shorterURL = strings.TrimPrefix(rq.URL.Path, "/rev-text/")
|
hyphaName = util.CanonicalName(slug)
|
||||||
firstSlashIndex = strings.IndexRune(shorterURL, '/')
|
|
||||||
revHash = shorterURL[:firstSlashIndex]
|
|
||||||
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
|
|
||||||
h = hyphae.ByName(hyphaName)
|
h = hyphae.ByName(hyphaName)
|
||||||
)
|
)
|
||||||
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
@@ -103,17 +106,17 @@ func handlerRevisionText(w http.ResponseWriter, rq *http.Request) {
|
|||||||
// handlerRevision displays a specific revision of the text part the hypha
|
// handlerRevision displays a specific revision of the text part the hypha
|
||||||
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
|
func handlerRevision(w http.ResponseWriter, rq *http.Request) {
|
||||||
util.PrepareRq(rq)
|
util.PrepareRq(rq)
|
||||||
|
lc := l18n.FromRequest(rq)
|
||||||
|
shorterURL := strings.TrimPrefix(rq.URL.Path, "/rev/")
|
||||||
|
revHash, slug, found := strings.Cut(shorterURL, "/")
|
||||||
|
if !found || !util.IsRevHash(revHash) || len(slug) < 1 {
|
||||||
|
http.Error(w, "403 bad request", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
var (
|
var (
|
||||||
lc = l18n.FromRequest(rq)
|
hyphaName = util.CanonicalName(slug)
|
||||||
shorterURL = strings.TrimPrefix(rq.URL.Path, "/rev/")
|
|
||||||
firstSlashIndex = strings.IndexRune(shorterURL, '/')
|
|
||||||
revHash = shorterURL[:firstSlashIndex]
|
|
||||||
hyphaName = util.CanonicalName(shorterURL[firstSlashIndex+1:])
|
|
||||||
h = hyphae.ByName(hyphaName)
|
h = hyphae.ByName(hyphaName)
|
||||||
contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text"))
|
contents = fmt.Sprintf(`<p>%s</p>`, lc.Get("ui.revision_no_text"))
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
textContents string
|
textContents string
|
||||||
err error
|
err error
|
||||||
mycoFilePath string
|
mycoFilePath string
|
||||||
|
|||||||
Reference in New Issue
Block a user