Skip to content

Conversation

@allouis
Copy link
Contributor

@allouis allouis commented Jul 28, 2025

This refactors the doubleKnock function and related signing functions to get the request body once at the top level instead of cloning it multiple times during iterations and redirects.

Changes:

  • Add optional body parameter to SignRequestOptions and DoubleKnockOptions
  • Update signRequestDraft and signRequestRfc9421 to accept pre-read body
  • Modify doubleKnock to read body once and pass through all operations
  • Update redirect handling to reuse the same body buffer

This has already been approved and merged into main but we want to backport the bug fix to 1.7.x

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @allouis, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a bug where request.clone() could cause errors in the doubleKnock function, particularly during iterations and redirects. The core change involves centralizing the reading of the request body to a single operation and then passing this pre-read body buffer throughout the signing and redirection processes, improving robustness and efficiency.

Highlights

  • Refactored Request Body Handling: I've refactored the doubleKnock function and related HTTP signing functions (signRequestDraft, signRequestRfc9421) to prevent request.clone() errors. The request body is now read once at the top level of doubleKnock and passed as an ArrayBuffer through subsequent operations, including signing and redirect handling, avoiding multiple costly and error-prone cloning operations.
  • API Surface Extension: I've extended the SignRequestOptions and DoubleKnockOptions interfaces with an optional body parameter (ArrayBuffer | null). This allows callers to pre-provide the request body, further optimizing scenarios where the body is already available.
  • Improved Redirect Logic: The redirect handling within doubleKnock has been updated to consistently reuse the pre-read request body buffer, eliminating redundant body cloning during redirects and ensuring proper request signing for redirected requests.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions bot added component/signatures OIP or HTTP/LD Signatures related priority/medium Medium priority issues runtime/node Node.js runtime related status/ready for review Ready for review type/refactor Code refactoring labels Jul 28, 2025
@allouis allouis changed the base branch from main to 1.7-maintenance July 28, 2025 03:47
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses the request.clone() errors in doubleKnock by refactoring the logic to read the request body only once at the top level. The changes are consistent and well-implemented, passing the pre-read body down to the signing functions and recursive calls. The addition of a regression test is also a great way to ensure the fix is effective and prevents future regressions.

I've added a few suggestions to improve the readability of the new body-handling logic by refactoring the nested ternary expressions into clearer if/else blocks. This should enhance the code's maintainability.

This refactors the doubleKnock function and related signing functions to
get the request body once at the top level instead of cloning it
multiple times during iterations and redirects.

Changes:
- Add optional `body` parameter to SignRequestOptions and DoubleKnockOptions
- Update signRequestDraft and signRequestRfc9421 to accept pre-read body
- Modify doubleKnock to read body once and pass through all operations
- Update redirect handling to reuse the same body buffer
@dahlia dahlia force-pushed the fix-request-clone-issues branch from 9ea2c21 to fc7178a Compare July 28, 2025 05:19
@github-actions
Copy link
Contributor

The latest push to this pull request has been published to JSR and npm as a pre-release:

@github-actions
Copy link
Contributor

The docs for this pull request have been published:

https://db85221e.fedify.pages.dev

@dahlia dahlia merged commit 8174a58 into fedify-dev:1.7-maintenance Jul 28, 2025
16 checks passed
@dahlia
Copy link
Member

dahlia commented Jul 28, 2025

Shipped with v1.7.7.

allouis added a commit to TryGhost/ActivityPub that referenced this pull request Jul 28, 2025
ref fedify-dev/fedify#335
ref fedify-dev/fedify#324

This bump to fedify includes two fixes the the doubleKnock
implementation. One which is an attempt to remedy the 'unusable' errors
when cloning the request objects, and the other which handles relative
paths in the Location header when recieving a redirect.

Both of these errors cause significant noise in our production
environment and cleaning them up should allow us to spot real errors a
lot easier in future.
allouis added a commit to TryGhost/ActivityPub that referenced this pull request Jul 28, 2025
ref fedify-dev/fedify#335
ref fedify-dev/fedify#324

This bump to fedify includes two fixes the the doubleKnock
implementation. One which is an attempt to remedy the 'unusable' errors
when cloning the request objects, and the other which handles relative
paths in the Location header when recieving a redirect.

Both of these errors cause significant noise in our production
environment and cleaning them up should allow us to spot real errors a
lot easier in future.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/signatures OIP or HTTP/LD Signatures related priority/medium Medium priority issues runtime/node Node.js runtime related status/ready for review Ready for review type/refactor Code refactoring

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants