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

[APPSEC-9936] Update AppSec response content_type resolution #2900

Merged
merged 2 commits into from
Jun 12, 2023

Conversation

GustavoCaso
Copy link
Member

@GustavoCaso GustavoCaso commented Jun 7, 2023

What does this PR do?

Based on the [internal] specs. The default content type and the response of the blocked page should be JSON.

I update the code that handles that, I also added a bunch os new specs. I fixed an issue in which spaces between the , for separating multiple HTTP accept values.

Motivation

Additional Notes

How to test the change?

CI

@github-actions github-actions bot added the appsec Application Security monitoring product label Jun 7, 2023
@GustavoCaso GustavoCaso changed the title Update AppSec response content_type resolution [APPSEC-9936] Update AppSec response content_type resolution Jun 7, 2023
@GustavoCaso GustavoCaso requested a review from lloeki June 7, 2023 11:38
@GustavoCaso GustavoCaso marked this pull request as ready for review June 7, 2023 11:38
@GustavoCaso GustavoCaso requested a review from a team June 7, 2023 11:38
@codecov-commenter
Copy link

codecov-commenter commented Jun 7, 2023

Codecov Report

Merging #2900 (fe93a72) into master (d3bdba5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #2900   +/-   ##
=======================================
  Coverage   98.09%   98.09%           
=======================================
  Files        1268     1268           
  Lines       70031    70047   +16     
  Branches     3195     3195           
=======================================
+ Hits        68697    68714   +17     
+ Misses       1334     1333    -1     
Impacted Files Coverage Δ
lib/datadog/appsec/response.rb 100.00% <100.00%> (ø)
spec/datadog/appsec/response_spec.rb 100.00% <100.00%> (ø)

... and 2 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

lloeki
lloeki previously requested changes Jun 8, 2023
Copy link
Contributor

@lloeki lloeki left a comment

Choose a reason for hiding this comment

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

Overall OK to change the default, but I believe there's a bug introduced here.


accepted.each_with_object(DEFAULT_CONTENT_TYPE) do |range, _default|
match = FORMAT_MAP.keys.find { |type| range === type }
accepted.each do |range|
Copy link
Contributor

Choose a reason for hiding this comment

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

accepted.each_with_object(DEFAULT_CONTENT_TYPE) was making it so it returns DEFAULT_CONTENT_TYPE when no match has occured.

accepted.each now instead returns accepted, which is an array of MediaRange, so:

  • the return value of content_type is now inconsistent: when a match is found it returns a content type but when none is found it returns an array of MediaRange
  • when there is no match it does not return the default value anymore, which will break FORMAT_MAP[content_type]

Copy link
Member Author

Choose a reason for hiding this comment

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

Excellent point and catch @lloeki

I decided to know use each_with_object because I believe it doesn't express as clearly the intent of returning DEFAULT_CONTENT_TYPE as having an explicit return value at the end of the loop.

I added the necessary code and specs to make sure we do not introduce any regression on this code here:
fe93a72

accepted.each_with_object(DEFAULT_CONTENT_TYPE) do |range, _default|
match = FORMAT_MAP.keys.find { |type| range === type }
accepted.each do |range|
match = FORMAT_MAP.keys.find { |key| range === key }
Copy link
Contributor

Choose a reason for hiding this comment

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

FORMAT_MAP keys are content types, so I'm questioning the name change here.

I would even change it that way to make it clearer:

type_match = FORMAT_MAP.keys.find { |type| range === type }

return type_match if type_match

@GustavoCaso GustavoCaso requested a review from lloeki June 12, 2023 09:02
@GustavoCaso GustavoCaso force-pushed the appsec-make-sure-blocked-reponse-page-is-correct branch from 83dcc33 to c4df9db Compare June 12, 2023 09:07
@GustavoCaso GustavoCaso force-pushed the appsec-make-sure-blocked-reponse-page-is-correct branch from c4df9db to fe93a72 Compare June 12, 2023 09:12
Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

LGTM 👍


return match if match
accepted.each do |range|
type_match = CONTENT_TYPE_TO_FORMAT.keys.find { |type| range === type }
Copy link
Member

Choose a reason for hiding this comment

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

I noticed MediaRange#=== with a String as input will parse that input into a MediaRange before doing anything else. Aka, we keep parsing the items in CONTENT_TYPE_TO_FORMAT all the type on this check. Maybe something to improve?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good 😄

@GustavoCaso GustavoCaso dismissed lloeki’s stale review June 12, 2023 11:06

The changes has been address

@GustavoCaso GustavoCaso merged commit c59e7fb into master Jun 12, 2023
202 checks passed
@GustavoCaso GustavoCaso deleted the appsec-make-sure-blocked-reponse-page-is-correct branch June 12, 2023 11:24
@github-actions github-actions bot added this to the 1.13.0 milestone Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
appsec Application Security monitoring product
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants