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

Fix panic observed in setSiteImplicitly function #3053

Merged
merged 2 commits into from
Sep 5, 2023

Conversation

ashishshinde-pubm
Copy link
Contributor

No description provided.

@hhhjort
Copy link
Collaborator

hhhjort commented Aug 24, 2023

Fixes #3052

@@ -1972,6 +1973,9 @@ func setSiteImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) {
}

if referrerCandidate != "" {
if r.Site == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

There's another way that I think this check can be made that could improve the flow of the overall function:

I think you can pull this check to the beginning of the function, which will then allow the other r.Site nil checks to be removed.

So the overall function would maybe look something like this:

func setSiteImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) {
	if r.Site == nil {
		r.Site = &openrtb2.Site{}
	}

	referrerCandidate := httpReq.Referer()
	if referrerCandidate == "" && r.Site.Page != "" {
		referrerCandidate = r.Site.Page // If http referer is disabled and thus has empty value - use site.page instead
	}

	if referrerCandidate != "" {
		setSitePageIfEmpty(r.Site, referrerCandidate)
		if parsedUrl, err := url.Parse(referrerCandidate); err == nil {
			setSiteDomainIfEmpty(r.Site, parsedUrl.Host)
			if publisherDomain, err := publicsuffix.EffectiveTLDPlusOne(parsedUrl.Host); err == nil {
				setSitePublisherDomainIfEmpty(r.Site, publisherDomain)
			}
		}
	}

	if siteExt, err := r.GetSiteExt(); err == nil && siteExt.GetAmp() == nil {
		siteExt.SetAmp(&notAmp)
	}
}

What do you think about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AlexBVolcy
Setting it at the top of function will change the existing behaviour of function, will provide the wrong message in bid-response.

Example -
Moving the condition to top breaks the TestJsonSampleRequests when request does not contain both site and app object.

Test: TestJsonSampleRequests/Asserts_we_return_400s_on_requests_that_are_not_supposed_to_pass_validation#49
Actual: Invalid request: request.site should include at least one of request.site.id or request.site.page.
Expected: Invalid request: request.site or request.app must be defined, but not both.
Filename: sample-requests/invalid-whole/no-site-or-app.json

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@AlexBVolcy what is your suggestion ? should we move it to top or keep it as it is ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it is good to move it to the top. Since this is only called when app is nil, we know we always want to have a site object by the time we are done. So might as well make sure we have one at the top, and then we don't have to check for the remainder of the function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay, moved the nil-check at the top.
Also, modified the error-message in no-site-or-app.json file

Copy link
Contributor

Choose a reason for hiding this comment

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

@ashishshinde-pubm Apologies for the late reply, but yes I agree with @hhhjort, thank you for moving it.

@SyntaxNode SyntaxNode changed the title Issue_3052: Fix panic observed in setSiteImplicitly function Fix panic observed in setSiteImplicitly function Aug 28, 2023
Copy link
Collaborator

@hhhjort hhhjort left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@AlexBVolcy AlexBVolcy left a comment

Choose a reason for hiding this comment

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

LGTM

@hhhjort hhhjort merged commit 94dc670 into prebid:master Sep 5, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants