Skip to content

proposal: encoding/pem: add DecodeStrict #34069

@tux21b

Description

@tux21b

The "encoding/pem".Decode function in the standard library was designed to find and decode PEM blocks within arbitrary text (like emails according to a comment by rsc). Therefore, it tries to find BEGIN blocks anywhere and if parsing fails, it automatically backtracks and tries to decode the next block. No errors are reported.

Nowadays, this function is mainly used to decode TLS certificates as also stated by the package comment. Within this context, it is really annoying to not see any errors at all. Lot's of server software is written in Go, and invalid characters within those files (including white spaces or a windows line-ending at a wrong location) usually causes the software to silently ignore the certificate.

I currently work at a consulting company and we regularly have reports from clients that can be traced back to silently ignored errors when decoding PEM files and until now, I didn't know about this rather unusually behavior in the standard library either.

Can we please add a "encoding/pem.DecodeStrict" function for the common case of decoding certificates? We should probably also add a warning to the Decode function as well, because I think it is often used wrongly.

What version of Go are you using (go version)?

$ go version
go version go1.13 linux/amd64

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions