middleware: add sanitization hook

This commit is contained in:
Leo Balduf
2016-11-28 20:55:04 +01:00
parent 91ce2aaf77
commit 3c098c0703
15 changed files with 248 additions and 63 deletions
+17 -2
View File
@@ -13,6 +13,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/tylerb/graceful"
"github.com/chihaya/chihaya/bittorrent"
"github.com/chihaya/chihaya/frontend"
"github.com/chihaya/chihaya/middleware"
)
@@ -22,6 +23,9 @@ func init() {
recordResponseDuration("action", nil, time.Second)
}
// ErrInvalidIP indicates an invalid IP.
var ErrInvalidIP = bittorrent.ClientError("invalid IP")
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "chihaya_http_response_duration_milliseconds",
@@ -172,8 +176,19 @@ func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprou
return
}
ip := net.ParseIP(host)
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, len(ip) == net.IPv6len)
reqIP := net.ParseIP(host)
af := bittorrent.IPv4
if reqIP.To4() != nil {
af = bittorrent.IPv4
} else if len(reqIP) == net.IPv6len { // implies reqIP.To4() == nil
af = bittorrent.IPv6
} else {
log.Errorln("http: invalid IP: neither v4 nor v6, RemoteAddr was", r.RemoteAddr)
WriteError(w, ErrInvalidIP)
return
}
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, af == bittorrent.IPv6)
resp, err := t.logic.HandleScrape(ctx, req)
if err != nil {
+2 -7
View File
@@ -74,16 +74,11 @@ func ParseAnnounce(r *http.Request, realIPHeader string, allowIPSpoofing bool) (
}
request.Peer.Port = uint16(port)
request.Peer.IP = requestedIP(r, qp, realIPHeader, allowIPSpoofing)
if request.Peer.IP == nil {
request.Peer.IP.IP = requestedIP(r, qp, realIPHeader, allowIPSpoofing)
if request.Peer.IP.IP == nil {
return nil, bittorrent.ClientError("failed to parse peer IP address")
}
// Sanitize IPv4 addresses to 4 bytes.
if ip := request.Peer.IP.To4(); ip != nil {
request.Peer.IP = ip
}
return request, nil
}
+16 -1
View File
@@ -10,6 +10,7 @@ import (
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/prometheus/client_golang/prometheus"
"github.com/chihaya/chihaya/bittorrent"
@@ -23,6 +24,9 @@ func init() {
recordResponseDuration("action", nil, time.Second)
}
// ErrInvalidIP indicates an invalid IP.
var ErrInvalidIP = bittorrent.ClientError("invalid IP")
var promResponseDurationMilliseconds = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "chihaya_udp_response_duration_milliseconds",
@@ -228,7 +232,18 @@ func (t *Frontend) handleRequest(r Request, w ResponseWriter) (actionName string
return
}
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, len(r.IP) == net.IPv6len)
af := bittorrent.IPv4
if r.IP.To4() != nil {
af = bittorrent.IPv4
} else if len(r.IP) == net.IPv6len { // implies r.IP.To4() == nil
af = bittorrent.IPv6
} else {
log.Errorln("http: invalid IP: neither v4 nor v6, IP was", r.IP)
WriteError(w, txID, ErrInvalidIP)
return
}
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, af == bittorrent.IPv6)
var resp *bittorrent.ScrapeResponse
resp, err = t.logic.HandleScrape(ctx, req)
+1 -5
View File
@@ -29,10 +29,6 @@ var (
// initialConnectionID is the magic initial connection ID specified by BEP 15.
initialConnectionID = []byte{0, 0, 0x04, 0x17, 0x27, 0x10, 0x19, 0x80}
// emptyIPs are the value of an IP field that has been left blank.
emptyIPv4 = []byte{0, 0, 0, 0}
emptyIPv6 = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// eventIDs map values described in BEP 15 to Events.
eventIDs = []bittorrent.Event{
bittorrent.None,
@@ -105,7 +101,7 @@ func ParseAnnounce(r Request, allowIPSpoofing, v6 bool) (*bittorrent.AnnounceReq
Uploaded: uploaded,
Peer: bittorrent.Peer{
ID: bittorrent.PeerIDFromBytes(peerID),
IP: ip,
IP: bittorrent.IP{IP: ip},
Port: port,
},
Params: params,
+1 -1
View File
@@ -46,7 +46,7 @@ func WriteAnnounce(w io.Writer, txID []byte, resp *bittorrent.AnnounceResponse,
}
for _, peer := range peers {
buf.Write(peer.IP)
buf.Write(peer.IP.IP)
binary.Write(buf, binary.BigEndian, peer.Port)
}