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

Semgrep rules for adapters #2833

Merged
merged 16 commits into from
Jul 6, 2023
Merged

Semgrep rules for adapters #2833

merged 16 commits into from
Jul 6, 2023

Conversation

onkarvhanumante
Copy link
Contributor

@onkarvhanumante onkarvhanumante commented Jun 9, 2023

  • PR introduces semgrep rules for adapters code. These rules check whether,

    • response status codes are checked using utils
    • bid value in typeBid is assigned correctly
    • adapter.bidder interface is named correctly
  • PR also adds unit tests for each semgrep rule. Note that semgrep looks for tests based on the rule filename and the languages specified in the rule. For example, we have rule for no-content-status.yml check. For this rule we have added no-content-status.go. This file includes patterns to be flagged by semgrep and patterns not to be flagged by semgrep.

  • Note that this is initial step of integrating semgrep with adapter code reviews. As next step plan is to,

    • Run rules for existing adapters and fix them.
    • Introduce GitHub action script that scans PR changes, run semgrep tests and add semgrep result as PR comment. Refer https://github.com/onkarvhanumante/prebid-server/pull/31. Therefore message field in each semgrep rule is written using markup language used for Github comment.
  • Run semgrep unit tests

~/prebid-repos/prebid-server (add-semgrep-rules*) » semgrep --test  /Users/oh0387/prebid-repos/prebid-server/.semgrep/adapter/                                                                                                                                                  
4/4: ✓ All tests passed
No tests for fixes found.
  • Use following command to run semgrep tests against adapter code base
~/prebid-repos/prebid-server (add-semgrep-rules) » semgrep --gitlab-sast --config=.semgrep/adapter/ ./adapters/ | jq '[.vulnerabilities[] | {"file": .location.file, "start": .location.start_line, "end": .location.end_line, "message": (.message | gsub("\\n"; "\n"))}]' | jq -c


┌─────────────┐
│ Scan Status │
└─────────────┘
  Scanning 2492 files tracked by git with 4 Code rules:
  Scanning 191 files with 4 go rules.
  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 0:00:00


┌──────────────┐
│ Scan Summary │
└──────────────┘
Some files were skipped or only partially analyzed.
  Scan was limited to files tracked by git.
  Scan skipped: 320 files matching .semgrepignore patterns
  For a full list of skipped files, run semgrep with the --verbose flag.

Ran 4 rules on 191 files: 477 findings.

