Skip to content

Conversation

@average-gary
Copy link
Contributor

The UserIdentity TLV field has a protocol-specified maximum of 32 bytes. When downstream miners send mining.authorize with long worker names, the translator would panic on unwrap().

This fix:

  • Rejects mining.authorize if the username portion (before the dot) exceeds 32 bytes, as this indicates an invalid configuration
  • Truncates only the worker name portion if the total length exceeds 32 bytes, preserving the username
  • Adds defensive error handling in sv1_server.rs as a fallback

@average-gary average-gary force-pushed the fix/user-identity-validate-authorize branch from 67bf1cf to 190ec4c Compare January 21, 2026 19:20
@average-gary
Copy link
Contributor Author

Rebased onto #162

Pr ot that branch here:
Shourya742#1

@average-gary average-gary force-pushed the fix/user-identity-validate-authorize branch from 190ec4c to 30d4718 Compare January 22, 2026 17:06
MAX_USER_IDENTITY_LENGTH,
username.len()
);
return false;

Choose a reason for hiding this comment

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

Is there a more specific error type we should use here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The return type is defined upstream in stratum_core::sv2_api

The UserIdentity TLV field has a protocol-specified maximum of 32 bytes.
When downstream miners send mining.authorize with long worker names,
the translator would panic on unwrap().

This fix:
- Rejects mining.authorize if the username portion (before the dot)
  exceeds 32 bytes, as this indicates an invalid configuration
- Truncates only the worker name portion if the total length exceeds
  32 bytes, preserving the username
- Adds defensive error handling in sv1_server.rs as a fallback
Byte-slicing a string (e.g., &name[..32]) can panic if the index falls
within a multi-byte UTF-8 character. Use is_char_boundary() to find a
safe truncation point. Also removes an unnecessary .clone() on String.
@average-gary average-gary force-pushed the fix/user-identity-validate-authorize branch from 8acdf30 to 9583132 Compare January 23, 2026 15:16
@GitGab19
Copy link
Member

I think we have some misconceptions, and that's probably due to how we called and managed some fields in the DownstreamData in the past.

We currently have authorized_worker_name and user_identity there, but we are currently setting both values with the same string we receive from the mining.authorize message.

Here the changes I would apply:

  • rename authorized_worker_name into worker_name_from_authorize to better reflect that this field contains the exact string we receive from the Sv1 mining.authorize
  • assign to user_identity only the suffix part of what we receive from the Sv1 mining.authorize (e.g. in case of gitgab.worker1 the user_identity will contain only worker1)
    • if there's no . delimiter, assign the same value as authorized_worker_name
  • we don't check the length of any of these fields in the authorize step of the initial "Sv1 handshake", because we don't even know if we need to use the extension or not there
  • in the Sv1Server, where we create the TLV using the user_identity field of the DownstreamData we handle the result and, if we get an error from the new() (the check on the length is already made there), we return an error and we disconnect the client

Does it make sense to you @average-gary ?

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.

3 participants