Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-ASCII Header Encoding Issues #891

Closed
JakeWharton opened this issue Jun 3, 2014 · 12 comments
Closed

Non-ASCII Header Encoding Issues #891

JakeWharton opened this issue Jun 3, 2014 · 12 comments
Labels
bug Bug in existing code
Milestone

Comments

@JakeWharton
Copy link
Member

When passing headers parameters containing a 'é' it is received on server side as 'é'.
It might seem wird to pass such chars as arguments but I can't modify server side.
I know this behaviour is specific to retrofit because it worked with the old client using HttpUrlConnection and it also works with Postman chrome plugin.

via @cmorin6 from square/retrofit#516

@swankjesse swankjesse added the bug label Jun 8, 2014
@swankjesse swankjesse added this to the 2.1 milestone Jun 8, 2014
@swankjesse
Copy link
Member

The spec wants ISO-8859-1 for header values.

   Historically, HTTP has allowed field content with text in the
   ISO-8859-1 charset [ISO-8859-1], supporting other charsets only
   through use of [RFC2047] encoding.  In practice, most HTTP header
   field values use only a subset of the US-ASCII charset [USASCII].
   Newly defined header fields SHOULD limit their field values to
   US-ASCII octets.  A recipient SHOULD treat other octets in field
   content (obs-text) as opaque data.

@JakeWharton
Copy link
Member Author

So is this just a documentation issue then? My primary reasoning for propagating the issue was that from my testing Apache supports it. The original poster also claims HttpUrlConnection support, which I didn't see.

@swankjesse
Copy link
Member

I think we should do ISO-8859-1, but servers that rely on that behavior shouldn't be.

@swankjesse swankjesse modified the milestones: 2.2, 2.1 Nov 5, 2014
@swankjesse
Copy link
Member

Punting to the icebox until somebody complains!

@yyjdelete
Copy link

Android use the below method in com.android.internal.os.RuntimeInit to set default UA, and okhttp-urlconnection direct use the UA as default http head without encode it.

    /**
     * Returns an HTTP user agent of the form
     * "Dalvik/1.1.0 (Linux; U; Android Eclair Build/MASTER)".
     */
    private static String getDefaultUserAgent() {
        StringBuilder result = new StringBuilder(64);
        result.append("Dalvik/");
        result.append(System.getProperty("java.vm.version")); // such as 1.1.0
        result.append(" (Linux; U; Android ");

        String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
        result.append(version.length() > 0 ? version : "1.0");

        // add the model for the release build
        if ("REL".equals(Build.VERSION.CODENAME)) {
            String model = Build.MODEL;
            if (model.length() > 0) {
                result.append("; ");
                result.append(model);
            }
        }
        String id = Build.ID; // "MASTER" or "M4-rc20"
        if (id.length() > 0) {
            result.append(" Build/");
            result.append(id);
        }
        result.append(")");
        return result.toString();
    }

As an result, when I use okhttp-urlconnection in my apk, my server recevied the below request from an device with non-ASCII MODEL, which is invalid.

Model with non-ASCII char

@yyjdelete
Copy link

@swankjesse

@swankjesse
Copy link
Member

Yep, we should sanitize the User-Agent.

@swankjesse swankjesse modified the milestones: 2.5, Icebox Jul 27, 2015
@swankjesse
Copy link
Member

Working plan: throw an exception on non-ASCII headers in Headers.Builder.

@vvelmurugan
Copy link

@swankjesse Why the add method is not throwing illegalArgumentException, for us developers to catch and handle the error and mostly importantly to know that this method will throw this exception. Not many will look into the source and find that the header values are sanitised strictly.

@swankjesse
Copy link
Member

@vvelmurugan which header aren’t we being strict enough on? Most code paths that add headers are strict!

@vvelmurugan
Copy link

vvelmurugan commented Jul 13, 2020

@swankjesse I think I am not clear with my question. My question is
Current method is like this
private void checkNameAndValue(String name, String value)
but I feel it should be like this
private void checkNameAndValue(String name, String value) throws illegalArgumentException
So ,
Add method will look this
public Builder add(String name, String value) throws illegalArgumentException

if it is like this then our lint itself will suggest to handle that exception and we will look into okhttp code on why this exception is thrown. Currently many devs who come from HttpUrlConnection will not know that you sanitise headers for non-ascii characters

@swankjesse
Copy link
Member

@vvelmurugan ahhhh. Can you open a new issue requesting better documentation on the restrictions upon headers? Also please tell us which docs you read before running into this issue so we have a good idea as to where to document it!

PruthiviRaj27 added a commit to testpress/android-sdk that referenced this issue Aug 14, 2023
)

- We include the application name in the User-Agent request header to
identify the app usage. If the app name includes special characters, it
leads to a crash because the header value should not contain non-ASCII
values. square/okhttp#891
- In this commit, we have passed the package name to the User-Agent
instead of the App name because the package name is also unique.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

4 participants