【问题标题】:Is there a way to gatekeep laravel tinker?有没有办法保护 laravel tinker?
【发布时间】:2020-12-10 09:06:23
【问题描述】:

我想知道是否可以扩展或替换 php artisan tinker 命令,因此它首先要求进行身份验证,以此作为看门人可以使用它的一种方式。

我尝试了以下方法:

<?php

namespace App\Console\Commands;

use Laravel\Tinker\Console\TinkerCommand;
use Illuminate\Support\Facades\Auth;

class Tinker extends TinkerCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tinker';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $email      = $this->ask('Login (email)');
        $password   = $this->secret('Password for '.$email);

        if (!Auth::attempt(compact('email', 'password'))) {
            $this->error('Invalid Credentials.');
            return;
        }

        if (Auth::user()->cannot('use-tinker')) {
            $this->error('Unauthorized.');
            return;
        }

        parent::handle();
    }
}

但我收到一个错误,因为我没有包含 TinkerCommand@handle 使用的 'include' 参数

    public function handle()
    {
        $this->getApplication()->setCatchExceptions(false);

        $config = new Configuration([
            'updateCheck' => 'never',
        ]);

        $config->getPresenter()->addCasters(
            $this->getCasters()
        );

        $shell = new Shell($config);
        $shell->addCommands($this->getCommands());
        $shell->setIncludes($this->argument('include')); # <-------- include argument

        if (isset($_ENV['COMPOSER_VENDOR_DIR'])) {
            $path = $_ENV['COMPOSER_VENDOR_DIR'];
        } else {
            $path = $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor';
        }

        $path .= '/composer/autoload_classmap.php';

        $loader = ClassAliasAutoloader::register($shell, $path);

        try {
            $shell->run();
        } finally {
            $loader->unregister();
        }
    }

我不确定 include 参数是关于什么的。我尝试转储它,它是一个空数组。在这一点上,我想知道是否有更好的方法。

【问题讨论】:

    标签: laravel laravel-artisan tinker


    【解决方案1】:

    如果用户能够运行php artisan tinker,他还能够:

    • 查看项目的源代码。他可能也可以编辑它,但在适当的文件权限下可能不是这样

    • 查看您的.env,其中包含您的数据库凭据和其他敏感信息,例如 api 密钥

    我不确定将 tinker 内部的访问限制为已经拥有如此多特权和可能性的用户是否真的有用。例如,他可以编辑您的数据库users 表以授予由他自己控制的用户访问权限,或者他可以编辑源代码以允许访问。

    这是问题的一点可视化:


    如果你仍然想这样做,我会扩展基础Command,而不是扩展 TinkerCommand,然后在身份验证后运行 tinker comman。

    public function handle() {
      
      // Do your own verification
    
      $this->runCommand(TinkerCommand::class, [], $this->output);
      return;
    }
    

    【讨论】:

    • 视觉效果很棒! ?
    猜你喜欢
    • 2020-04-02
    • 1970-01-01
    • 2016-01-15
    • 2023-03-27
    • 2011-08-05
    • 2022-12-18
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多