r/shittyprogramming • • Mar 30 '17

This is how you properly sanitize user input

Post image
369 Upvotes

31 comments sorted by

69

u/down_vote_magnet Mar 30 '17

I'm not sure if I opened and viewed the image or if I had a stroke.

9

u/_Skitzzzy Mar 31 '17

You sure it's not both?

8

u/Mitiz Apr 02 '17
private String SanitizeInput(String input) {
    return input;
}

9

u/ShowMeYourTiddles Mar 30 '17

My god! For-loop with a switch statement ffs.

0

u/[deleted] Mar 30 '17

[deleted]

7

u/jimmpony Mar 30 '17

weird cases like this make me appreciate how Visual Studio enforces a standard styling for c# that you can change parts of with editorconfig (now natively included) and you can fix a file with a short key combo

0

u/[deleted] Mar 30 '17

[deleted]

4

u/[deleted] Mar 31 '17

The eternal programmer paradox:

  • job is to make other people's lives easier

  • goes out of their way to make things as difficult as possible for themselves

0

u/[deleted] Mar 31 '17

[deleted]

1

u/jarfil Mar 31 '17 edited Dec 02 '23

CENSORED

2

u/BromeyerofSolairina Mar 31 '17

Have mercy on us!

1

u/shatteredarm1 Mar 31 '17

Maybe this would be better? Works in C#:

for(int i = 0; i < 10; (new Action<int>((int j) => { switch(j
){
    case 1: ... 
}}))(i++));

4

u/jimmpony Mar 30 '17

even if they didn't want to use built in escaping functions, why didn't they just remove all <>'etc instead of trying to enumerate every tag possible to be malicious with?

2

u/[deleted] Mar 30 '17

it's Classic ASP. Try not to expect too much.

1

u/SarahC Mar 31 '17

Shouldn't dictate the caliber of programmer?

3

u/LowB0b Mar 30 '17

is this from a coworker of yours? please tell him about regex, loops and arrays :(

2

u/[deleted] Mar 30 '17

Just noticed the SanitizeInput function. If that doesn't 'sanitize' the input properly then I have no idea what it's supposed to do otherwise.

6

u/tomharto Mar 31 '17

It vaguely reminds me of how my step mum used to clean up the house before the cleaner they have comes around.

1

u/DJDarkViper Mar 31 '17

Oops I think it's missing a couple trims in there. Tsk tsk.

1

u/[deleted] Mar 31 '17

Hmmm years before I had this exact same PHP code.

1

u/FR_STARMER Mar 31 '17

Look regex is hard okay

1

u/[deleted] Apr 08 '17

Goddamnit lmao

1

u/zhezow Apr 11 '17

Thanks! Very nice tip! Can you send me as a text, so I can copy and use on my project?

1

u/[deleted] Apr 13 '17

nothing was replaced in the linked image.

0

u/[deleted] Mar 30 '17

[deleted]

11

u/zeugma25 Mar 31 '17

It also fails for names like O'conner

it should correct it to O'Connor?

2

u/masterwit Mar 31 '17

Oh! Conner

0

u/[deleted] Mar 30 '17

[deleted]

2

u/SandalsMan Apr 25 '17
&lt;img \x00src=x onerror="alert`1`"&gt;

Use backticks :)

1

u/theavengedCguy Mar 31 '17

Can you explain this piece of code that you wrote please? I'm guessing that it embeds arbitrary code within an img tag, but how?

8

u/chrismbarr Mar 31 '17

The one source is set to a bad URL, so when the image fails to load it will run the script in the onerror attribute. That's just something built into HTML that is being exploited here.

0

u/[deleted] Mar 30 '17

[deleted]

10

u/maurycy0 Mar 31 '17

It doesn't. This wouldn't work.

5

u/michael1026 Mar 31 '17

I've never seen it work in practice.