Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/harden-parse-struct-tag.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/sui': patch
---

Fix `parseStructTag` to reject malformed inputs: empty address/module/name components (e.g. `::foo::Bar`) and trailing content after type parameters (e.g. `Coin<u8>GARBAGE`).
10 changes: 10 additions & 0 deletions packages/sui/src/utils/sui-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,20 @@ export function parseStructTag(type: string): StructTag {
}

const [address, module] = parts;

if (!address || !module) {
throw new Error(`Invalid struct tag: ${type}`);
}

const isMvrPackage = isValidNamedPackage(address);

const rest = type.slice(address.length + module.length + 4);
const name = rest.includes('<') ? rest.slice(0, rest.indexOf('<')) : rest;

if (!name || (rest.includes('<') && !rest.endsWith('>'))) {
throw new Error(`Invalid struct tag: ${type}`);
}

const typeParams = rest.includes('<')
? splitGenericParameters(rest.slice(rest.indexOf('<') + 1, rest.lastIndexOf('>'))).map(
(typeParam) => parseTypeTag(typeParam.trim()),
Expand Down
12 changes: 12 additions & 0 deletions packages/sui/test/unit/types/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ describe('parseStructTag', () => {
expect(() => parseStructTag('0x2::foo::Bar<vector<u8>')).toThrow('Invalid type tag');
});

it('rejects struct tags with empty components', () => {
expect(() => parseStructTag('::foo::Bar')).toThrow('Invalid struct tag');
expect(() => parseStructTag('0x2::::Bar')).toThrow('Invalid struct tag');
expect(() => parseStructTag('0x2::foo::')).toThrow('Invalid struct tag');
});

it('rejects struct tags with trailing content after type parameters', () => {
expect(() => parseStructTag('0x2::coin::Coin<u8>GARBAGE')).toThrow('Invalid struct tag');
expect(() => parseStructTag('0x2::foo::Bar<bool> ')).toThrow('Invalid struct tag');
expect(() => parseStructTag('0x2::foo::Bar<u64>xyz')).toThrow('Invalid struct tag');
});

it('parses named struct tags correctly', () => {
expect(parseStructTag('@mvr/demo::foo::bar')).toMatchInlineSnapshot(`
{
Expand Down