プロジェクト

全般

プロフィール

Bug(バグ) #3401

sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある

Yuya Watanabe10年以上前に追加. 約7年前に更新.

ステータス:
Won't fix(対応せず)
優先度:
High(高め)
対象バージョン:
開始日:
2013-09-12
期日:
進捗率:

0%

3.6 で発生するか:
Unknown (未調査)
3.8 で発生するか:
Unknown (未調査)

説明

概要

sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある.

sfValidatorChoice に choices オプションを渡すと渡された配列に含まれるかどうかについてバリデーションが行われるが,この choices に指定される配列の中身に "00" や "11.1" といったような文字列が渡された場合に正しくバリデーションが行われれない.

この問題については下記チケットで reopen されている.ただし動きはない.

#4212 (sfValidatorChoice in_array bug (php bug?)) - symfony - Trac
http://trac.symfony-project.org/ticket/4212

原因

chices に含まれるかどうかの判定で string にキャストした後に == でチェックしているため,文字列に数値が含まれる場合に正しく評価されない.

lib/vendor/symfony/lib/validator/sfValidatorChoice.class.php
117   /** 
118    * Checks if a value is part of given choices (see bug #4212) 
119    * 
120    * @param  mixed $value   The value to check 
121    * @param  array $choices The array of available choices 
122    * 
123    * @return Boolean 
124    */ 
125   static protected function inChoices($value, array $choices = array()) 
126   { 
127     foreach ($choices as $choice) 
128     { 
129       if ((string) $choice == (string) $value) 
130       { 
131         return true; 
132       } 
133     } 
134  
135     return false; 
136   } 

修正案

sfValidatorChoice 自体は choices に整数値を与えることを目的としたバリデータとしたうえで, choices に文字列など整数値以外のものを対象とした opValidatorChoice のようなクラスを新たにつくり, inChoices および inChoices を用いた部分を上書きする. inChoices 自体が static で sfValidatorChoice で self 呼び出ししているため継承してオーバーライドするだけでは有効に働かない様子.

テストコード

<?php

include_once dirname(__FILE__) . '/../../bootstrap/unit.php';

$t = new lime_test(9, new lime_output_color());

$t->diag('opValidatorChoice');

$t->diag('->clean()');

$v = new opValidatorChoice(array(
  'choices' => array('00', '000', '0.0'),
));

try
{
  $v->clean('0');
  $t->fail('->clean() throws a sfValidatorError if not in choices');
  $t->skip('', 1);
}
catch (Exception $e)
{
  $t->pass('->clean() throws a sfValidatorError if not in choices');
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}

$t->is($v->clean('00'), '00', '->clean() accepts in choices');
$t->is($v->clean('000'), '000', '->clean() accepts in choices');
$t->is($v->clean('0.0'), '0.0', '->clean() accepts in choices');

$v = new opValidatorChoice(array(
  'choices' => array('011', '11.0'),
));

try
{
  $v->clean('11');
  $t->fail('->clean() throws a sfValidatorError if not in choices');
  $t->skip('', 1);
}
catch (Exception $e)
{ 
  $t->pass('->clean() throws a sfValidatorError if not in choices');
  $t->is($e->getCode(), 'invalid', '->clean() throws a sfValidatorError');
}

$t->is($v->clean('011'), '011', '->clean() accepts in choices');
$t->is($v->clean('11.0'), '11.0', '->clean() accepts in choices');

t-2982.patch 表示 (3.52 KB) Shinichi Urabe, 2015-06-19 16:43


関連するチケット

コピー先 OpenPNE 3 - Backport(バックポート) #3810: sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある Fixed(完了) 2013-09-12
コピー先 OpenPNE 3 - Backport(バックポート) #3811: sfValidatorChoice に数値以外を渡した場合に正しくバリデーションが行われない場合がある Fixed(完了) 2013-09-12

履歴

#1 Yuya Watanabe10年以上前に更新

  • 対象バージョンOpenPNE 3.9.0-old にセット

#2 Shinichi Urabe9年以上前に更新

  • 優先度Normal(通常) から High(高め) に変更

#3 Shinichi Urabeほぼ9年前に更新

修正案のパッチを添付します

ただし、本パッチを適用するのみではFIXとは言えないです
sfValidatorChoice が元々使われている箇所を opValidatorChoice に置き換える必要があります。以下 (テスト範囲大)

[urabe@urab-mbp OpenPNE3]$ ag -w sfValidatorChoice --ignore=vendor -l
apps/pc_backend/modules/advanced/lib/opRichTextareaOpenPNEConfigForm.class.php
apps/pc_backend/modules/design/lib/PickHomeLayoutForm.class.php
apps/pc_backend/modules/mail/lib/opMailNotificationForm.class.php
apps/pc_backend/modules/member/lib/AdminInviteForm.class.php
apps/pc_backend/modules/plugin/lib/PluginActivationForm.class.php
lib/form/doctrine/ActivityDataForm.class.php
lib/form/doctrine/BannerForm.class.php
lib/form/doctrine/CommunityConfigForm.class.php
lib/form/doctrine/OAuthConsumerInformationForm.class.php
lib/form/doctrine/opPermitMemberConfigSnsConfigForm.class.php
lib/form/doctrine/ProfileForm.class.php
lib/form/MemberConfigForm/MemberConfigMailForm.class.php
lib/form/opLanguageSelecterForm.class.php
lib/form/opPresetProfileForm.class.php
lib/plugins/sfFormExtraPlugin/lib/validator/sfValidatorBlacklist.class.php
lib/plugins/sfFormExtraPlugin/lib/widget/sfWidgetFormSelectUSState.class.php
lib/util/opFormItemGenerator.class.php
lib/validator/opValidatorProfile.class.php
plugins/opAuthMailAddressPlugin/lib/form/opAuthMailAddressPasswordRecoveryForm.class.php

#4 Chiharu Nakajimaほぼ9年前に更新

#5 Chiharu Nakajimaほぼ9年前に更新

#6 Chiharu Nakajima8年以上前に更新

  • ステータスNew(新規) から Pending Review(レビュー待ち) に変更
  • 進捗率0 から 50 に変更

プルリクエストしました。
https://github.com/openpne/OpenPNE3/pull/254

#7 Shinichi Urabe8年以上前に更新

  • 担当者Chiharu Nakajima にセット

#8 Shinichi Urabe8年以上前に更新

  • ステータスPending Review(レビュー待ち) から Pending Testing(テスト待ち) に変更
  • 進捗率50 から 70 に変更

#10 isao sano約7年前に更新

  • ステータスPending Testing(テスト待ち) から Won't fix(対応せず) に変更
  • 進捗率70 から 0 に変更

OpenPNE 3.8.17 にて対応済みであったため、対応せずとします。

他の形式にエクスポート: Atom PDF