Phoenix 中的 Atom 键与字符串键
Atom keys vs string keys in Phoenix
在新的 Programming Phoenix 书中,Chris McCord 谈到了将字符串和原子键用于控制器操作参数:
In the world action in our controllers, the external parameters have string keys, "name" => name, while internally we use name: name. That’s a convention followed throughout Phoenix. External data is unsafe, so we explicitly match on the string keys, and then our application boundaries like controllers and channels will convert them into atoms keys which we will rely on everywhere else inside Phoenix.
但是,我不清楚为什么使用字符串密钥比原子密钥更安全。为什么字符串键在这里是更安全的解决方案?
默认情况下,Erlang VM 中的最大原子数为 1048576。因此,通过将外部值转换为原子,您将填充全局原子 table,这不会被垃圾收集。因此,您很容易受到拒绝服务攻击。
Relevant SO answer
在新的 Programming Phoenix 书中,Chris McCord 谈到了将字符串和原子键用于控制器操作参数:
In the world action in our controllers, the external parameters have string keys, "name" => name, while internally we use name: name. That’s a convention followed throughout Phoenix. External data is unsafe, so we explicitly match on the string keys, and then our application boundaries like controllers and channels will convert them into atoms keys which we will rely on everywhere else inside Phoenix.
但是,我不清楚为什么使用字符串密钥比原子密钥更安全。为什么字符串键在这里是更安全的解决方案?
默认情况下,Erlang VM 中的最大原子数为 1048576。因此,通过将外部值转换为原子,您将填充全局原子 table,这不会被垃圾收集。因此,您很容易受到拒绝服务攻击。
Relevant SO answer