-
Notifications
You must be signed in to change notification settings - Fork 19
522 implement and enforce domain whitelist validation #533
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
Open
Joshua-Ogbonna
wants to merge
20
commits into
main
Choose a base branch
from
522-implement-and-enforce-domain-whitelist-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
872badb
feat: implement domain whitelist validation for sender API endpoints
Joshua-Ogbonna 2f85f43
chore: work
Joshua-Ogbonna e5bd4be
chore: implement balanced fallback stratgy when domain extraction fai…
Joshua-Ogbonna a80466e
feat: add kyb_rejection_comment column to kyb_profiles table
sundayonah 5d86794
feat: add amount in usd field to the payment order tables
Joshua-Ogbonna cfe540c
fix: update hash for add_amount_in_usd_to_payment_tables migration an…
chibie 49070fa
feat: rename transaction history method to fetch token transfers from…
chibie 9166230
feat: normalize token transfer field names in Blockscout API response
chibie f9f1f5e
fix: resolve release workflow regex patterns for proper commit classi…
chibie 8974cae
feat: enhance balance validation by loading provider and currency edges
chibie f8f09b8
feat: add transaction hash to payment order updates
chibie 89b497d
fix: enhance account name validation in response handling
chibie c2f1a98
fix: add address non-empty check in priority queue creation
chibie 6197215
fix: filter enabled tokens in provider profile query
chibie fff23ad
fix: streamline token query in provider profile retrieval
chibie 43539f6
fix: improve Etherscan error grouping in Glitchtip
onahprosper 6110dc1
fix: temporarily disable and re-enable trigger during amount_in_usd u…
chibie c774be6
chore: work PR comments from coderabbit
Joshua-Ogbonna 21f4f38
Merge branch 'main' into 522-implement-and-enforce-domain-whitelist-v…
Joshua-Ogbonna 5074107
Merge branch 'main' into 522-implement-and-enforce-domain-whitelist-v…
Dprof-in-tech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/paycrest/aggregator/ent" | ||
| u "github.com/paycrest/aggregator/utils" | ||
| "github.com/paycrest/aggregator/utils/logger" | ||
| ) | ||
|
|
||
| func DomainWhitelistMiddleware() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| if !strings.Contains(c.Request.URL.Path, "/sender/") { | ||
| c.Next() | ||
| return | ||
| } | ||
|
|
||
| senderCtx, ok := c.Get("sender") | ||
| if !ok || senderCtx == nil { | ||
| c.Next() | ||
| return | ||
| } | ||
|
|
||
| sender := senderCtx.(*ent.SenderProfile) | ||
|
|
||
| if !sender.IsActive { | ||
| c.Next() | ||
| return | ||
| } | ||
|
|
||
| origin := c.GetHeader("Origin") | ||
| referer := c.GetHeader("Referer") | ||
|
|
||
| requestDomain, err := u.ExtractDomainFromRequest(origin, referer) | ||
| if err != nil { | ||
| logger.WithFields(logger.Fields{ | ||
| "origin": origin, | ||
| "referer": referer, | ||
| "error": err.Error(), | ||
| }).Warnf("Failed to extract domain from request headers") | ||
|
|
||
| if len(sender.DomainWhitelist) > 0 { | ||
| u.APIResponse(c, http.StatusBadRequest, "error", | ||
| "Invalid request origin", nil) | ||
| c.Abort() | ||
| return | ||
| } | ||
|
|
||
| c.Next() | ||
Dprof-in-tech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
|
|
||
| if !u.IsDomainAllowed(requestDomain, sender.DomainWhitelist) { | ||
| logger.WithFields(logger.Fields{ | ||
| "sender_id": sender.ID.String(), | ||
| "request_domain": requestDomain, | ||
| "whitelist": sender.DomainWhitelist, | ||
| "origin": origin, | ||
| "referer": referer, | ||
| }).Warnf("Request blocked due to domain whitelist violation") | ||
|
|
||
| u.APIResponse(c, http.StatusForbidden, "error", | ||
| "Access denied: Domain not whitelisted", map[string]interface{}{ | ||
| "domain": requestDomain, | ||
| }) | ||
| c.Abort() | ||
| return | ||
| } | ||
|
|
||
| logger.WithFields(logger.Fields{ | ||
| "sender_id": sender.ID.String(), | ||
| "request_domain": requestDomain, | ||
| }).Debugf("Domain whitelist validation passed") | ||
|
|
||
| c.Next() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/google/uuid" | ||
| "github.com/paycrest/aggregator/ent" | ||
| ) | ||
|
|
||
| func TestDomainWhitelistMiddleware(t *testing.T) { | ||
| // Create a mock sender profile for testing | ||
| senderProfile := &ent.SenderProfile{ | ||
| ID: uuid.New(), | ||
| DomainWhitelist: []string{}, | ||
| IsActive: true, | ||
| } | ||
|
|
||
| gin.SetMode(gin.TestMode) | ||
| router := gin.New() | ||
|
|
||
| // Add middleware | ||
| router.Use(func(c *gin.Context) { | ||
| c.Set("sender", senderProfile) | ||
| c.Next() | ||
| }) | ||
| router.Use(DomainWhitelistMiddleware()) | ||
|
|
||
| router.GET("/sender/test", func(c *gin.Context) { | ||
| c.JSON(http.StatusOK, gin.H{"status": "success"}) | ||
| }) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| origin string | ||
| whitelist []string | ||
| expectedStatus int | ||
| expectedMessage string | ||
| }{ | ||
| { | ||
| name: "Empty whitelist allows all", | ||
| origin: "https://example.com", | ||
| whitelist: []string{}, | ||
| expectedStatus: http.StatusOK, | ||
| }, | ||
| { | ||
| name: "Whitelisted domain allowed", | ||
| origin: "https://example.com", | ||
| whitelist: []string{"example.com"}, | ||
| expectedStatus: http.StatusOK, | ||
| }, | ||
| { | ||
| name: "Non-whitelisted domain blocked", | ||
| origin: "https://malicious.com", | ||
| whitelist: []string{"example.com"}, | ||
| expectedStatus: http.StatusForbidden, | ||
| }, | ||
| { | ||
| name: "Subdomain allowed", | ||
| origin: "https://api.example.com", | ||
| whitelist: []string{"example.com"}, | ||
| expectedStatus: http.StatusOK, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Update the mock sender profile whitelist | ||
| senderProfile.DomainWhitelist = tt.whitelist | ||
|
|
||
| // Create request | ||
| req := httptest.NewRequest("GET", "/sender/test", nil) | ||
| req.Header.Set("Origin", tt.origin) | ||
|
|
||
| // Create response recorder | ||
| w := httptest.NewRecorder() | ||
|
|
||
| // Perform request | ||
| router.ServeHTTP(w, req) | ||
|
|
||
| // Check status | ||
| if w.Code != tt.expectedStatus { | ||
| t.Errorf("Expected status %d, got %d", tt.expectedStatus, w.Code) | ||
| } | ||
| }) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.