PerlDancer / PerlDancer/Dancer2

UTF-8 encoding is assumed

Open
#1,792 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Perl
Stars
604
Forks
288
Avg merge
1d 5h
Merged PRs (30d)
5

Description

I'm creating a dancer2 request with some Shift-JIS-encoded text in it. These are valid octets, suitable for transmission. When the body is parsed a warning is emitted, because it's not valid UTF-8 -- this shouldn't be assumed.

stack trace:

Invalid UTF-8 in body parameters; leaving bytes unchanged at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 481.
        Dancer2::Core::Request::_invalid_utf8(Dancer2::Core::Request=HASH(0x7afa1d018), "\x{82}\x{e2}\x{82}\x{c1}\x{82}\x{bd}", "body parameters") called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 463
        Dancer2::Core::Request::_decode_bytes(Dancer2::Core::Request=HASH(0x7afa1d018), "\x{82}\x{e2}\x{82}\x{c1}\x{82}\x{bd}", "body parameters") called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 438
        Dancer2::Core::Request::_decode(Dancer2::Core::Request=HASH(0x7afa1d018), "\x{82}\x{e2}\x{82}\x{c1}\x{82}\x{bd}", "body parameters") called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 446
        Dancer2::Core::Request::_decode(Dancer2::Core::Request=HASH(0x7afa1d018), HASH(0x7afa67240), "body parameters") called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 441
        Dancer2::Core::Request::_decode(Dancer2::Core::Request=HASH(0x7afa1d018), Hash::MultiValue=HASH(0x7af8d6990), "body parameters") called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 402
        Dancer2::Core::Request::body_parameters(Dancer2::Core::Request=HASH(0x7afa1d018)) called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 128
        Dancer2::Core::Request::_body_params(Dancer2::Core::Request=HASH(0x7afa1d018)) called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 584
        Dancer2::Core::Request::_build_uploads(Dancer2::Core::Request=HASH(0x7afa1d018)) called at /Volumes/chambord/Users/ether/.perlbrew/libs/43.10@std/lib/perl5/Dancer2/Core/Request.pm line 92
        Dancer2::Core::Request::new("Dancer2::Core::Request", "env", HASH(0x7afed3ea0)) called at t/lib/Helper.pm line 164
...

By the way, this is bad code:

437:        if ( !is_ref($h) && !utf8::is_utf8($h) ) {

as is this:

458:    # If PSGI already gave us characters, avoid re-decoding.
459:    return $bytes if utf8::is_utf8($bytes);

You simply can't use is_utf8 that way. It does NOT convey whether a string is unicode characters vs utf8 bytes. (see perldoc utf8 for references.)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Dancer2/Core/Request.pm around lines 402-481, following _decode, _decode_bytes, and _invalid_utf8. Read perldoc utf8 and reproduce the request with the Shift-JIS octets described in the issue. Done means request body parsing no longer assumes every incoming byte string is UTF-8 or emits the reported warning for valid non-UTF-8 input.

Written by the indexing model from the issue text.

Assessment

Tech stack
perl
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.