Session/Serialize/JSON.pm

filter: Statementstmt 100% 40 / 40 Branchbran 90.0% 9 / 10 Conditioncond 87.5% 7 / 8 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 4 totaltotal 90.4% 66 / 73 CC 9 SCAR 14.0
CC
9
Cov
97%
_unserialize
7.0
unserializeLatin1
2.0
unserialize
2.0
CRAP 4.1
« Session/_common.pm ↑ Summary Session/Store/File.pm »
1 # Directly copied from Lemonldap::NG project (http://lemonldap-ng.org/)
2 package Web::Session::Serialize::JSON;
3  
4 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 35 our $VERSION = '1.0.0';
8  
9 35 my $json = JSON->new->allow_nonref;
10 35 my $utf8Json = JSON->new->utf8->allow_nonref;
11  
12 # Writers produce canonical JSON (keys sorted): Perl randomizes the order of
13 # hash keys (per process and per hash), so the same data would otherwise be
14 # serialized differently from one save to the next. SQL stores compare the
15 # serialized data with the one they read to skip the write of an unchanged
16 # session (see Web::Session::Store::DBI)
17 35 my $canonicalJson = JSON->new->canonical->allow_nonref;
18 35 my $latin1Json = JSON->new->canonical->latin1->allow_nonref;
19  
20 352 ▶sub serialize {
Subroutine serialize: called CC=1 SCAR=0.0
Pod undocumented
21 352 my $session = shift;
22  
23 352 $session->{serialized} = $canonicalJson->encode( $session->{data} );
24  
25 # JSON::PP doesn't flag its result as UTF-8 when data holds only Latin-1
26 # characters: some DBD drivers would then store Latin-1 bytes
27 352 utf8::upgrade( $session->{serialized} );
28 352 return $session->{serialized};
29 }
30  
31 # For stores that can only hold Latin-1 bytes (Redis): characters above U+00FF
32 # are written as \uXXXX escapes, so the result can always be downgraded
33 5 ▶sub serializeLatin1 {
Subroutine serializeLatin1: called CC=1 SCAR=0.0
Pod undocumented
34 5 my $session = shift;
35  
36 5 $session->{serialized} = $latin1Json->encode( $session->{data} );
37 5 return $session->{serialized};
38 }
39  
40 552 ▶sub unserialize {
Subroutine unserialize: called CC=2 SCAR=6.9
Pod undocumented
41 552 my ( $session, $next ) = @_;
42  
43 552 my $data = _unserialize( $session->{serialized}, $next );
44 551 ▶ die "Session could not be unserialized" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 1 550 551
45 550 $session->{data} = $data;
46 }
47  
48 # For stores that never return UTF-8 bytes (Redis stores Latin-1)
49 3 ▶sub unserializeLatin1 {
Subroutine unserializeLatin1: called CC=2 SCAR=7.0
Pod undocumented
50 3 my ( $session, $next ) = @_;
51  
52 3 my $data = _unserialize( $session->{serialized}, $next, 1 );
53 3 ▶ die "Session could not be unserialized" unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 3 3
54 3 $session->{data} = $data;
55 }
56  
57 918 ▶sub _unserialize {
Subroutine _unserialize: called CC=7 SCAR=19.5
58 918 my ( $serialized, $next, $latin1 ) = @_;
59 918 my $tmp;
60  
61 # Some stores (MySQL json columns, files,...) return UTF-8 bytes
62 918 ▶ if ( defined $serialized and !$latin1 and !utf8::is_utf8($serialized) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $serialized and not $latin1 and not utf8::is_utf8($serialized)) 488 430 918
Condition83%5/6
defined $serialized and not $latin1 and not utf8::is_utf8($serialized)
!ll&&!rl&&r
3427488
defined $serialized and not $latin1
!ll&&!rl&&r
03915
MC/DC66%2/3
defined $serialized and not $latin1 and not utf8::is_utf8($serialized)
defined $serialized not $latin1 not utf8::is_utf8($serialized)
Truth table75%3/4
A and not(B) and not(C)
A= defined $serializedB= not $latin1C= not utf8::is_utf8($serialized)
ABCresult
0XX0
10X0
1100
1111
63 488 $tmp = eval { $utf8Json->decode($serialized) };
64 488 ▶ return $tmp unless ($@);
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 478 10 488
65 }
66 440 eval { $tmp = $json->decode($serialized) };
67 440 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 7 433 440
68 7 require Storable;
69 7 ▶ $next ||= \&Storable::thaw;
Condition100%2/2
$next ||= \&Storable::thaw
l!l
34
MC/DC100%1/1
$next ||= \&Storable::thaw
$next
Truth table100%2/2
A
A= $next
Aresult
11
00
70 7 return &$next($serialized);
71 }
72 433 return $tmp;
73 }
74  
75 35 1;
76  
77 =pod
78  
79 =head1 NAME
80  
81 =encoding utf8
82  
83 Web::Session::Serialize::JSON - Use JSON to zip up data
84  
85 =head1 SYNOPSIS
86  
87 use Web::Session::Serialize::JSON;
88  
89 $zipped = Web::Session::Serialize::JSON::serialize($ref);
90 $ref = Web::Session::Serialize::JSON::unserialize($zipped);
91  
92 =head1 DESCRIPTION
93  
94 This module fulfills the serialization interface of Apache::Session.
95 It serializes the data in the session object by use of L<JSON>. The
96 serialized data is UTF-8 text.
97  
98 The output is canonical: object keys are sorted, so the same data always gives
99 the same string, whatever the order in which Perl stores the hash keys. SQL
100 backends rely on it to skip the rewrite of an unchanged session (see
101 L<Web::Session/"UNCHANGED SESSIONS ARE NOT REWRITTEN">).
102  
103 When reading, a string returned as bytes by the store (not flagged as UTF-8,
104 for example a MySQL C<json> column) is first decoded as UTF-8 JSON; if it is
105 not valid UTF-8, it is read as characters.
106  
107 Some stores return Latin-1 bytes instead: SQLite without C<sqlite_unicode>,
108 and MySQL C<json> or C<text> columns holding data written by an old node
109 using JSON::PP. A Latin-1 session that is also valid UTF-8 is then read as
110 UTF-8: for example "é" becomes "é". This needs every non-ASCII character of
111 the session to be part of such a sequence, that is bytes C2-DF followed by
112 80-BF, or a valid UTF-8 sequence formed over the whole session (mostly values
113 that are already mojibake).
114 How strictly "valid UTF-8" is checked depends on the JSON backend: JSON::XS
115 accepts surrogates, code points above U+10FFFF and 5-byte sequences, unlike
116 JSON::PP and Cpanel::JSON::XS. The rare Latin-1 byte strings misread as UTF-8
117 therefore vary slightly between backends.
118  
119 File and Redis are not affected: Redis stores Latin-1 but reads it with
120 C<unserializeLatin1()>, which skips the UTF-8 decode, and
121 L<Web::Session::Store::File> always writes UTF-8. Files written
122 by an older version keep their ambiguity: Latin-1 bytes that also form a valid
123 UTF-8 sequence can't be told apart from UTF-8 ones.
124  
125 =head1 UPGRADE
126  
127 Previous versions read UTF-8 bytes returned by the store as Latin-1. With
128 JSON::XS the serialized data is unchanged, so they still read sessions as
129 they did before. With JSON::PP the added C<utf8::upgrade()> changes the
130 bytes stored by the DBD-based backends (a Latin-1 string becomes UTF-8), so
131 an old node reading a session written by a new one sees mojibake. Files are
132 affected the same way: they are now always written as UTF-8, where older
133 versions stored Latin-1 bytes for a session holding only Latin-1 characters.
134 A session
135 rewritten by a previous version during a rolling upgrade keeps the
136 double-encoded values it read, and sessions already corrupted this way are
137 not repaired. Upgrade all servers sharing sessions (for Lemonldap::NG, all
138 portals and handlers) together.
139  
140 Redis clients refuse characters above U+00FF. Redis therefore writes sessions
141 with C<serializeLatin1()>: characters above U+00FF (surrogate pairs above
142 U+FFFF) are escaped as C<\uXXXX> and the others are kept, so the result can
143 always be downgraded to Latin-1 bytes. For a session holding only Latin-1
144 characters, it is the same string as C<serialize()>. C<unserialize()> reads
145 the escapes back as the original characters.
146  
147 =head1 SEE ALSO
148  
149 L<JSON>, L<Apache::Session>
150  
151 =head1 BUG REPORT
152  
153 Use OW2 system to report bug or ask for features:
154 L<http://jira.ow2.org>
155  
156 =head1 COPYRIGHT AND LICENSE
157  
158 =over
159  
160 =item 2009-2026 by Xavier Guimard
161  
162 =item 2013-2025 by Clément Oudot
163  
164 =item 2019-2025 by Maxime Besson
165  
166 =item 2013-2025 by Worteks
167  
168 =item 2023-2026 by Linagora
169  
170 =back
171  
172 This library is free software; you can redistribute it and/or modify
173 it under the terms of the GNU General Public License as published by
174 the Free Software Foundation; either version 2, or (at your option)
175 any later version.
176  
177 This program is distributed in the hope that it will be useful,
178 but WITHOUT ANY WARRANTY; without even the implied warranty of
179 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
180 GNU General Public License for more details.
181  
182 You should have received a copy of the GNU General Public License
183 along with this program. If not, see L<http://www.gnu.org/licenses/>.
184  
185 =cut
« Session/_common.pm ↑ Summary Session/Store/File.pm »