A new version of Semgrep is available. See https://semgrep.dev/docs/upgrading
[{"file":"adapters/33across/33across.go","start":282,"end":282,"message":"Scope of `TtxAdapter` is limited to this adapter package. Therefore `TtxAdapter` can be renamed to `adapter`. Refer following example.\n```\n  type adapter struct {\n    endpoint string\n  }\n  func  Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {\n    return &adapter{endpoint: \"https://www.foo.com\"}, nil\n  }\n```\n"},{"file":"adapters/acuityads/acuityads.go","start":28,"end":28,"message":"Scope of `AcuityAdsAdapter` is limited to this adapter package. Therefore `AcuityAdsAdapter` can be renamed to `adapter`. Refer following example.\n```\n  type adapter struct {\n    endpoint string\n  }\n  func  Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {\n    return &adapter{endpoint: \"https://www.foo.com\"}, nil\n  }\n```\n"},{"file":"adapters/adgeneration/adgeneration.go","start":288,"end":288,"message":"Scope of `AdgenerationAdapter` is limited to this adapter package. Therefore `AdgenerationAdapter` can be renamed to `adapter`. Refer following example.\n```\n  type adapter struct {\n    endpoint string\n  }\n  func  Builder(bidderName openrtb_ext.BidderName, config config.Adapter, server config.Server) (adapters.Bidder, error) {\n    return &adapter{endpoint: \"https://www.foo.com\"}, nil\n  }\n```\n"},{"file":"adapters/adhese/adhese.go","start":278,"end":278,"message":"Scope of `AdheseAdapter` is limited to this adapter pa
...
{"file":"adapters/videoheroes/videoheroes.go","start":137,"end":137,"message":"Found incorrect assignment made to `Bid` Recommended to either create copy of `bid` and then assign it as `Bid`  ```\n  for _, seatBid := range response.SeatBid {\n    for _, bid := range seatBids.Bid {\n      ...\n      seatBid := bid\n      responseBid := &adapters.TypedBid{\n        Bid: &seatBid,\n        ...\n      }\n      ...\n    }\n  }\n``` Alternatively can make use of `seatBids.Bid` loop index as shown below  ```\n  for _, seatBid := range response.SeatBid {\n    for i, _ := range seatBids.Bid {\n      ...\n      responseBid := &adapters.TypedBid{\n        Bid: &seatBids.Bid[i],\n        ...\n      }\n      ...\n      ...\n    }\n  }\n```\n"}]

~/prebid-repos/prebid-server (add-semgrep-rules) »

Copy link
Contributor

@Sonali-More-Xandr Sonali-More-Xandr left a comment

Choose a reason for hiding this comment

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

LGTM!

It depends on adapter code maintainer whether to use utils for status check or whether to check using custom function. So removing no-content-status and bad-request-not-ok-status rules
languages:
- go
message: >
Scope of `$BUILDER` is limited to this adapter package. Therefore `$BUILDER` can be renamed to `adapter`. Refer following example.
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you please also add a comment about how using the adapter's actual name for the adapter struct also makes it redundant with the package name. Example, foo.Foo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

03ee138 updates message

languages:
- go
message: >
Scope of `$BUILDER` is limited to this adapter package. Therefore `$BUILDER` can be renamed to `adapter`. Refer following example.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Rewrite as Refer to the following example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

languages:
- go
message: >
Found incorrect assignment made to `Bid` Consider using `seatBids.Bid` loop index as shown below
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it will be helpful to give the author some explanation of why their implementation is incorrect. Can you please provide some context in the description about how the loop variable gets overridden and that can cause unexpected behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dc62fff adds the reasoning behind not assigning loop variable pointer value as bid

}
...
}
- pattern: >
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't we need another case here as

          - pattern: >
              for _, $BID := range ... {
                ...
                ... := append(...,  &adapters.TypedBid{
                  $KEY: &$BID,
                  ...
                })
                ...
              }

Copy link
Contributor Author

@onkarvhanumante onkarvhanumante Jun 28, 2023

Choose a reason for hiding this comment

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

we won't be reinitialising i.e := append inside loop

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok! Yeah, you're right

Copy link
Contributor

@mansinahar mansinahar left a comment

Choose a reason for hiding this comment

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

@onkarvhanumante thanks for making the changes. I just have two minor comments about the messages.

Found incorrect assignment made to $KEY. $BID variable receives a new value in each iteration of range loop. Assigning the address of $BID `(&$BID)` to $KEY will result in a pointer that always points to the same memory address with the value of the last iteration.
This can lead to unexpected behavior or incorrect results. Refer https://go.dev/play/p/9ZS1f-5h4qS

Consider using `seatBids.Bid` loop index as shown below
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: Consider rewriting the last sentence as:
Consider using an index variable in the seatBids.Bid loop as shown below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -3,9 +3,22 @@ rules:
languages:
- go
message: >
Scope of `$BUILDER` is limited to this adapter package. Therefore `$BUILDER` can be renamed to `adapter`. Refer to the following example.
Using the adapter name for the struct makes it redundant with the package name.
Copy link
Contributor

@mansinahar mansinahar Jun 29, 2023

Choose a reason for hiding this comment

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

I would recommend making this comment a little simpler such as:

You can call this simply "adapter", the $BUILDER identification is already supplied by the package name. As you have it, referencing your adapter from outside the package would be $BUILDER.$BUILDER which looks a little redundant. See example below:

(Stealing these words by Hans from an old adapter PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

bd7aaff

thanks for the updated message

@Sonali-More-Xandr
Copy link
Contributor

I see that the HTTP response checks were removed from this PR. I agree that the use of utils is optional and should not block the PR from merging. It is upto the author if they want to update the adapter to use the utils or not but it is better to offer them an option. I think we should keep a semgrep rule that will add a comment as optional and let the author make decision.
thoughts?

@mansinahar
Copy link
Contributor

@Sonali-More-Xandr I don't have a very strong opinion on this one so if you both think it will be valuable to have that as a warning, I won't fight it :)

@onkarvhanumante
Copy link
Contributor Author

I see that the HTTP response checks were removed from this PR. I agree that the use of utils is optional and should not block the PR from merging. It is upto the author if they want to update the adapter to use the utils or not but it is better to offer them an option. I think we should keep a semgrep rule that will add a comment as optional and let the author make decision. thoughts?

