johmue

johmue

When working on Phoenix projects I sometimes need to take integers as a string as user input from HTTP requests. When I use String.to_integer/1 to get the integer values, an attacker could easily craft a request that makes String.to_integer/1 raise an exception and thus crash the Erlang process.

Question: Is this failing String.to_integer/1 a security issue? Or does will only make the concerning Erlang process crash without further damage? What do I have to look after?

Showing Posts 1 to 10

kokolegorille

kokolegorille

You might use guard clause to avoid this kind of error, no need to let it crash.

You might also use changeset to validate outside data, which is probably the cleanest way.

Another solution is to use Integer.parse, when there is a chance String.to_integer will fail.

Never trust user data :slight_smile:

johmue

johmue OP

Yes, of course, that is the cleanest solution. The question is, when I see such a potential crash in somewhere in production code, do I simply silently fix it and let it deploy one day or do I immediately shutdown the service and ship a hotfix?

kokolegorille

kokolegorille

I think You should prevent those errors.

But if You have them, it will just crash the process, I don’t think it’s urgent like patching Log4j :slight_smile:

eksperimental

eksperimental

From the docs:

https://hexdocs.pm/elixir/String.html#to_integer/1

If you want to parse a string that may contain an ill-formatted integer, use Integer.parse/1.

dimitarvp

dimitarvp

I feel your question is a bit philosophical and thus ignores context. There’s no right answer. The good question here is: is failing to parse the integer an actual error condition or is it acceptable?

But still, if we’re talking out of context then in my mind something like parsing an integer should never be allowed to error out. Pattern match the result of Integer.parse.

trisolaran

trisolaran

IMO, and in the specific context of web applications, an invalid input should NEVER make your process crash (thus resulting in a 500 error). Instead, it should always be handled by the application and trigger a 400 or 422 response.

Nicd

Nicd

Answering your direct question about security issues, then by itself it’s not a security issue. Phoenix will reply with a 500 error to the user which is not good UX, but assuming you’re in prod mode, it won’t leak any details. The memory of the process will be garbage collected and the system stability won’t be affected.

Now where it can be an issue is if your system relies on the process to do something security related and it crashes in the middle of that, leaving side effects or leaving something undone. Usually you’d use DB transactions to prevent issues from such matters but maybe you’re also talking to external systems, handling files, etc., where it’s not that simple. Only you can spot those dangerous places in your application.

johmue

johmue OP

Thanks for all the answers.

To wrap up:

  • It’s not a security issue per se. However it might be, if the application relies on crashing process to succeed.
  • Of course it is in any way a good choice to pattern match against the result of Integer.parse/1
trisolaran

trisolaran

And I would add that, even though it’s not a security issue, it’s definitely a UX issue as @Nicd pointed out. As a user, you simply don’t want to see your application crash because you entered an invalid input. Instead, you want an error message that tells you what you did wrong.

The request doesn’t need to be malicious, it could simply be a mistake :slight_smile:

johmue

johmue OP

I am thinking of cases where the exact request is formed in a LiveView by a phx-value-number={some.number} binding. So when I assume that the user does not explicitly inject a JSON record with a broken integer string, the issue will never be triggered and thus a “normal” user will never see the 500. Only the ones who manually inject some JSON data will see it, and the UX for those people is let’s say, secondary :slight_smile:

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews