A social RSS reader built on the AT Protocol. glean.at
glean atproto atmosphere rss feed social app
14
fork

Configure Feed

Select the types of activity you want to include in your feed.

Validate favicon content types before returning them

+32 -4
+32 -4
internal/feed/discover.go
··· 10 10 "time" 11 11 ) 12 12 13 + type imageContentTypePrefixes []string 14 + 15 + func (p imageContentTypePrefixes) matches(contentType string) bool { 16 + for _, prefix := range p { 17 + if strings.HasPrefix(contentType, prefix) { 18 + return true 19 + } 20 + } 21 + return false 22 + } 23 + 24 + var imageContentTypes = imageContentTypePrefixes{"image/"} 25 + 13 26 type DiscoveryResult struct { 14 27 FeedURLs []string 15 28 Favicon string ··· 92 105 continue 93 106 } 94 107 if u, err := base.Parse(href); err == nil { 95 - return cleanFavicon(u.String()) 108 + if checkContentType(ctx, u.String()) { 109 + return cleanFavicon(u.String()) 110 + } 96 111 } 97 112 } 98 113 ··· 104 119 for _, path := range faviconPaths { 105 120 u, _ := url.Parse(path) 106 121 resolved := origin.ResolveReference(u) 107 - req, err := http.NewRequestWithContext(ctx, http.MethodHead, resolved.String(), nil) 122 + req, err := http.NewRequestWithContext(ctx, http.MethodGet, resolved.String(), nil) 108 123 if err != nil { 109 124 continue 110 125 } ··· 113 128 continue 114 129 } 115 130 resp.Body.Close() 116 - if resp.StatusCode == http.StatusOK { 131 + if resp.StatusCode == http.StatusOK && imageContentTypes.matches(resp.Header.Get("Content-Type")) { 117 132 return cleanFavicon(resolved.String()) 118 133 } 119 134 } 120 135 return "" 136 + } 137 + 138 + func checkContentType(ctx context.Context, url string) bool { 139 + req, err := http.NewRequestWithContext(ctx, http.MethodHead, url, nil) 140 + if err != nil { 141 + return false 142 + } 143 + resp, err := discoverClient.Do(req) 144 + if err != nil { 145 + return false 146 + } 147 + resp.Body.Close() 148 + return resp.StatusCode == http.StatusOK && imageContentTypes.matches(resp.Header.Get("Content-Type")) 121 149 } 122 150 123 151 func extractHref(link string) string { ··· 152 180 153 181 base := resp.Request.URL 154 182 return base, string(body) 155 - } 183 + }