Skip to content

refactor: パラメータ設定値の検証を token_get_all による構文解析に変更 - #1407

Open
nanasess wants to merge 5 commits into
EC-CUBE:masterfrom
nanasess:feature/eval-check-tokenize
Open

refactor: パラメータ設定値の検証を token_get_all による構文解析に変更#1407
nanasess wants to merge 5 commits into
EC-CUBE:masterfrom
nanasess:feature/eval-check-tokenize

Conversation

@nanasess

@nanasess nanasess commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Summary

システム設定 > パラメーター設定 (LC_Page_Admin_System_Parameter) の値検証を担う SC_CheckError::evalCheck() を、eval() による評価から token_get_all(..., TOKEN_PARSE) を用いた構文解析に変更します。

パラメーター値は data/cache/mtb_constants.phpdefine('KEY', <値>); の形で出力され、各リクエストで読み込まれます。そのため <値>スカラ定数式(リテラル / 定数 / それらの連結式)として成立することを、値を実行せずに確認します。

変更点

  • evalCheck()eval('return is_scalar(...)') から、token_get_all(TOKEN_PARSE) による構文解析 + トークンの許可リストに変更
    • 数値 / クオート文字列 / 定数名 (true/false/null 含む) / 連結 (.) / 符号 (+/-) のみ許可
    • 関数呼び出し・複数の文・変数などスカラ定数式でない値は許可しない
  • mtb_constants_init.php の既定値 245 件、およびシングル / ダブルクォート文字列はいずれも従来どおり許可されます
  • SC_CheckError_EVAL_CHECKTest にスカラ定数式・非スカラ式のデータプロバイダを追加

補足

define('AAA', 'BBB') のような関数呼び出しは、旧実装では is_scalar() の評価結果が true になるため許可されていましたが、スカラ定数式ではないため本変更では許可しません。mtb_constants の実値に該当するものはありません。

Test plan

  • tests/class/SC_CheckError/SC_CheckError_EVAL_CHECKTest.php (24 tests) 通過
  • tests/class/pages/admin/system/LC_Page_Admin_System_ParameterTest.php 通過
  • PHPUnit 全テスト通過 (MySQL, 1814 tests OK)
  • PHPUnit 全テスト通過 (PostgreSQL)
  • 管理画面のパラメーター設定で各種の値が保存でき、不正な値はバリデーションエラーになること

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • 定数値検証の判定基準を見直し、出力される定数が「スカラ定数式」(許可されたリテラル・定数・連結のみ)であることを厳密に確認するよう改善しました。構文エラー時は安全に誤判定しない挙動に変更しました。
  • Tests

    • 有効/無効のパターンをデータ駆動で網羅するテストを追加・拡充し、入力変更に合わせて検証シナリオを整理しました。旧実装の条件付きテストは整理・削除し、エラー期待値を統一しました。

SC_CheckError::evalCheck() の値検証を eval() から
token_get_all(TOKEN_PARSE) を用いた構文解析に変更する。値は
mtb_constants.php へ define('KEY', <値>); の形で出力されるため、
リテラル・定数・それらの連結式といったスカラ定数式のみを許可する。

- 関数呼び出しや複数の文を含む値はスカラ定数式ではないため許可しない
- mtb_constants_init.php の既定値245件、および
  シングル/ダブルクォート文字列はいずれも従来どおり許可される
- EVAL_CHECK のテストにスカラ定数式・非スカラ式の各ケースを追加

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9655f0b4-ed04-4a6a-9b41-d0ed413e9055

📥 Commits

Reviewing files that changed from the base of the PR and between 846c8a4 and ad7c6d2.

📒 Files selected for processing (1)
  • data/class/SC_CheckError.php
🚧 Files skipped from review as they are similar to previous changes (1)
  • data/class/SC_CheckError.php

📝 Walkthrough

Walkthrough

SC_CheckError::evalCheck()evalis_scalar による判定から token_get_all() による検査へ変更し、スカラ定数式のみを許可する実装と、許可値・拒否値を検証するパラメータ化テストを追加しました。

Changes

スカラ定数式検証

Layer / File(s) Summary
トークン解析による検証実装
data/class/SC_CheckError.php
evalCheck() をトークン列検査へ変更し、許可トークン以外、構文エラー、不正な区切り、値のない入力を拒否します。
許可値・拒否値のパラメータ化テスト
tests/class/SC_CheckError/SC_CheckError_EVAL_CHECKTest.php
文字列、数値、真偽値、定数参照、連結式などの許可値と、関数呼び出し、変数、複数文などの拒否値をデータ駆動で検証します。

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: nobuhiko, seasoftjapan

Poem

トークン跳ねるよ、ぴょんぴょんと
定数の式を見分けてる
関数呼び出しはおことわり
テストがにっこり耳を振る
うさぎも安心、月の夜 LinkedIn osephinated

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed 主要変更である検証方式の変更(evalからtoken_get_allによる解析へ)を的確に表しています。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nanasess
nanasess enabled auto-merge June 12, 2026 01:54

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@data/class/SC_CheckError.php`:
- Around line 1792-1815: evalCheck() currently treats a bare ';' / '1;' /
whitespace as valid because token_get_all('<?php '.$value.';', TOKEN_PARSE)
allows the trailing ';' unconditionally; tighten evalCheck() so single-character
';' is only permitted if it is the final wrapper-added token and there is at
least one value token present (i.e. require at least one
non-T_OPEN_TAG/T_WHITESPACE token other than the wrapper ';'), and otherwise
reject inputs like ';', '1;' and pure whitespace; update the allowedTypes/token
loop in SC_CheckError::evalCheck to enforce that rule and ensure
getMasterDataAsDefine() cannot receive an empty/only-semicolon $val, and add the
test cases ';', '1;', and ' ' to invalidValueProvider so they are asserted
invalid.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 897557d1-b6a2-4212-b02f-dc11ca4c0c40

📥 Commits

Reviewing files that changed from the base of the PR and between e8722b1 and 91f89c5.

📒 Files selected for processing (2)
  • data/class/SC_CheckError.php
  • tests/class/SC_CheckError/SC_CheckError_EVAL_CHECKTest.php

Comment thread data/class/SC_CheckError.php
nanasess and others added 2 commits June 12, 2026 12:11
token_get_all のトークン検証で ';' を末尾 (構文解析用に付与した区切り) のみ
許可し、値となるトークンを最低 1 つ必須とする。これにより ';' / '1;' /
空白のみといった、define('KEY', <値>); に展開すると構文エラーとなる入力を
バリデーションエラーとする。

SC_CheckError_EVAL_CHECKTest の invalidValueProvider に該当ケースを追加。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 56.31%. Comparing base (f56c50a) to head (c849084).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1407      +/-   ##
==========================================
+ Coverage   56.23%   56.31%   +0.07%     
==========================================
  Files          87       87              
  Lines       11091    11111      +20     
==========================================
+ Hits         6237     6257      +20     
  Misses       4854     4854              
Flag Coverage Δ
tests 56.31% <100.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant