Allow opting out of the default charset being appended to the request `Content-Type` header

Ouverte Adaptée aux débutants
#1,729 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
72/100
Type d'issue
Fonctionnalité
Clarté
Plutôt claire
Activité
Active
Stack technique
java
Domaine
api, backend

Piste de recherche

Commence par AwsProxyHttpServletRequestReader.java et ContainerConfig.java pour suivre la manière dont le jeu de caractères par défaut est ajouté et configuré. Lis ou exécute ensuite AwsProxyHttpServletRequestReaderTest ; ajoute des tests couvrant la désactivation, le comportement par défaut inchangé et la présence d’un paramètre charset=. Le travail est terminé lorsque la désactivation laisse le Content-Type entrant inchangé, tandis que le comportement par défaut reste intact.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

  • Framework version: 3.0.2 (also confirmed identical in 3.0.3 and on main)
  • Implementations: All (core AwsProxyHttpServletRequestReader)

Scenario

AwsProxyHttpServletRequestReader.readRequest(...) rewrites the incoming Content-Type header on the
proxy event before the servlet request is built (and so before any filter runs):

// AwsProxyHttpServletRequestReader.java#L55
request.getMultiValueHeaders().putSingle(HttpHeaders.CONTENT_TYPE, getContentTypeWithCharset(contentType, config));

If the client sent a Content-Type without a charset parameter, getContentTypeWithCharset appends
"; charset=" + config.getDefaultContentCharset(), which defaults to ISO-8859-1:

This breaks any application that has to see the request headers exactly as the client sent them.
The case I hit is request-signature verification: the client signs a set of request components that
includes Content-Type (e.g. an HTTP message signature, a proof-of-possession token whose claims
hash the request headers, or an HMAC scheme). A servlet filter then recomputes the signature input
from HttpServletRequest. Because the container has already changed application/json to
application/json; charset=ISO-8859-1, the recomputed hash never matches and every request that
omits a charset fails verification.

There is currently no way to turn this off through ContainerConfig:

  • setDefaultContentCharset(null) produces Content-Type: application/json; charset=null
  • setDefaultContentCharset("") produces Content-Type: application/json; charset=

Both still change the header. The only workarounds are to replace or subclass the request reader, or to
reverse-engineer the exact appended suffix inside the verifier and retry with it stripped. Both
depend on container internals.

(Related background: #175 covered how a forced default charset interacts badly with
application/json, which RFC 8259 defines as UTF-8 and which has no charset parameter.)

Expected behavior

There should be a supported way to make the container leave the client's Content-Type unmodified.
The default behavior can stay as it is.

Actual behavior

Whenever no charset is present, the header is always rewritten, and no configuration value avoids it.

Steps to reproduce

  1. Build an AwsProxyRequest with header Content-Type: application/json (no charset).
  2. Run it through AwsProxyHttpServletRequestReader.readRequest(...). Calling
    setDefaultContentCharset(null) on the ContainerConfig first doesn't change the outcome.
  3. Observe servletRequest.getHeader("Content-Type") returns application/json; charset=ISO-8859-1
    (or ; charset=null), and that the original event's multi-value headers were mutated as well.

Proposed fix

Make the append opt-out-able, default unchanged. Two options, happy to go with whichever the
maintainers prefer:

Option A: treat a null/blank charset as "don't append" (smallest diff, no new API)

private String getContentTypeWithCharset(String headerValue, ContainerConfig config) {
    if (headerValue == null || "".equals(headerValue.trim())) {
        return headerValue;
    }
    if (headerValue.contains("charset=")) {
        return headerValue;
    }
    String charset = config.getDefaultContentCharset();
    if (charset == null || charset.trim().isEmpty()) {
        return headerValue;
    }
    ...
}

Users opt out with config.setDefaultContentCharset(null). Downside: it gives null a special meaning
that isn't obvious from the API, so the setDefaultContentCharset Javadoc would need to document it.
Right now null just produces a malformed charset=null value, so nobody can depend on the current
behavior.

Option B: explicit boolean flag (more readable intent)

Add ContainerConfig#setAppendDefaultContentCharset(boolean) (default true) and check it in
getContentTypeWithCharset. It's a slightly bigger change (field, getter/setter, default init, guard),
but the setting explains itself.

Either way the default (ISO-8859-1 appended) stays the same, so existing users aren't affected. I'm
happy to open a PR with the change plus tests in AwsProxyHttpServletRequestReaderTest. The tests would
cover: no suffix when opted out, the default path still appending, and an existing charset= left as is.

Langage dominant
Java
Étoiles
1.6k
Forks
575
Merge moyen
1 j 15 h
PR mergées (30 j)
12

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de aws/serverless-java-container

Toutes les issues de aws/serverless-java-container

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.