Skip to content

Conversation

@nguyennk92
Copy link
Member

No description provided.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Bug: EIP-712 Hashing Bug with Dynamic Bytes

The isAtomic function incorrectly treats dynamic bytes as atomic, causing them to be directly encoded instead of keccak256 hashed in the generated EIP-712 hash function. Additionally, several variables (objs, fieldList, fields, hashString, functionString, obj) are implicitly declared as global, leading to scope pollution and potential conflicts.

gen-js/gen-sol.js#L3-L98

function isAtomic(t) {
return t.startsWith('bytes') || t.startsWith('uint') || t.startsWith('int') || t === 'bool' || t === 'address';
}
objs = {}
Object.keys(x).forEach(key => {
objs[key] = {
arr: x[key],
objName: key,
deps: [],
};
});
function getDeps(obj, deps = []) {
obj.arr.forEach(element => {
if (objs[element.type]) {
if (!deps.includes(element.type)) {
deps.push(element.type);
getDeps(objs[element.type], deps);
}
}
});
deps.sort();
return deps;
}
function getInitHashString(obj) {
fieldList = [];
obj.arr.forEach(element => {
fieldList.push(`${element.type} ${element.name}`);
});
fields = fieldList.join(',');
hashString = `${obj.objName}(${fields})`;
return hashString;
}
function getFullHashString(obj) {
hashString = getInitHashString(obj);
obj.deps.forEach(dep => {
hashString += getInitHashString(objs[dep]);
});
return hashString;
}
function genTypeHashStatement(obj) {
return ` // keccak256(
// \"${getFullHashString(obj)}\"
// );
bytes32 constant ${obj.objName}_TYPEHASH = ${keccak256(getFullHashString(obj))};` // do NOT delete spaces
}
function genStruct(obj) {
fieldList = [];
obj.arr.forEach(element => {
ename = element.name;
if (element.name === 'type') {
ename = '_type';
}
fieldList.push(` ${element.type} ${ename};\n`);
});
return ` struct ${obj.objName} {\n${fieldList.join('')} }` // do NOT delete spaces
}
function genHashFunction(obj) {
fieldList = [`${obj.objName}_TYPEHASH`];
obj.arr.forEach(element => {
ename = element.name;
if (element.name === 'type') {
ename = '_type';
}
if (isAtomic(element.type)) {
hashLine = `obj.${ename}`;
} else if (element.type === 'string') {
hashLine = `keccak256(bytes(obj.${ename}))`;
} else if (element.type === 'bytes') {
hashLine = `keccak256(obj.${ename})`;
} else {
hashLine = `_hash(obj.${ename})`;
}
fieldList.push(hashLine);
});
functionString = ` function _hash(${obj.objName} memory obj) internal pure returns (bytes32) {
return keccak256(abi.encode(
${fieldList.join(',\n ')}
));
}`
return functionString;
}
Object.keys(objs).forEach(key => {
objs[key].deps = getDeps(objs[key]);
});
Object.keys(objs).forEach(key => {
obj = objs[key];

Fix in Cursor


Comment bugbot run to trigger another review on this PR
Was this report helpful? Give feedback by reacting with 👍 or 👎

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.

1 participant