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

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews