mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-17 07:36:44 +00:00
feat: clear keys after unmarshaling
This commit is contained in:
@ -39,21 +39,39 @@ type PMKey struct {
|
|||||||
Signature *string `json:",omitempty"`
|
Signature *string `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type clearable []byte
|
||||||
|
|
||||||
|
func (c *clearable) UnmarshalJSON(b []byte) error {
|
||||||
|
b = bytes.Trim(b, "\"")
|
||||||
|
b = bytes.ReplaceAll(b, []byte("\\n"), []byte("\n"))
|
||||||
|
b = bytes.ReplaceAll(b, []byte("\\r"), []byte("\r"))
|
||||||
|
*c = b
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *clearable) clear() {
|
||||||
|
for i := range *c {
|
||||||
|
(*c)[i] = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (key *PMKey) UnmarshalJSON(b []byte) (err error) {
|
func (key *PMKey) UnmarshalJSON(b []byte) (err error) {
|
||||||
type _PMKey PMKey
|
type _PMKey PMKey
|
||||||
|
|
||||||
rawKey := struct {
|
rawKey := struct {
|
||||||
_PMKey
|
_PMKey
|
||||||
PrivateKey string
|
PrivateKey clearable
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
|
defer rawKey.PrivateKey.clear()
|
||||||
|
|
||||||
if err = json.Unmarshal(b, &rawKey); err != nil {
|
if err = json.Unmarshal(b, &rawKey); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
*key = PMKey(rawKey._PMKey)
|
*key = PMKey(rawKey._PMKey)
|
||||||
|
|
||||||
if key.PrivateKey, err = crypto.NewKeyFromArmored(rawKey.PrivateKey); err != nil {
|
if key.PrivateKey, err = crypto.NewKeyFromArmoredReader(bytes.NewReader(rawKey.PrivateKey)); err != nil {
|
||||||
return errors.Wrap(err, "failed to create crypto key from armored private key")
|
return errors.Wrap(err, "failed to create crypto key from armored private key")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user