Ensure query params are fully decoded

LinkedIn linkes to send double-encoded query params
This commit is contained in:
Marcus Noble 2022-01-20 11:26:38 +00:00
parent a3274f8df1
commit 04ca37fe14

27
main.go
View File

@ -42,15 +42,15 @@ func main() {
app.Get("/opengraph", func(c *fiber.Ctx) error { app.Get("/opengraph", func(c *fiber.Ctx) error {
vars := map[string]string{ vars := map[string]string{
"siteTitle": c.Query("siteTitle", ""), "siteTitle": ensureDecoded(c.Query("siteTitle", "")),
"title": c.Query("title", ""), "title": ensureDecoded(c.Query("title", "")),
"tags": c.Query("tags", ""), "tags": ensureDecoded(c.Query("tags", "")),
"image": c.Query("image", ""), "image": ensureDecoded(c.Query("image", "")),
"twitter": c.Query("twitter", ""), "twitter": ensureDecoded(c.Query("twitter", "")),
"github": c.Query("github", ""), "github": ensureDecoded(c.Query("github", "")),
"website": c.Query("website", ""), "website": ensureDecoded(c.Query("website", "")),
"bgColor": c.Query("bgColor", c.Query("bgColour", "#fff")), "bgColor": ensureDecoded(c.Query("bgColor", c.Query("bgColour", "#fff"))),
"fgColor": c.Query("fgColor", c.Query("fgColour", "#2B414D")), "fgColor": ensureDecoded(c.Query("fgColor", c.Query("fgColour", "#2B414D"))),
} }
key := generateKey(vars) key := generateKey(vars)
@ -101,3 +101,12 @@ func generateImage(vars map[string]string) ([]byte, error) {
return os.ReadFile(imageFile.Name()) return os.ReadFile(imageFile.Name())
} }
// Some sites (LinkedIn) encode the already encoded URL so we need to double-decode to be sure
func ensureDecoded(str string) string {
decoded, err := url.QueryUnescape(str)
if err != nil {
return str
}
return decoded
}