From 580a1c2fb07ad43485cf541799f62039d4579310 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 3 Oct 2019 20:05:25 -0400 Subject: [PATCH] Add maximum body size to html link parsing --- default.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/default.go b/default.go index 26cb016..47481c7 100644 --- a/default.go +++ b/default.go @@ -15,6 +15,8 @@ import ( const ( contentTypeHtml = "text/html" + + maxBodySizeBytes = 20971520 ) func defaultLinkHandler(link string) (*LinkInfo, error) { @@ -70,7 +72,11 @@ func defaultLinkHandler(link string) (*LinkInfo, error) { switch contentType { case contentTypeHtml: - err = retrieveHtmlLinkTitle(ret, link) + if contentLength > 0 && contentLength < maxBodySizeBytes { + err = retrieveHtmlLinkTitle(ret, link) + break + } + fallthrough default: ret.Title = fmt.Sprintf("%s (%s, %s)", path.Base(u.Path), contentType, ByteCountDecimal(contentLength)) } @@ -123,7 +129,7 @@ func retrieveHtmlLinkTitle(i *LinkInfo, link string) error { defer res.Body.Close() - q, err := goquery.NewDocumentFromReader(res.Body) + q, err := goquery.NewDocumentFromReader(io.LimitReader(res.Body, maxBodySizeBytes)) if err != nil { return err