| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More; |
| 4 |
|
| 5 |
# Option-list labels must be HTML-escaped when rendered. |
| 6 |
# |
| 7 |
# The option list is the part of a form that is routinely built from data |
| 8 |
# (options_from, an options_<field> method, the DBIC model) rather than written |
| 9 |
# as literals, so anything in it may carry text the application did not author. |
| 10 |
# |
| 11 |
# These assertions are deliberately plain string matches rather than is_html(). |
| 12 |
# is_html() compares by parsing both sides with HTML::TreeBuilder and comparing |
| 13 |
# as_XML, which re-escapes as it serialises: for a payload like `&` a raw and an |
| 14 |
# escaped rendering normalise to the same string, so the comparison cannot see |
| 15 |
# the difference this test exists to catch. |
| 16 |
# |
| 17 |
# Note also that t/render/escaping.t already covers a radio group, but through |
| 18 |
# Render::Simple::render_radio_group, which applies html_filter. The widget path |
| 19 |
# exercised here is a different one. |
| 20 |
|
| 21 |
{ |
| 22 |
package Test::Form; |
| 23 |
use HTML::FormHandler::Moose; |
| 24 |
extends 'HTML::FormHandler'; |
| 25 |
|
| 26 |
has_field 'sel' => ( type => 'Select', widget => 'Select' ); |
| 27 |
has_field 'radio' => ( type => 'Select', widget => 'RadioGroup' ); |
| 28 |
has_field 'cbg' => ( type => 'Multiple', widget => 'CheckboxGroup' ); |
| 29 |
has_field 'hcbg' => ( type => 'Multiple', widget => 'HorizCheckboxGroup' ); |
| 30 |
|
| 31 |
# The same unsafe text in a group label and an option label, plus a benign |
| 32 |
# option so the escaping assertions cannot pass by nothing being rendered. |
| 33 |
sub _opts { |
| 34 |
return ( |
| 35 |
{ group => q{"<b>'group'</b>"}, |
| 36 |
options => [ |
| 37 |
{ value => 1, label => q{"<b>'option'</b>"} }, |
| 38 |
{ value => 2, label => 'plain' }, |
| 39 |
], |
| 40 |
}, |
| 41 |
); |
| 42 |
} |
| 43 |
sub options_sel { _opts() } |
| 44 |
sub options_radio { _opts() } |
| 45 |
sub options_cbg { _opts() } |
| 46 |
sub options_hcbg { _opts() } |
| 47 |
} |
| 48 |
|
| 49 |
my $form = Test::Form->new; |
| 50 |
$form->process( params => {} ); |
| 51 |
|
| 52 |
my %html = map {; $_ => $form->field($_)->render } qw( sel radio cbg hcbg ); |
| 53 |
my $group_label_html_attribute_context_encoded = q{"<b>'group'</b>"}; |
| 54 |
my $group_label_html_text_context_encoded = q{"<b>'group'</b>"}; |
| 55 |
my $option_label_html_text_context_encoded = q{"<b>'option'</b>"}; |
| 56 |
|
| 57 |
for my $name ( sort keys %html ) { |
| 58 |
my $out = $html{$name}; |
| 59 |
|
| 60 |
# The group label must not reach the output as markup. |
| 61 |
# In Select it lands inside label="..." (HTML attribute context) |
| 62 |
# in the others in element content (HTML text context). |
| 63 |
# Neither may carry a live tag. |
| 64 |
unlike $out, qr/"<b>'group'<\/b>"/, |
| 65 |
"$name: group label is never emitted as raw markup"; |
| 66 |
if ($name eq 'sel') { |
| 67 |
like $out, qr/\Q$group_label_html_attribute_context_encoded/, |
| 68 |
"$name: group label is encoded in HTML attribute context"; |
| 69 |
} |
| 70 |
else { |
| 71 |
like $out, qr/\Q$group_label_html_text_context_encoded/, |
| 72 |
"$name: group label is encoded in HTML text context"; |
| 73 |
} |
| 74 |
|
| 75 |
# Same for the option label |
| 76 |
unlike $out, qr/"<b>'option'<\/b>"/, |
| 77 |
"$name: option label is never emitted as raw markup"; |
| 78 |
like $out, qr/\Q$option_label_html_text_context_encoded/, |
| 79 |
"$name: option label is encoded in HTML text context"; |
| 80 |
|
| 81 |
# Behaviour the fix must not break: a label needing no escaping is |
| 82 |
# unchanged, so the fix cannot pass by mangling or dropping labels. |
| 83 |
like $out, qr/\bplain\b/, |
| 84 |
"$name: a benign label still renders intact"; |
| 85 |
} |
| 86 |
|
| 87 |
done_testing; |