Convert a map into a keyword list in Elixir

对着背影说爱祢 提交于 2020-06-17 06:08:48

问题


I have a map of the form:

%{"browser_name" => "Chrome", "platform" => "linux"}

and I need to convert it to a keyword list:

[browser_name: "Chrome", platform: "linux"]

What's the best way of achieving this?


回答1:


Wouldn't this work:

 def ConvertMapToKList(dict) do
     Enum.map(dict, fn({key, value}) -> {String.to_existing_atom(key), value} end)
 end



回答2:


I would put it here for the sake of future readers.

While one surely might blindly call String.to_atom/1 here and there, that approach is strongly discouraged due to its vulnerability to atom DoS attacks.

Here is the excerpt from Erlang docs:

Atoms are not garbage-collected. Once an atom is created, it is never removed. The emulator terminates if the limit for the number of atoms (1,048,576 by default) is reached.

Therefore, converting arbitrary input strings to atoms can be dangerous in a system that runs continuously. If only certain well-defined atoms are allowed as input, list_to_existing_atom/1 can be used to guard against a denial-of-service attack. (All atoms that are allowed must have been created earlier, for example, by simply using all of them in a module and loading that module.)

— http://erlang.org/doc/efficiency_guide/commoncaveats.html#list_to_atom-1

That said, whether you receive the map from the external untrusted source (e. g. it’s the parameters in the call to your API or something,) you must not use String.to_atom/1 and should use String.to_existing_atom/1 instead.

Otherwise, the intruder with a simple random generator for keys would blow up your ErlangVM without any issue.



来源:https://stackoverflow.com/questions/54616306/convert-a-map-into-a-keyword-list-in-elixir

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!