What is the best way to safely read user input?

Dimitrios K.

Let's consider a REST endpoint which receives a JSON object. One of the JSON fields is a String, so I want to validate that no malicious text is received.

@ValidateRequest
public interface RestService {
  @POST
  @Consumes(APPLICATION_JSON)
  @Path("endpoint")
  void postData (@Valid @NotNull Data data);
}

public class Data {
  @ValidString
  private String s;
  // get,set methods 
}

I'm using the bean validation framework via @ValidString to delegate the validation to the ESAPI library.

@Override
public boolean isValid (String value, ConstraintValidatorContext context) {
  return ESAPI.validator().isValidInput(
      "String validation",
      value,
      this.constraint.type(),
      this.constraint.maxLength(),
      this.constraint.nullable(),
      true);
}

This method canonicalizes the value (i.e. removes encryption) and then validates against a regular expression provided in the ESAPI config. The regex is not that important to the question, but it mostly whitelists 'safe' characters.

All good so far. However, in a few occasions, I need to accept 'less' safe characters like %, ", <, >, etc. because the incoming text is from an end user's free text input field.

Is there a known pattern for this kind of String sanitization? What kind of text can cause server-side problems if SQL queries are considered safe (e.g. using bind variables)? What if the user wants to store <script>alert("Hello")</script> as his description which at some point will be send back to the client? Do I store that in the DB? Is that a client-side concern?

SBurris

When dealing with text coming from the user, best practice is to white list only known character sets as you stated. But that is not the whole solution, since there are times when that will not work, again as you pointed out sometimes "dangerous" characters are part of the valid character set.

When this happens you need to be very vigilant in how you handle the data. I, as well as the commenters, recommended is to keep the original data from the user in its original state as long as possible. Dealing with the data securely will be to use proper functions for the target domain/output.

SQL

When putting free format strings into a SQL database, best practice is to use prepared statements (in java this is the PreparedStatement object or using ORM that will automatically parameterizes the data.

To read more on SQL injection attacks and other forms of Injection attacks (XML, LDAP, etc.) I recommended OWASPS Top 10 - A1 Injections

XSS

You also mentioned what to do when outputting this data to client. In this case I you want to make sure you html encode the output for the proper context, aka contextual output encoding. ESAPI has Encoder Class/Interface for this. The important thing to note is which context (HTML Body, HTML Attribute, JavaScript, URL, etc.) will the data be outputted. Each area is going to be encoded differently.

Take for example the input: <script>alert('Hello World');<script>

Sample Encoding Outputs:

  • HTML: &lt;script&gt;alert(&#39;Hello World&#39;);&lt;script&gt;
  • JavaScript: \u003cscript\u003ealert(\u0027Hello World\u0027);\u003cscript\u003e URL: %3Cscript%3Ealert%28%27Hello%20World%27%29%3B%3Cscript%3E
  • Form URL: %3Cscript%3Ealert%28%27Hello+World%27%29%3B%3Cscript%3E
  • CSS: \00003Cscript\00003Ealert\000028\000027Hello\000020World\000027\000029\00003B\00003Cscript\00003E
  • XML: &lt;script&gt;alert(&apos;Hello World&apos;);&lt;script&gt;

For more reading on XSS look at OWASP Top 10 - A3 Cross-Site Scripting (XSS)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to safely update hibernate entity?

From Dev

jQuery mobile - What is the best way save user input?

From Dev

jQuery mobile - What is the best way save user input?

From Dev

Best way to accept permanent input from a user?

From Dev

Best way to submit user input on click

From Dev

What's the best way to display data when user input changes in Meteor (datepicker)?

From Dev

What is the best way to set time boundries for user to input data? (asp.net mvc)

From Dev

What is the best way to have date and time input in html (from user) and receive in Javascript?

From Dev

What is the best way to read a file in Java?

From Dev

What is the best way to create text input Redux

From Dev

What is the best way to implement user account activation?

From Dev

What is the best way to store a user feature?

From Dev

What is the best way to store user notifications in MySQL?

From Dev

What is the best way to add a user to the sudoer group?

From Dev

What is the best way to get a user's city?

From Dev

What is the best way to store a user feature?

From Dev

What is the best way to implement user account activation?

From Dev

What is the best way to authenticate user with node and angularjs?

From Dev

What is the best way to authorize the user in Angular 5

From Dev

Safely parsing and evaluating user input

From Dev

What is the difference in these ways to read in user-input?

From Dev

What's the best way to send an input's data to a hidden input?

From Dev

What's the best C++ way to multiply unsigned integers modularly safely?

From Dev

What's the best way to safely remove an external hard drive from Manjaro?

From Dev

What is the proper way to validate user input?

From Dev

Best way to safely clear data in a singleton?

From Dev

Best way to safely call new on this trivial example?

From Dev

Best way to go about sanitizing user input in rails

From Dev

Best way to validate user input URL (see if URL exists)

Related Related

  1. 1

    What is the best way to safely update hibernate entity?

  2. 2

    jQuery mobile - What is the best way save user input?

  3. 3

    jQuery mobile - What is the best way save user input?

  4. 4

    Best way to accept permanent input from a user?

  5. 5

    Best way to submit user input on click

  6. 6

    What's the best way to display data when user input changes in Meteor (datepicker)?

  7. 7

    What is the best way to set time boundries for user to input data? (asp.net mvc)

  8. 8

    What is the best way to have date and time input in html (from user) and receive in Javascript?

  9. 9

    What is the best way to read a file in Java?

  10. 10

    What is the best way to create text input Redux

  11. 11

    What is the best way to implement user account activation?

  12. 12

    What is the best way to store a user feature?

  13. 13

    What is the best way to store user notifications in MySQL?

  14. 14

    What is the best way to add a user to the sudoer group?

  15. 15

    What is the best way to get a user's city?

  16. 16

    What is the best way to store a user feature?

  17. 17

    What is the best way to implement user account activation?

  18. 18

    What is the best way to authenticate user with node and angularjs?

  19. 19

    What is the best way to authorize the user in Angular 5

  20. 20

    Safely parsing and evaluating user input

  21. 21

    What is the difference in these ways to read in user-input?

  22. 22

    What's the best way to send an input's data to a hidden input?

  23. 23

    What's the best C++ way to multiply unsigned integers modularly safely?

  24. 24

    What's the best way to safely remove an external hard drive from Manjaro?

  25. 25

    What is the proper way to validate user input?

  26. 26

    Best way to safely clear data in a singleton?

  27. 27

    Best way to safely call new on this trivial example?

  28. 28

    Best way to go about sanitizing user input in rails

  29. 29

    Best way to validate user input URL (see if URL exists)

HotTag

Archive