If I log into a new project with Metamask, and the operation costs no gas, can I get hacked?
Does no gas mean you are safe when signing with Metamask?
Kind of, you can sign a message off-chain for zero gas that could be used maliciously.
One of the oldest, and most dangerous ways to sign data, is
eth_sign. See this quote from Metamask:
it allows the website you're on to request that you sign an arbitrary hash. In this mathematical context, 'arbitrary' means unspecified; your signature could be applied by the requesting dapp to pretty much anything. eth_sign is therefore unsuitable to use with sources that you don't trust.
However, in 2016, EIP 191 standardized
"\x19Ethereum Signed Message:\n" being prepended before any signed data. This essentially solves the issue listed above, see this quote from Ethereum.org:
By adding a prefix to the message makes the calculated signature recognizable as an Ethereum specific signature. This prevents misuse where a malicious dapp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
Ok so now that transactions can't be sent directly from a malicious JSON RPC
eth_sign, this doesn't mean you are entirely safe. Also please note that as of October 3rd, 2023, Metamask still contains the original
eth_sign method, however, it is disabled by default and must be enabled in the settings.
personal_sign will implement the prefix specifying that it is a signed message.
The new updated JSON-RPC
eth_sign can still be used in unwanted ways, such as bypassing a function that uses ecrecover to prove ownership.
EIP 712 introduced
eth_signTypedData which defines a standard for how data will be structured and signed, thus allowing the data to be more human-readable and easily verifiable. If the DeFi protocol you are using has implemented this standard, or any of its upgraded versions such as
eth_signTypedData_v4, you should be able to see exactly what you are signing.
Regardless, unless you fully understand what you are doing, it's probably best to only sign transactions from trusted parties to prevent your signature from being used maliciously.
Some contracts support the use of signatures to authorize third parties to perform operations, so you may suffer losses due to signing without knowing the content.
For example, some tokens, e.g. DAI and USDC, implement EIP-2612 that allow users approve by signature.