Skip to content

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

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

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

Conversation

@Dargon789
Copy link
Owner

@Dargon789 Dargon789 commented Oct 7, 2025

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

To completely mitigate SSRF possibilities, we need to ensure the interpolated user input into the URL path does not allow for any path traversal or URL manipulation. The current isValidEthereumAddress function should strictly enforce that address consists only of the allowed Ethereum address characters (0x + 40 lowercase hexadecimal digits), without allowing any extra characters. After validation/normalization, the value should not contain slashes, periods, or any reserved URI characters.

For extra certainty and defense-in-depth, we should:

  • Add a stricter validation regex that rejects anything except the expected form (e.g., /^0x[a-f0-9]{40}$/).
  • Use this stricter check before constructing the URL or reject the request if invalid.
  • (Optional but recommended) Make the fetchStamps function accept only a validated address or refuse unsafe forms.

You only need to edit within the code shown in the snippet in app/api/metadata/gitcoin/[address]/route.ts.


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

Summary by Sourcery

Enforce strict Ethereum address validation in the GET route to mitigate SSRF risks by rejecting any input that does not match the exact lowercase 0x-prefixed 40-hex-digit format.

Bug Fixes:

  • Prevent SSRF by rejecting requests with invalid or manipulated Ethereum addresses before URL construction.

Enhancements:

  • Add STRICT_ETH_ADDR_RE regex to ensure only lowercase "0x" + 40 hexadecimal characters are accepted.
  • Normalize and re-validate the address string to lowercase and enforce the strict regex check.

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

sourcery-ai bot commented Oct 7, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR strengthens SSRF mitigation by introducing a strict regex for Ethereum addresses and integrating it into the GET handler validation flow, ensuring only lowercase, 0x-prefixed 40-hex-digit addresses are accepted before any server request is made.

Sequence diagram for stricter Ethereum address validation in GET API route

sequenceDiagram
actor User
participant API as "API Route (/metadata/gitcoin/[address])"
participant Validator as "Ethereum Address Validator"
User->>API: GET request with address param
API->>Validator: Validate address (isValidEthereumAddress)
Validator-->>API: Validation result
API->>Validator: Apply STRICT_ETH_ADDR_RE regex
Validator-->>API: Regex test result
alt Address is valid
    API->>User: Return metadata response
else Address is invalid
    API->>User: Return empty response
end
Loading

Class diagram for updated address validation logic

classDiagram
class "API Route Handler" {
  +GET(req: NextRequest): Promise<NextResponse>
}
class "Ethereum Address Validator" {
  +isValidEthereumAddress(address: string): boolean
  +STRICT_ETH_ADDR_RE: RegExp
}
"API Route Handler" --> "Ethereum Address Validator" : uses
Loading

File-Level Changes

Change Details Files
Add STRICT_ETH_ADDR_RE to enforce strict Ethereum address pattern
  • Define regex /^0x[a-f0-9]{40}$/ to match lowercase, 0x-prefixed 40-hex-digit addresses
app/api/metadata/gitcoin/[address]/route.ts
Enhance GET handler address validation
  • Normalize user input by stripping and lowercasing the address
  • Ensure '0x' prefix is present before regex test
  • Combine isValidEthereumAddress check with STRICT_ETH_ADDR_RE.test in the conditional
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 7, 2025 15:42
Copy link

@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 and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `app/api/metadata/gitcoin/[address]/route.ts:61` </location>
<code_context>
+  if (
+    !address ||
+    !isValidEthereumAddress(address) ||
+    !STRICT_ETH_ADDR_RE.test("0x" + address.replace(/^0x/i, "").toLowerCase())
+  ) {
     return NextResponse.json(createResponse(0, []));
</code_context>

<issue_to_address>
**question (bug_risk):** Lowercasing the address before validation may cause issues with checksum addresses.

Forcing lowercase may reject valid EIP-55 checksum addresses. If lowercase is required, update documentation or error messages to clarify this behavior.
</issue_to_address>

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 16d0306 into main Oct 7, 2025
4 of 5 checks passed
@Dargon789 Dargon789 deleted the alert-autofix-7 branch October 7, 2025 15:49
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

Comments