Security Advisories (10)
CPANSA-Mojolicious-2022-03 (2022-12-10)

Mojo::DOM did not correctly parse <script> tags.

CPANSA-Mojolicious-2021-02 (2021-06-01)

Small sessions could be used as part of a brute-force attack to decode the session secret.

CVE-2021-47208 (2021-03-16)

A bug in format detection can potentially be exploited for a DoS attack.

CVE-2018-25100 (2018-02-13)

Mojo::UserAgent::CookieJar leaks old cookies because of the missing host_only flag on empty domain.

CPANSA-Mojolicious-2015-01 (2015-02-02)

Directory traversal on Windows

CPANSA-Mojolicious-2018-03 (2018-05-19)

Mojo::UserAgent was not checking peer SSL certificates by default.

CVE-2020-36829 (2020-11-10)

Mojo::Util secure_compare can leak the string length. By immediately returning when the two strings are not the same length, the function allows an attacker to guess the length of the secret string using timing attacks.

CPANSA-Mojolicious-2018-02 (2018-05-11)

GET requests with embedded backslashes can be used to access local files on Windows hosts

CPANSA-Mojolicious-2014-01 (2014-10-07)

Context sensitivity of method param could lead to parameter injection attacks.

CVE-2024-58134 (2025-05-03)

Mojolicious versions from 0.999922 for Perl uses a hard coded string, or the application's class name, as a HMAC session secret by default. These predictable default secrets can be exploited to forge session cookies. An attacker who knows or guesses the secret could compute valid HMAC signatures for the session cookie, allowing them to tamper with or hijack another user's session.

NAME

Mojo::Message - HTTP message base class

SYNOPSIS

package Mojo::Message::MyMessage;
use Mojo::Base 'Mojo::Message';

sub cookies              {...}
sub extract_start_line   {...}
sub get_start_line_chunk {...}

DESCRIPTION

Mojo::Message is an abstract base class for HTTP messages as described in RFC 2616 and RFC 2388.

EVENTS

Mojo::Message inherits all events from Mojo::EventEmitter and can emit the following new ones.

finish

$msg->on(finish => sub {
  my $msg = shift;
  ...
});

Emitted after message building or parsing is finished.

my $before = time;
$msg->on(finish => sub {
  my $msg = shift;
  $msg->headers->header('X-Parser-Time' => time - $before);
});

progress

$msg->on(progress => sub {
  my $msg = shift;
  ...
});

Emitted when message building or parsing makes progress.

# Building
$msg->on(progress => sub {
  my ($msg, $state, $offset) = @_;
  say qq{Building "$state" at offset $offset};
});

# Parsing
$msg->on(progress => sub {
  my $msg = shift;
  return unless my $len = $msg->headers->content_length;
  my $size = $msg->content->progress;
  say 'Progress: ', $size == $len ? 100 : int($size / ($len / 100)), '%';
});

ATTRIBUTES

Mojo::Message implements the following attributes.

content

my $msg = $msg->content;
$msg    = $msg->content(Mojo::Content::Single->new);

Message content, defaults to a Mojo::Content::Single object.

default_charset

my $charset = $msg->default_charset;
$msg        = $msg->default_charset('UTF-8');

Default charset used for form-data parsing, defaults to UTF-8.

max_line_size

my $size = $msg->max_line_size;
$msg     = $msg->max_line_size(1024);

Maximum start line size in bytes, defaults to the value of the MOJO_MAX_LINE_SIZE environment variable or 10240.

max_message_size

my $size = $msg->max_message_size;
$msg     = $msg->max_message_size(1024);

Maximum message size in bytes, defaults to the value of the MOJO_MAX_MESSAGE_SIZE environment variable or 10485760. Note that increasing this value can also drastically increase memory usage, should you for example attempt to parse an excessively large message body with the body_params, dom or json methods.

version

my $version = $msg->version;
$msg        = $msg->version('1.1');

HTTP version of message, defaults to 1.1.

METHODS

Mojo::Message inherits all methods from Mojo::EventEmitter and implements the following new ones.

body

my $bytes = $msg->body;
$msg      = $msg->body('Hello!');

Slurp or replace content.

body_params

my $params = $msg->body_params;

POST parameters extracted from application/x-www-form-urlencoded or multipart/form-data message body, usually a Mojo::Parameters object. Note that this method caches all data, so it should not be called before the entire message body has been received. Parts of the message body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large.

