Skip to content

User.toJson builds JSON without escaping, special-char passwords break it #15

Description

@CanReader

User.toJson() builds JSON by string concatenation without escaping any of the values:

public String toJson() {
    String strTemp = "\"%s\"";
    return "{" +
        "\"Id\":" + id + "," +
        "\"Username\":" + String.format(strTemp, getUsername()) + "," +
        "\"Password\":" + String.format(strTemp, getPassword()) + "," +
        "\"Email\":" + String.format(strTemp, getEmail()) + "," +
        ...
}

It just wraps each value in quotes. If any field contains a double-quote, backslash, or newline, the produced JSON is broken. Username is charset-limited so it's safe, but Password is not, passwords are allowed to contain punctuation (only "at least one letter and one number" is enforced). So a user whose password contains a " or \ produces malformed JSON here.

Wherever toJson() feeds a request (registration is the obvious one), that means users with those characters in their password get a broken payload and can't register/login through this path, and in the worst case the unescaped value could inject extra JSON fields.

This should use a real JSON builder (org.json's JSONObject, which the codebase already uses elsewhere) instead of hand-concatenating, so values get escaped properly. Bonus: it'd stop serializing the plaintext Password/Email into a string that can end up in logs.

File: core/src/com/focus/kingdom/network/dto/User.java, toJson, around line 370-386.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions