Skip to content

Potential fix for code scanning alert no. 7: Server-side request forgery#1

Merged
Dargon789 merged 1 commit intomainfrom
alert-autofix-7
Oct 3, 2025
Merged

Potential fix for code scanning alert no. 7: Server-side request forgery#1
Dargon789 merged 1 commit intomainfrom
alert-autofix-7

Conversation

@Dargon789
Copy link
Copy Markdown
Owner

@Dargon789 Dargon789 commented Oct 3, 2025

Potential fix for https://github.com/Dargon789/web3bio/security/code-scanning/7

To eliminate any risk of SSRF, ensure that address is strictly validated before it is used as part of an outgoing URL. Currently, the validation logic uses isValidEthereumAddress(address). To strengthen this, we should guarantee the address string is normalized (e.g., ensure it’s lowercase and starts with "0x"), and recheck that it contains only allowed characters (hex string of 40 chars after "0x", or 42 with the prefix). The best fix is to sanitize and normalize the value right after the validation in the GET method, so the only value passed into fetchStamps and further is guaranteed safe.

Thus:

  • After validating isValidEthereumAddress(address), normalize and strictly parse the address to ensure canonical, unambiguous behavior.
  • Replace the usage of user input in the URL with this normalized/sanitized value.
  • If isValidEthereumAddress is robust and you can guarantee this, the fix can be minimal: forcibly lowercasing and enforcing "0x" prefix before passing to fetchStamps (or, if not, add a simple sanitation function inline).

Make this change in GET() in app/api/metadata/gitcoin/[address]/route.ts after the validation, before using address.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

Summary by Sourcery

Sanitize and normalize the Ethereum address in the Gitcoin metadata API route to prevent server-side request forgery by enforcing a lowercase 0x-prefixed canonical format before making external requests.

Bug Fixes:

  • Mitigate SSRF by normalizing the validated address to lowercase with an enforced 0x prefix.
  • Replace direct use of the raw address in fetchStamps with the sanitized safeAddress.

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: AU_gdev_19 <[email protected]>
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented Oct 3, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

After validating the Ethereum address, the PR normalizes it to a canonical lowercase form with a ‘0x’ prefix and updates the downstream fetch call to use this sanitized value, eliminating the SSRF risk.

Sequence diagram for Ethereum address normalization in GET request

sequenceDiagram
participant Client
participant API_Server
participant fetchStamps
Client->>API_Server: GET /api/metadata/gitcoin/[address]
API_Server->>API_Server: Validate address (isValidEthereumAddress)
API_Server->>API_Server: Normalize address (lowercase, enforce '0x' prefix)
API_Server->>fetchStamps: fetchStamps(safeAddress)
fetchStamps-->>API_Server: Return stamps
API_Server-->>Client: Response with details
Loading

Class diagram for address normalization in GET method

classDiagram
class APIHandler {
  +GET(req: NextRequest): Promise<NextResponse>
}
APIHandler : -address: string
APIHandler : -safeAddress: string
APIHandler : +isValidEthereumAddress(address: string): boolean
APIHandler : +fetchStamps(address: string): Stamp[]
APIHandler : +Normalization: safeAddress = "0x" + address.replace(/^0x/i, "").toLowerCase()
Loading

File-Level Changes

Change Details Files
Normalize and sanitize Ethereum address before outgoing request
  • Inserted logic to strip any existing prefix, lowercase the address, and prepend ‘0x’
  • Replaced raw address usage in fetchStamps with the new safeAddress variable
app/api/metadata/gitcoin/[address]/route.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Dargon789 Dargon789 marked this pull request as ready for review October 3, 2025 11:25
Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • After normalizing the address, re-run your isValidEthereumAddress check (or an equivalent) on safeAddress to guard against any unexpected input circumvention.
  • For more robust normalization and validation, consider using a battle-tested utility like ethers.js’s getAddress to checksum and canonicalize the address instead of manual string ops.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- After normalizing the address, re-run your isValidEthereumAddress check (or an equivalent) on `safeAddress` to guard against any unexpected input circumvention.
- For more robust normalization and validation, consider using a battle-tested utility like ethers.js’s `getAddress` to checksum and canonicalize the address instead of manual string ops.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Dargon789 Dargon789 merged commit 1a019e5 into main Oct 3, 2025
4 checks passed
@Dargon789 Dargon789 deleted the alert-autofix-7 branch October 3, 2025 11:26
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.

1 participant