Skip to content

Potential fix for code scanning alert no. 25: Server-side request forgery#18

Merged
Dargon789 merged 1 commit intomasterfrom
alert-autofix-25
Jul 5, 2025
Merged

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

Conversation

@Dargon789
Copy link
Owner

@Dargon789 Dargon789 commented Jul 5, 2025

Potential fix for https://github.com/Dargon789/mempool/security/code-scanning/25

To fix the SSRF vulnerability, we need to ensure that user input (req.originalUrl) is validated and sanitized before being used to construct the url. Specifically:

  1. Restrict the user input to a predefined set of allowed paths or endpoints using an allow-list.
  2. Ensure that the constructed url does not allow path traversal or other manipulations that could redirect the request to unintended endpoints.

The best approach is to use an allow-list to map user input to known, safe paths. This ensures that the outgoing request is limited to trusted endpoints.

Changes to make:

  • Introduce an allow-list of valid paths or endpoints.
  • Replace the direct use of req.originalUrl.replace('/api/v1/services/', '') with a lookup in the allow-list.
  • Reject requests with invalid or unrecognized paths.

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

Summary by Sourcery

Restrict service requests to a predefined set of safe endpoints by mapping user input through an allow-list and rejecting any unexpected paths to fix a server-side request forgery vulnerability

Bug Fixes:

  • Mitigate SSRF by validating and sanitizing request paths before constructing service URLs

Enhancements:

  • Introduce an allow-list for mapping user-supplied paths to safe service endpoints
  • Reject requests with invalid or unrecognized paths by returning a 400 error

…gery

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 Jul 5, 2025

Reviewer's Guide

Fix SSRF by validating req.originalUrl against a predefined allow-list and constructing outgoing URLs only from known safe endpoints, logging and rejecting any invalid user paths with an HTTP 400 response.

Sequence diagram for SSRF-protected acceleration service request

sequenceDiagram
    actor User
    participant API as AccelerationRoutes
    participant Logger
    participant MempoolService
    User->>API: HTTP request to /api/v1/services/{userPath}
    API->>API: Extract userPath from req.originalUrl
    API->>API: Lookup userPath in allow-list
    alt userPath is allowed
        API->>MempoolService: Forward request to safe endpoint
        MempoolService-->>API: Response
        API-->>User: Response
    else userPath is not allowed
        API->>Logger: Log invalid path
        API-->>User: HTTP 400 with error
    end
Loading

File-Level Changes

Change Details Files
Introduce allow-list validation for service route paths to prevent SSRF
  • Defined an allowedPaths mapping for valid service endpoints
  • Extracted and validated userPath from req.originalUrl via allow-list lookup
  • Replaced dynamic URL construction using raw input with ${config.MEMPOOL_SERVICES.API}/${safePath}
  • Logged errors and sent HTTP 400 responses when userPath is not recognized
backend/src/api/acceleration/acceleration.routes.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 July 5, 2025 05:27
@Dargon789 Dargon789 merged commit 18e681a into master Jul 5, 2025
22 checks passed
@Dargon789 Dargon789 deleted the alert-autofix-25 branch July 5, 2025 05:27
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 @Dargon789 - 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> `backend/src/api/acceleration/acceleration.routes.ts:51` </location>
<code_context>
+    const userPath = req.originalUrl.replace('/api/v1/services/', '');
+    const safePath = allowedPaths[userPath];
+    if (!safePath) {
+      logger.err(`Invalid path requested: ${userPath}`, this.tag);
+      res.status(400).send({ error: 'Invalid path' });
+      return;
</code_context>

<issue_to_address>
Logging user input directly may expose sensitive information.

Sanitize or restrict the logged userPath to prevent sensitive or malicious data from appearing in logs.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
    if (!safePath) {
      logger.err(`Invalid path requested: ${userPath}`, this.tag);
      res.status(400).send({ error: 'Invalid path' });
      return;
=======
    if (!safePath) {
      // Log only the attempted key, not the raw user input, to avoid leaking sensitive data
      const sanitizedPath = Object.prototype.hasOwnProperty.call(allowedPaths, userPath) ? userPath : '[unrecognized]';
      logger.err(`Invalid path requested: ${sanitizedPath}`, this.tag);
      res.status(400).send({ error: 'Invalid path' });
      return;
>>>>>>> REPLACE

</suggested_fix>

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 linked an issue Oct 2, 2025 that may be closed by this pull request
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.

GitHub Actions GKE deployment workflow

1 participant