hassankhan / config

Config is a lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files
MIT License
971 stars 136 forks source link

parse中eval(code) 有bug #115

Closed lphkxd closed 5 years ago

lphkxd commented 5 years ago

我配置文件中用的是 $config['key'] = $val;

Parser/Php.php 文件中 parse 方法的 eval($config)造成字符串转数组失败。

hassankhan commented 5 years ago

Hi, could you please post an example of the file that is causing the error and the stack trace showing the exact error?

2pgcn commented 5 years ago
PHP Fatal error:  Uncaught Noodlehaus\Exception\UnsupportedFormatException: PHP string does not return an array in /home/pg/www/vendor/hassankhan/config/src/Parser/Php.php:54
Stack trace:
#0 /home/pg/www/vendor/hassankhan/config/src/Config.php(97): Noodlehaus\Parser\Php->parse('3/**\n * Created...')
#1 /home/pg/www/vendor/hassankhan/config/src/Config.php(62): Noodlehaus\Config->loadFromFile('/home/pg/www/bi...', Object(Noodlehaus\Parser\Php))
#2 /home/pg/www/vendor/tmtbe/swooledistributed/src/Server/Console/ChannelCmd.php(31): Noodlehaus\Config->__construct('/home/pg/www/bi...')
#3 /home/pg/www/vendor/tmtbe/swooledistributed/src/Server/Start.php(124): Server\Console\ChannelCmd->__construct('ChannelCmd')
#4 /home/pg/www/vendor/tmtbe/swooledistributed/src/Server/Start.php(102): Server\Start::addDirCommand('/home/pg/www/bi...', 'Server', Object(Symfony\Component\Console\Application))
#5 /home/pg/www/bin/start_swoole_server.php(11): Server\Start::run()
#6 {main}
  thrown in /home/pg/www/vendor/hassankhan/config/src/Parser/Php.php on line 54

@hassankhan 将文件$config修改为$configs就可以正常使用 版本2出现的 改成1后正常

2pgcn commented 5 years ago

![Uploading image.png…]() 修改名字之后可以正常使用

hassankhan commented 5 years ago

Thanks for the stack trace, @php403, any chance you could post a sample config file as well?

filips123 commented 5 years ago

It seems this is (another) issue with eval in PHP parser. This is probably fixed in #114.

hassankhan commented 5 years ago

@lphkxd @php403 Could you please try again using the develop branch? #114 has been merged in and should hopefully fix the issues you were seeing

FanchangWang commented 5 years ago

@lphkxd @php403 Could you please try again using the develop branch? #114 has been merged in and should hopefully fix the issues you were seeing

file: testConfig.php

<?php
    $config['a'] = 'b';
    return $config;

file: eval.php

<?php
$str = file_get_contents('./testConfig.php');

parse($str);

function parse($config)
{
    // Strip PHP start and end tags
    $config = str_replace('<?php', '', $config);
    $config = str_replace('<?', '', $config);
    $config = str_replace('?>', '', $config);

    try {
        $temp = eval($config);
    } catch (Exception $e) {
        print_r($e->getMessage()); //error message
    }

    print_r($temp);
}

error message:

PHP Warning:  Illegal string offset 'a' in eval.php(14) : eval()'d code on line 2

Warning: Illegal string offset 'a' in eval.php(14) : eval()'d code on line 2
b   $config['a'] = 'b';
    return $config;%    

testConfig.php 中的 $config 与 function parse 中的 $config 有冲突

shykhy commented 5 years ago

Hi guys I have gotten the same question like this. config file: config The below is the error statement. error statement: PHP Fatal error: Uncaught Error: Cannot use string offset as an array in vendor/hassankhan/config/src/Parser/Php.php(37) : eval()'d code:10

shykhy commented 5 years ago

It's occurred on version 2.00. @hassankhan

FanchangWang commented 5 years ago

Hi guys I have gotten the same question like this. config file: config The below is the error statement. error statement: PHP Fatal error: Uncaught Error: Cannot use string offset as an array in vendor/hassankhan/config/src/Parser/Php.php(37) : eval()'d code:10

SwooleDistributed?有啥交流群之类的分享一个吗

filips123 commented 5 years ago

The problem is that variable $config is already initialized in parameter as string. The configuration file will than try to re-initialize it and fail it because eval executions can see and modify variables from the real file.

Solution may be to run eval in isolated environment, but eval doesn't support this. Maybe it could ran in separate method for this.

filips123 commented 5 years ago

@hassankhan I fixed this in #118. Can you check and merge it?