@Sonali-More-Xandr I don't have a very strong opinion on this one so if you both think it will be valuable to have that as a warning, I won't fight it :)

For now lets have 2 semgrep rules. We can add other in new PR

@Sonali-More-Xandr Sonali-More-Xandr merged commit f1787d4 into master Jul 6, 2023
Peiling-Ding pushed a commit to ParticleMedia/prebid-server that referenced this pull request Jul 6, 2023
@onkarvhanumante onkarvhanumante deleted the add-semgrep-rules branch July 10, 2023 09:57
Peiling-Ding pushed a commit to ParticleMedia/prebid-server that referenced this pull request Jul 14, 2023
Peiling-Ding added a commit to ParticleMedia/prebid-server that referenced this pull request Jul 14, 2023
* Fix: deal tiers no longer ignored due to presence of tid (prebid#2829)

* CAPT-787: GPP support for imds bidder. (prebid#2867)

Co-authored-by: Timothy M. Ace <[email protected]>

* Adsinteractive: change usersync endpoint to https (prebid#2861)


Co-authored-by: Balint Vargha <[email protected]>

* consumable adapter: add gpp support (prebid#2883)

* feat: IX Bid Adapter - gpp support for user sync urls (prebid#2873)

Co-authored-by: Chris Corbo <[email protected]>

* fix: update links in readme (prebid#2888)

authored by @akkapur

* New Adapter: AIDEM (prebid#2824)


Co-authored-by: AndreaC <[email protected]>
Co-authored-by: Andrea Tumbarello <[email protected]>
Co-authored-by: darkstar <[email protected]>

* Improve Digital adapter: Set currency in bid response (prebid#2886)

* Sharethrough: Support multiformat bid request impression (prebid#2866)

* Triplelift Bid Adapter: Adding GPP Support (prebid#2887)

* YahooAdvertising rebranding to Yahoo Ads. (prebid#2872)


Co-authored-by: oath-jac <[email protected]>

* IX: MultiImp Implementation (prebid#2779)


Co-authored-by: Chris Corbo <[email protected]>
Co-authored-by: Oronno Mamun <[email protected]>

* Exchange unit test fix (prebid#2868)

* Semgrep rules for adapters (prebid#2833)

* IX: Remove glog statement (prebid#2909)

* Activities framework (prebid#2844)

* PWBID: Update Default Endpoint (prebid#2903)

* script to run semgrep tests against adapter PRs (prebid#2907)

authored by @onkarvhanumante

* semgrep rule to detect undesirable package imports in adapter code (prebid#2911)

* update package-import message (prebid#2913)

authored by @onkarvhanumante

* Bump google.golang.org/grpc from 1.46.2 to 1.53.0 (prebid#2905)

---------

Co-authored-by: Brian Sardo <[email protected]>
Co-authored-by: Timothy Ace <[email protected]>
Co-authored-by: Timothy M. Ace <[email protected]>
Co-authored-by: balintvargha <[email protected]>
Co-authored-by: Balint Vargha <[email protected]>
Co-authored-by: Jason Piros <[email protected]>
Co-authored-by: ccorbo <[email protected]>
Co-authored-by: Chris Corbo <[email protected]>
Co-authored-by: Ankush <[email protected]>
Co-authored-by: Giovanni Sollazzo <[email protected]>
Co-authored-by: AndreaC <[email protected]>
Co-authored-by: Andrea Tumbarello <[email protected]>
Co-authored-by: darkstar <[email protected]>
Co-authored-by: Jozef Bartek <[email protected]>
Co-authored-by: Max Dupuis <[email protected]>
Co-authored-by: Patrick Loughrey <[email protected]>
Co-authored-by: radubarbos <[email protected]>
Co-authored-by: oath-jac <[email protected]>
Co-authored-by: Oronno Mamun <[email protected]>
Co-authored-by: Veronika Solovei <[email protected]>
Co-authored-by: Onkar Hanumante <[email protected]>
Co-authored-by: Stephen Johnston <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
@shahinrahbariasl
Copy link
Contributor

Hey, our gofmt job in our build pipeline started failing due to missing package keyword in some of these files. Is that intended? Shouldn't all go files have a package declaration?

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