/usr/share/perl5/Template/Plugin/JSON/Escape.pm is in libtemplate-plugin-json-escape-perl 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | package Template::Plugin::JSON::Escape;
use strict;
use warnings;
use base qw/Template::Plugin/;
use JSON ();
our $VERSION = 0.02;
sub new {
my ($class, $context, $args) = @_;
my $self = bless { json => undef, args => $args }, $class;
my $encode = sub { $self->json_encode( @_ ) };
$context->define_vmethod( $_ => json => $encode ) for qw/hash list scalar/;
$context->define_filter( json => \&json_filter );
return $self;
}
sub json {
my $self = shift;
return $self->{json} if $self->{json};
my $json = JSON->new->allow_nonref;
my $args = $self->{args};
for ( keys %$args ) {
$json->$_( $args->{ $_ } ) if $json->can( $_ );
}
return $self->{json} = $json;
}
sub json_encode {
my ($self, $value) = @_;
json_filter( $self->json->encode( $value ) );
}
sub json_decode {
my ($self, $value) = @_;
$self->json->decode( $value );
}
sub json_filter {
my $value = shift;
$value =~ s!&!\\u0026!g;
$value =~ s!<!\\u003c!g;
$value =~ s!>!\\u003e!g;
$value =~ s!\+!\\u002b!g;
$value =~ s!\x{2028}!\\u2028!g;
$value =~ s!\x{2029}!\\u2029!g;
$value;
}
1;
__END__
=pod
=head1 NAME
Template::Plugin::JSON::Escape - Adds a .json vmethod and a json filter.
=head1 SYNOPSIS
[% USE JSON.Escape( pretty => 1 ) %];
<script type="text/javascript">
var foo = [% foo.json %];
var bar = [% json_string | json %]
</script>
or read in JSON
[% USE JSON.Escape %]
[% data = JSON.Escape.json_decode(json) %]
[% data.thing %]
=head1 DESCRIPTION
This plugin allows you to embed JSON strings in HTML. In the output, special characters such as C<E<lt>> and C<&> are escaped as C<\uxxxx> to prevent XSS attacks.
It also provides decoding function to keep compatibility with L<Template::Plugin::JSON>.
=head1 FEATURES
=head2 USE JSON.Escape
Any options on the USE line are passed through to the JSON object, much like L<JSON/to_json>.
=head2 json vmethod
A C<.json> vmethod converts scalars, arrays and hashes into corresponding JSON strings.
[% json_stuct = { foo => 42, bar => [ 1, 2, 3 ] } %]
<script>
var json = [% json_struct.json %];
</script>
<span onclick="doSomething([% json_struct.json %]);">
=head2 json filter
A C<json> filter escapes C<E<lt>>, C<E<gt>>, C<&>, C<+>, C<U+2028> and C<U+2029> as C<\uxxxx>. In the attribute, you may just use an C<html> filter.
[% json_string = '{ "foo": 42, "bar": [ 1, 2, 3 ] }' %]
<script>
var json = [% json_string | json %];
</script>
<span onclick="doSomething([% json_string | html %]);">
=head2 json_decode method
A C<json_decode> method allow you to convert from a JSON string into a corresponding data structure.
[% SET json_struct = JSON.Escape.json_decode(json_string) %]
[% json_struct.foo | html %]
=head1 SEE ALSO
L<Template::Plugin::JSON>, L<JSON>, L<Template::Plugin>
=head1 VERSION CONTROL
L<https://github.com/nanto/perl-Template-Plugin-JSON-Escape>
=head1 AUTHOR
nanto_vi (TOYAMA Nao) <nanto@moon.email.ne.jp>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2011 nanto_vi (TOYAMA Nao).
Copyright (c) 2006, 2008 Infinity Interactive, Yuval Kogman.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
=cut
|