22//Petit Note (c)さとぴあ @satopian 2021-2026 MIT License
33//https://paintbbs.sakura.ne.jp/
44
5- $ search_inc_ver = 20260614 ;
5+ $ search_inc_ver = 20260615 ;
66class processsearch
77{
88
@@ -13,19 +13,10 @@ class processsearch
1313
1414 private static function init (): void
1515 {
16-
17- $ gets =filter_input_array (INPUT_GET ) ?? [];
18-
19- // 許可リストをキーにした配列を作成
20- $ allowed_keys = array_fill_keys (['mode ' , 'radio ' , 'imgsearch ' , 'q ' ,'page ' ], true );
21- // 不正なキーを抽出
22- $ invalid_keys = array_diff_key ($ gets , $ allowed_keys );
23- if (!empty ($ invalid_keys )) {
24- header ("HTTP/1.1 403 Forbidden " );
25- exit ();
26- }
2716
28- self ::$ imgsearch = (bool )filter_input_data ('GET ' , 'imgsearch ' , FILTER_VALIDATE_BOOLEAN );
17+ self ::validateQueryParameters ();
18+
19+ self ::$ imgsearch = (bool )filter_input_data ('GET ' , 'imgsearch ' , FILTER_VALIDATE_BOOLEAN );
2920 self ::$ page = (int )filter_input_data ('GET ' , 'page ' , FILTER_VALIDATE_INT );
3021 self ::$ q = (string )filter_input_data ('GET ' , 'q ' );
3122 self ::$ radio = (int )filter_input_data ('GET ' , 'radio ' , FILTER_VALIDATE_INT );
@@ -305,4 +296,34 @@ private static function create_formatted_text_for_search(?string $str): string
305296
306297 return $ s_str ;
307298 }
299+ /**
300+ * 不正なクエリパラメータの時は 403 Forbiddenを返す
301+ * @return void
302+ */
303+ private static function validateQueryParameters (): void {
304+
305+ $ gets =filter_input_array (INPUT_GET ) ?? [];
306+ // 許可リストをキーにした配列を作成
307+ $ allowed_keys = array_fill_keys (['mode ' , 'radio ' , 'imgsearch ' , 'q ' ,'page ' ], true );
308+ // 不正なキーを抽出
309+ $ invalid_keys = array_diff_key ($ gets , $ allowed_keys );
310+
311+ $ page =filter_input_data ('GET ' ,'page ' ,FILTER_VALIDATE_INT );
312+ $ radio =filter_input_data ('GET ' ,'radio ' ,FILTER_VALIDATE_INT );
313+ $ imgsearch =filter_input_data ('GET ' ,'imgsearch ' );
314+ $ imagSearchOptions =["on " ,"off " ];
315+ $ isAllowedOption = in_array ($ imgsearch ,$ imagSearchOptions );
316+
317+ //フィルタが失敗した時はfalse
318+ if (
319+ !empty ($ invalid_keys )||
320+ $ page ===false ||
321+ $ radio ===false ||
322+ $ imgsearch && $ isAllowedOption ===false
323+ )
324+ {
325+ header ("HTTP/1.1 403 Forbidden " );
326+ exit ();
327+ }
328+ }
308329}
0 commit comments