use strict; use warnings; use Test::More; # Option-list labels must be HTML-escaped when rendered. # # The option list is the part of a form that is routinely built from data # (options_from, an options_ method, the DBIC model) rather than written # as literals, so anything in it may carry text the application did not author. # # These assertions are deliberately plain string matches rather than is_html(). # is_html() compares by parsing both sides with HTML::TreeBuilder and comparing # as_XML, which re-escapes as it serialises: for a payload like `&` a raw and an # escaped rendering normalise to the same string, so the comparison cannot see # the difference this test exists to catch. # # Note also that t/render/escaping.t already covers a radio group, but through # Render::Simple::render_radio_group, which applies html_filter. The widget path # exercised here is a different one. { package Test::Form; use HTML::FormHandler::Moose; extends 'HTML::FormHandler'; has_field 'sel' => ( type => 'Select', widget => 'Select' ); has_field 'radio' => ( type => 'Select', widget => 'RadioGroup' ); has_field 'cbg' => ( type => 'Multiple', widget => 'CheckboxGroup' ); has_field 'hcbg' => ( type => 'Multiple', widget => 'HorizCheckboxGroup' ); # The same unsafe text in a group label and an option label, plus a benign # option so the escaping assertions cannot pass by nothing being rendered. sub _opts { return ( { group => q{"'group'"}, options => [ { value => 1, label => q{"'option'"} }, { value => 2, label => 'plain' }, ], }, ); } sub options_sel { _opts() } sub options_radio { _opts() } sub options_cbg { _opts() } sub options_hcbg { _opts() } } my $form = Test::Form->new; $form->process( params => {} ); my %html = map {; $_ => $form->field($_)->render } qw( sel radio cbg hcbg ); my $group_label_html_attribute_context_encoded = q{"'group'"}; my $group_label_html_text_context_encoded = q{"<b>'group'</b>"}; my $option_label_html_text_context_encoded = q{"<b>'option'</b>"}; for my $name ( sort keys %html ) { my $out = $html{$name}; # The group label must not reach the output as markup. # In Select it lands inside label="..." (HTML attribute context) # in the others in element content (HTML text context). # Neither may carry a live tag. unlike $out, qr/"'group'<\/b>"/, "$name: group label is never emitted as raw markup"; if ($name eq 'sel') { like $out, qr/\Q$group_label_html_attribute_context_encoded/, "$name: group label is encoded in HTML attribute context"; } else { like $out, qr/\Q$group_label_html_text_context_encoded/, "$name: group label is encoded in HTML text context"; } # Same for the option label unlike $out, qr/"'option'<\/b>"/, "$name: option label is never emitted as raw markup"; like $out, qr/\Q$option_label_html_text_context_encoded/, "$name: option label is encoded in HTML text context"; # Behaviour the fix must not break: a label needing no escaping is # unchanged, so the fix cannot pass by mangling or dropping labels. like $out, qr/\bplain\b/, "$name: a benign label still renders intact"; } done_testing;