Skip to content

[WIP] Update: Key-Pair auth data check #542

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

Merged
merged 3 commits into from
Jun 6, 2024
Merged

Conversation

farhanW3
Copy link
Contributor

@farhanW3 farhanW3 commented May 31, 2024

PR-Codex overview

The focus of this PR is to enhance authentication middleware in the server by adding keypair authentication with body hashing validation.

Detailed summary

  • Added createHash import for hashing
  • Updated hook from onRequest to preValidation
  • Modified handleKeypairAuth to include body hashing validation
  • Added hashRequestBody function to hash request body

✨ Ask PR-Codex anything about this PR by commenting with /codex {your question}

Comment on lines 285 to 288
if (data && req.method === "POST" && data !== hashRequestBody(req)) {
error = "The request body has been tampered with.";
throw error;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can only do this check when the request is of POST type, otherwise the req.body will come as empty

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we even need this check. If the developer hashes a request body and calls a GET endpoint, I think it's desirable (stricter) that the auth fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added the POST check as GET spec doesn't support body as per the HTTP/1.1. Some web servers (Apache, Nginx & Traefik) drop the body param for GET request when forwarding the request. The spec doesn't forbid it, but the HTTP servers do drop it.

@farhanW3 farhanW3 requested a review from arcoraven May 31, 2024 20:32
@@ -278,6 +282,11 @@ const handleKeypairAuth = async (
throw error;
}

if (data && req.method === "POST" && data !== hashRequestBody(req)) {
error = "The request body has been tampered with.";
Copy link
Contributor

Choose a reason for hiding this comment

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

This error sounds scary when the dev may have just made a mistake. Let's make sure errors directly state the issue and optionally provide a way to fix it:

The request body does not match the hash in the access token. See: https://portal.thirdweb.com/engine/features/keypair-authentication

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense.

can we tinyurl this?

@farhanW3 farhanW3 merged commit 0e2c0ea into main Jun 6, 2024
4 checks passed
@farhanW3 farhanW3 deleted the fk/key-pair-auth-upd branch June 6, 2024 00:59
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.

2 participants