Skip to content

Comments

Fix: Prevent .eth.eth Suffix in bytesToPacket Decoding#263

Open
st-mn wants to merge 2 commits intoensdomains:mainfrom
st-mn:ensjs-fix-label-handeling-suffix-issue-222
Open

Fix: Prevent .eth.eth Suffix in bytesToPacket Decoding#263
st-mn wants to merge 2 commits intoensdomains:mainfrom
st-mn:ensjs-fix-label-handeling-suffix-issue-222

Conversation

@st-mn
Copy link

@st-mn st-mn commented Oct 11, 2025

Fix: Prevent .eth.eth Suffix in bytesToPacket Decoding

Problem

When decoding DNS-encoded ENS names using bytesToPacket, certain packet bytes (e.g., 0x08383433362E6574680365746800) would incorrectly produce names with a double .eth suffix, such as 8436.eth.eth instead of the expected 8436.eth. This occurs when the encoded packet contains two labels: one that already ends with .eth and a trailing eth label, leading to an erroneous concatenation.

Solution

This PR updates the bytesToPacket function in hexEncodedName.ts to handle this edge case:
After decoding the labels, if the last label is 'eth' and the previous label already ends with '.eth', the function removes the trailing 'eth' label before joining the labels.
This ensures that names like 8436.eth.eth are correctly normalized to 8436.eth, while still allowing valid cases such as [labelhash].eth to decode as expected.

Implementation Details

The function now checks the decoded label array for the described pattern and mutates the array accordingly before joining.

Testing

Added/updated unit tests in hexEncodedName.test.ts to cover the problematic case and ensure no regression for other valid ENS names.
All tests pass, confirming the fix.

References

Fixes: #222
Context: namehash/ensnode#36 (comment)

st-mn added 2 commits October 11, 2025 18:50
Problem
When decoding DNS-encoded ENS names using bytesToPacket, certain packet bytes (e.g., 0x08383433362E6574680365746800) would incorrectly produce names with a double .eth suffix, such as 8436.eth.eth instead of the expected 8436.eth. This occurs when the encoded packet contains two labels: one that already ends with .eth and a trailing eth label, leading to an erroneous concatenation.

Solution
This PR updates the bytesToPacket function in hexEncodedName.ts to handle this edge case:

After decoding the labels, if the last label is 'eth' and the previous label already ends with '.eth', the function removes the trailing 'eth' label before joining the labels.
This ensures that names like 8436.eth.eth are correctly normalized to 8436.eth, while still allowing valid cases such as [labelhash].eth to decode as expected.
Implementation Details
The function now checks the decoded label array for the described pattern and mutates the array accordingly before joining.
The logic is robust against other edge cases and does not affect names that are supposed to end with .eth.
Testing
Added/updated unit tests in hexEncodedName.test.ts to cover the problematic case and ensure no regression for other valid ENS names.
All tests pass, confirming the fix.
References
Fixes: ensdomains#222
Context: namehash/ensnode#36 (comment)

	new file:   packages/ensjs/src/utils/hexEncodedName.test.ts
	modified:   packages/ensjs/src/utils/hexEncodedName.ts
	modified:   packages/ensjs/src/utils/hexEncodedName.ts
@sonarqubecloud
Copy link

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.

bytesToPacket doesn't handle malformed label packet correctly

1 participant