Skip to content
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

Brave News: Do not use APIRequestHelper for binary data #12857

Merged
merged 1 commit into from
Apr 5, 2022

Conversation

petemill
Copy link
Member

APIRequestHelper has recently changed to validate that the response is valid JSON.

Resolves brave/brave-browser#22024

Submitter Checklist:

  • I confirm that no security/privacy review is needed, or that I have requested one
  • There is a ticket for my issue
  • Used Github auto-closing keywords in the PR description above
  • Wrote a good PR/commit description
  • Squashed any review feedback or "fixup" commits before merge, so that history is a record of what happened in the repo, not your PR
  • Added appropriate labels (QA/Yes or QA/No; release-notes/include or release-notes/exclude; OS/...) to the associated issue
  • Checked the PR locally: npm run test -- brave_browser_tests, npm run test -- brave_unit_tests, npm run lint, npm run gn_check, npm run tslint
  • Ran git rebase master (if needed)

Reviewer Checklist:

  • A security review is not needed, or a link to one is included in the PR description
  • New files have MPL-2.0 license header
  • Adequate test coverage exists to prevent regressions
  • Major classes, functions and non-trivial code blocks are well-commented
  • Changes in component dependencies are properly reflected in gn
  • Code follows the style guide
  • Test plan is specified in PR before merging

After-merge Checklist:

Test Plan:

Copy link
Member

@fmarier fmarier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@petemill This doesn't look like it removes any of the (somewhat) identifying request headers (e.g. Accept-Language, User-Agent) when sending a request to the private CDN. Is that header removal done somewhere else? Or did we end up not implementing it? I can't remember.

@petemill
Copy link
Member Author

petemill commented Apr 2, 2022

@petemill Did doesn't look like it removes any of the (somewhat) identifying request headers (e.g. Accept-Language, User-Agent) when sending a request to the private CDN. Is that header removal done somewhere else? Or did we end up not implementing it? I can't remember.

@fmarier The header removal is done here https://github.com/brave/brave-core/pull/12857/files#diff-f5dc813d6939b2fdab7d89947a9657e8d6583d004b2d74c4839017647ca637f4R50

@petemill
Copy link
Member Author

petemill commented Apr 2, 2022

I pushed a refactor earlier today and didn't have a chance to build which is why there's CI errors, which I'll address now

net::NetworkTrafficAnnotationTag annotation_tag,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: annotation_tag_(annotation_tag),
url_loader_factory_(url_loader_factory) {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move(url_loader_factory)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, and noting that this is it's being done in the code I copied it from 😄 https://github.com/brave/brave-core/blob/master/components/api_request_helper/api_request_helper.cc#L45

request->credentials_mode = network::mojom::CredentialsMode::kOmit;
request->method = net::HttpRequestHeaders::kGetMethod;
// Don't send any identifying information, such as language or user agent
for (auto entry : brave::private_cdn_headers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auto& entry

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}
url_loaders_.erase(iter);
std::move(callback).Run(response_code, *response_body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response_body can be nullptr if a network error has occurred [1]. It's better just check response_body before dereferencing in case some other corner case will appear in SimpleURLLoader.

  1. See SetAllowPartialResults and this code:
    https://github.com/chromium/chromium/blob/4c7435484856fc171e16fa84d9c244fbadd6fa4a/services/network/public/cpp/simple_url_loader.cc#L1541-L1543

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

base::StringPiece body_payload(body.data(), body.size());
if (response_code < 200 || response_code >= 300 ||
(is_padded &&
!brave::PrivateCdnHelper::GetInstance()->RemovePadding(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RemovePadding can be static.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to change in a follow-up. This is not introduced in this PR, and would prefer to limit this PR to fix the regression and not refactor.

&body_payload))) {
// Byte padding removal failed
absl::optional<std::vector<uint8_t>> args;
std::move(callback).Run(std::move(args));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absl::nullopt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

bool auto_retry_on_network_change /* = true */,
size_t max_body_size /* = 5 * 1024 * 1024 */) {
// Do not allow unbounded max body
DCHECK(max_body_size != = -1u);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!= =? 😏

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for spotting - fixed

request->credentials_mode = network::mojom::CredentialsMode::kOmit;
request->method = net::HttpRequestHeaders::kGetMethod;
// Don't send any identifying information, such as language or user agent
for (auto entry : brave::private_cdn_headers)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brave::private_cdn_headers should be a base::NoDestructor instance accessible (and constructible) via some getter like brave::GetPrivateCDNHeaders().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer to change in a follow-up. This is not introduced in this PR, and would prefer to limit this PR to fix the regression and not refactor

Create PrivateCDNRequestHelper to help ensure requests are anonymous.
APIRequestHelper was no longer appropriate as it requires response to be JSON format.
@petemill petemill merged commit ddc239e into master Apr 5, 2022
@petemill petemill deleted the fix-news-image-request branch April 5, 2022 16:54
@petemill petemill added this to the 1.39.x - Nightly milestone Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Images missing/won't render in Brave News
3 participants