-
Notifications
You must be signed in to change notification settings - Fork 95
fix(media): constrain attachment redirects #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,9 +50,7 @@ func Fetch(ctx context.Context, s *store.Store, opts FetchOptions) (FetchStats, | |
| if opts.MaxBytes <= 0 { | ||
| opts.MaxBytes = DefaultMaxBytes | ||
| } | ||
| if opts.HTTPClient == nil { | ||
| opts.HTTPClient = &http.Client{Timeout: 30 * time.Second} | ||
| } | ||
| opts.HTTPClient = attachmentHTTPClient(opts.HTTPClient) | ||
| if opts.Now == nil { | ||
| opts.Now = time.Now | ||
| } | ||
|
|
@@ -133,6 +131,29 @@ func Fetch(ctx context.Context, s *store.Store, opts FetchOptions) (FetchStats, | |
| return stats, nil | ||
| } | ||
|
|
||
| func attachmentHTTPClient(client *http.Client) *http.Client { | ||
| if client == nil { | ||
| client = &http.Client{Timeout: 30 * time.Second} | ||
| } | ||
| clone := *client | ||
| previous := clone.CheckRedirect | ||
| clone.CheckRedirect = func(req *http.Request, via []*http.Request) error { | ||
| if len(via) >= 3 || !isAllowedAttachmentURL(req.URL.String()) { | ||
| return errors.New("attachment redirect denied") | ||
| } | ||
| if previous != nil { | ||
| if err := previous(req, via); err != nil { | ||
| return err | ||
| } | ||
| if !isAllowedAttachmentURL(req.URL.String()) { | ||
| return errors.New("attachment redirect denied") | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| return &clone | ||
| } | ||
|
|
||
| func attachmentFailureRef(attachment store.AttachmentRow) store.FailureRef { | ||
| return store.FailureRef{ | ||
| Operation: "fetch_attachment", | ||
|
|
@@ -234,6 +255,11 @@ func fetchURL(ctx context.Context, opts FetchOptions, attachment store.Attachmen | |
| } | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
| // Injected clients can supply their own redirect policy, so validate the | ||
| // final response URL at the fetch boundary as well. | ||
| if resp.Request == nil || resp.Request.URL == nil || !isAllowedAttachmentURL(resp.Request.URL.String()) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Useful? React with 👍 / 👎. |
||
| return fetchResult{}, errors.New("attachment response URL denied") | ||
| } | ||
| if resp.StatusCode < 200 || resp.StatusCode >= 300 { | ||
| return fetchResult{}, fmt.Errorf("attachment fetch returned HTTP %d", resp.StatusCode) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an attachment legitimately needs three allowed Discord CDN redirects, this guard fails it before issuing the third redirected request: net/http calls CheckRedirect with
reqas the upcoming request andviaas the requests already made, solen(via) == 3is the third redirect target (initial request plus two followed redirects), not a request after three redirect hops. This caps successful fetches at two redirects and records those otherwise-allowed attachments as failures.Useful? React with 👍 / 👎.