Watch
1
0
Fork
You've already forked pkg-proxy
1
mirror of https://github.com/git-pkgs/proxy.git synced 2026-11-09 07:39:19 -05:00
pkg-proxy/internal/handler/pypi_denylist.go

59 lines
1.3 KiB
Go
Raw Permalink Normal View History

package handler
import (
"bytes"
"net/url"
"path"
"golang.org/x/net/html"
)
// filterSimpleHTMLLinks reads hrefs rather than display text. Tokenization
// handles single quotes, entity escaping, and nested link text without
// reserializing the rest of the index.
func (h *PyPIHandler) filterSimpleHTMLLinks(body []byte, versions map[string]bool) []byte {
if len(versions) == 0 {
return body
}
var result bytes.Buffer
z := html.NewTokenizer(bytes.NewReader(body))
skip := false
for {
tokenType := z.Next()
if tokenType == html.ErrorToken {
return result.Bytes()
}
raw := append([]byte(nil), z.Raw()...)
if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken {
token := z.Token()
if token.Data == "a" {
skip = h.deniedSimpleLink(token, versions)
}
} else if tokenType == html.EndTagToken && z.Token().Data == "a" {
if skip {
skip = false
continue
}
}
if !skip {
result.Write(raw)
}
}
}
func (h *PyPIHandler) deniedSimpleLink(token html.Token, versions map[string]bool) bool {
for _, attr := range token.Attr {
if attr.Key != "href" {
continue
}
u, err := url.Parse(attr.Val)
if err != nil {
continue
}
_, version := h.parseFilename(path.Base(u.Path))
if versions[version] {
return true
}
}
return false
}