Skip to content

Commit

Permalink
Always drop malformed packets before dedup hockeypuck#198
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgdotcom committed Nov 11, 2023
1 parent ea9d15a commit 17b8b36
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
24 changes: 13 additions & 11 deletions src/hockeypuck/hkp/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,15 +356,7 @@ func (h *Handler) get(w http.ResponseWriter, l *Lookup) {

// Drop malformed packets, since these break GPG imports.
for _, key := range keys {
var others []*openpgp.Packet
for _, other := range key.Others {
if other.Malformed {
log.Debugf("get %q: ignoring malformed packet", l.Search)
continue
}
others = append(others, other)
}
key.Others = others
openpgp.DropMalformed(key)
}

w.Header().Set("Content-Type", "application/pgp-keys")
Expand Down Expand Up @@ -491,7 +483,12 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request, _ httprouter.Param
return
}
for _, key := range keys {
err := openpgp.DropDuplicates(key)
err := openpgp.DropMalformed(key)
if err != nil {
httpError(w, http.StatusInternalServerError, errors.WithStack(err))
return
}
err = openpgp.DropDuplicates(key)
if err != nil {
httpError(w, http.StatusInternalServerError, errors.WithStack(err))
return
Expand Down Expand Up @@ -564,7 +561,12 @@ func (h *Handler) Replace(w http.ResponseWriter, r *http.Request, _ httprouter.P
if signingFp != key.Fingerprint() {
continue
}
err := openpgp.DropDuplicates(key)
err := openpgp.DropMalformed(key)
if err != nil {
httpError(w, http.StatusInternalServerError, errors.WithStack(err))
return
}
err = openpgp.DropDuplicates(key)
if err != nil {
httpError(w, http.StatusInternalServerError, errors.WithStack(err))
return
Expand Down
6 changes: 5 additions & 1 deletion src/hockeypuck/hkp/sks/recon.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,11 @@ func (r *Peer) upsertKeys(rcvr *recon.Recover, buf []byte) (*upsertResult, error
}
result := &upsertResult{}
for _, key := range keys {
err := openpgp.DropDuplicates(key)
err := openpgp.DropMalformed(key)
if err != nil {
return nil, errors.WithStack(err)
}
err = openpgp.DropDuplicates(key)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
4 changes: 4 additions & 0 deletions src/hockeypuck/openpgp/cmd/hashcmp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func testKeyring(opkr *openpgp.OpaqueKeyring) (int, int, error) {
return 0, 0, errors.WithStack(err)
}

err = openpgp.DropMalformed(pk)
if err != nil {
return 0, 0, errors.WithStack(err)
}
err = openpgp.DropDuplicates(pk)
if err != nil {
return 0, 0, errors.WithStack(err)
Expand Down
11 changes: 11 additions & 0 deletions src/hockeypuck/openpgp/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ func ValidSelfSigned(key *PrimaryKey, selfSignedOnly bool) error {
return key.updateMD5()
}

func DropMalformed(key *PrimaryKey) error {
var others []*Packet
for _, other := range key.Others {
if !other.Malformed {
others = append(others, other)
}
}
key.Others = others
return key.updateMD5()
}

func DropDuplicates(key *PrimaryKey) error {
err := dedup(key, nil)
if err != nil {
Expand Down

0 comments on commit 17b8b36

Please sign in to comment.