# Get POST parameter value
say $msg->body_params->param('foo');

body_size

my $size = $msg->body_size;

Content size in bytes.

build_body

my $bytes = $msg->build_body;

Render whole body.

build_headers

my $bytes = $msg->build_headers;

Render all headers.

build_start_line

my $bytes = $msg->build_start_line;

Render start line.

my $cookie  = $msg->cookie('foo');
my @cookies = $msg->cookie('foo');

Access message cookies, usually Mojo::Cookie::Request or Mojo::Cookie::Response objects. Note that this method caches all data, so it should not be called before all headers have been received.

# Get cookie value
say $msg->cookie('foo')->value;

cookies

my $cookies = $msg->cookies;

Access message cookies. Meant to be overloaded in a subclass.

dom

my $dom        = $msg->dom;
my $collection = $msg->dom('a[href]');

Turns message body into a Mojo::DOM object and takes an optional selector to perform a find on it right away, which returns a Mojo::Collection object. Note that this method caches all data, so it should not be called before the entire message body has been received. The whole message body needs to be loaded into memory to parse it, so you have to make sure it is not excessively large.

# Perform "find" right away
say $msg->dom('h1, h2, h3')->pluck('text');

# Use everything else Mojo::DOM has to offer
say $msg->dom->at('title')->text;
say $msg->dom->html->body->children->pluck('type')->uniq;

error

my $err          = $msg->error;
my ($err, $code) = $msg->error;
$msg             = $msg->error('Parser error');
$msg             = $msg->error('Parser error', 500);

Error and code.

extract_start_line

my $success = $msg->extract_start_line(\$str);

Extract start line from string. Meant to be overloaded in a subclass.

finish

$msg = $msg->finish;

Finish message parser/generator.

fix_headers

$msg = $msg->fix_headers;

Make sure message has all required headers.

get_body_chunk

my $bytes = $msg->get_body_chunk($offset);

Get a chunk of body data starting from a specific position.

get_header_chunk

my $bytes = $msg->get_header_chunk($offset);

Get a chunk of header data, starting from a specific position.

get_start_line_chunk

my $bytes = $msg->get_start_line_chunk($offset);

Get a chunk of start line data starting from a specific position. Meant to be overloaded in a subclass.

header_size

my $size = $msg->header_size;

Size of headers in bytes.

headers

my $headers = $msg->headers;

Message headers, usually a Mojo::Headers object.

is_finished

my $success = $msg->is_finished;

Check if message parser/generator is finished.

is_limit_exceeded

my $success = $msg->is_limit_exceeded;

Check if message has exceeded max_line_size or max_message_size.

json

my $hash  = $msg->json;
my $array = $msg->json;
my $value = $msg->json('/foo/bar');

Decode JSON message body directly using Mojo::JSON if possible, returns undef otherwise. An optional JSON Pointer can be used to extract a specific value with Mojo::JSON::Pointer. Note that this method caches all data, so it should not be called before the entire message body has been received. The whole message body needs to be loaded into memory to parse it, so you have to make sure it is not excessively large.

# Extract JSON values
say $msg->json->{foo}{bar}[23];
say $msg->json('/foo/bar/23');

param

my @names = $msg->param;
my $foo   = $msg->param('foo');
my @foo   = $msg->param('foo');

Access POST parameters. Note that this method caches all data, so it should not be called before the entire message body has been received. Parts of the message body need to be loaded into memory to parse POST parameters, so you have to make sure it is not excessively large.

parse

$msg = $msg->parse('HTTP/1.1 200 OK...');

Parse message chunk.

start_line_size

my $size = $msg->start_line_size;

Size of the start line in bytes.

to_string

my $str = $msg->to_string;

Render whole message.

upload

my $upload  = $msg->upload('foo');
my @uploads = $msg->upload('foo');

Access multipart/form-data file uploads, usually Mojo::Upload objects. Note that this method caches all data, so it should not be called before the entire message body has been received.

# Get content of uploaded file
say $msg->upload('foo')->asset->slurp;

uploads

my $uploads = $msg->uploads;

All multipart/form-data file uploads, usually Mojo::Upload objects.

SEE ALSO

Mojolicious, Mojolicious::Guides, http://mojolicio.us.