如何在 linux 上正确使用带有 puppeteer 的沙箱并停止变得不安全?
How to properly use sandbox with puppeteer on linux and stop getting insecure?
我阅读了文档:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandboxing.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandbox_ipc.md
但无法找到正确配置沙箱的方法,也无法在我的系统上找到脚本 update-linux-sandbox.sh
。
找到了here
但我得到:
$ ./update-linux-sandbox.sh
/tmp/../out/Debug does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
$ BUILDTYPE=Release ./update-linux-sandbox.sh
/tmp/../out/Release does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
我唯一不安全的解决方法是使用:
const browser = await puppeteer.launch(
{headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']}
);
知道如何正确地做事吗?
弄明白了:enable user namespace cloning 来自内核:
sudo sysctl -w kernel.unprivileged_userns_clone=1
如果您在这里寻找一种方法 运行 在没有 --no-sandbox
arg 的情况下在 Centos7 中使用 Puppeteer,那么@MevatlaveKraspek 的答案将不起作用
我设法让 Puppeteer 在没有 --no-sandbox
标志 arg 的情况下截取屏幕截图,方法是设置 Linux 内核参数以启用命名空间(在 CentOS Linux 版本 7.4.1708 上)。
作为根用户 运行:
echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
检查它是否适用于:
sudo sysctl -a | grep user.max_user_namespaces
现在重新启动您的系统并 运行 一个不使用 --no-sandbox
的脚本,例如 const browser = await puppeteer.launch();
如果它仍然不起作用,您可能正在使用较旧的 Linux 内核,并且需要在内核中设置一些额外的参数。
作为根用户 运行:
grubby --args="user_namespace.enable=1 namespace.unpriv_enable=1" --update-kernel="$(grubby --default-kernel)"
现在重新启动您的系统并检查您刚刚添加的 2 个参数的内核命令行
cat /proc/cmdline
如果它们在命令行中 运行 无需再次使用 --no-sandbox
的脚本,例如 const browser = await puppeteer.launch();
现在应该可以了。如果不是,您可能正在使用不支持命名空间的旧内核。
您可以通过以下方式检查您的内核版本:
uname -a
这是我的内核版本,我有 Puppeteer 运行ning 没有 --no-sandbox
arg。
Linux centos7 3.10.0-693.21.1.el7.x86_64
希望这对您有所帮助:)
对于 Debian,在我的版本 9 Stretch 中,问题似乎与未打开沙盒有关。 Chromium 会吐出致命消息:
- 没有可用的沙箱!
重启前的解决方案(运行 从命令行以 root 身份):
- 回显 1 > /proc/sys/kernel/unprivileged_userns_clone
对于更永久的解决方案(运行 从命令行作为 root):
- echo 'kernel.unprivileged_userns_clone=1' > /etc/sysctl.d/00-local-userns.conf
- 服务进程重启
更多 Debian 相关信息可以在这里找到:
你可以试试:
作为根用户 运行:echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
重新加载 sysctl:sysctl -p
我阅读了文档:
https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandboxing.md
https://chromium.googlesource.com/chromium/src/+/master/docs/linux_sandbox_ipc.md
但无法找到正确配置沙箱的方法,也无法在我的系统上找到脚本 update-linux-sandbox.sh
。
找到了here
但我得到:
$ ./update-linux-sandbox.sh
/tmp/../out/Debug does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
$ BUILDTYPE=Release ./update-linux-sandbox.sh
/tmp/../out/Release does not exist. Use "BUILDTYPE=Release ./update-linux-sandbox.sh" If you are building in Release mode
我唯一不安全的解决方法是使用:
const browser = await puppeteer.launch(
{headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox']}
);
知道如何正确地做事吗?
弄明白了:enable user namespace cloning 来自内核:
sudo sysctl -w kernel.unprivileged_userns_clone=1
如果您在这里寻找一种方法 运行 在没有 --no-sandbox
arg 的情况下在 Centos7 中使用 Puppeteer,那么@MevatlaveKraspek 的答案将不起作用
我设法让 Puppeteer 在没有 --no-sandbox
标志 arg 的情况下截取屏幕截图,方法是设置 Linux 内核参数以启用命名空间(在 CentOS Linux 版本 7.4.1708 上)。
作为根用户 运行:
echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
检查它是否适用于:
sudo sysctl -a | grep user.max_user_namespaces
现在重新启动您的系统并 运行 一个不使用 --no-sandbox
的脚本,例如 const browser = await puppeteer.launch();
如果它仍然不起作用,您可能正在使用较旧的 Linux 内核,并且需要在内核中设置一些额外的参数。
作为根用户 运行:
grubby --args="user_namespace.enable=1 namespace.unpriv_enable=1" --update-kernel="$(grubby --default-kernel)"
现在重新启动您的系统并检查您刚刚添加的 2 个参数的内核命令行
cat /proc/cmdline
如果它们在命令行中 运行 无需再次使用 --no-sandbox
的脚本,例如 const browser = await puppeteer.launch();
现在应该可以了。如果不是,您可能正在使用不支持命名空间的旧内核。
您可以通过以下方式检查您的内核版本:
uname -a
这是我的内核版本,我有 Puppeteer 运行ning 没有 --no-sandbox
arg。
Linux centos7 3.10.0-693.21.1.el7.x86_64
希望这对您有所帮助:)
对于 Debian,在我的版本 9 Stretch 中,问题似乎与未打开沙盒有关。 Chromium 会吐出致命消息:
- 没有可用的沙箱!
重启前的解决方案(运行 从命令行以 root 身份):
- 回显 1 > /proc/sys/kernel/unprivileged_userns_clone
对于更永久的解决方案(运行 从命令行作为 root):
- echo 'kernel.unprivileged_userns_clone=1' > /etc/sysctl.d/00-local-userns.conf
- 服务进程重启
更多 Debian 相关信息可以在这里找到:
你可以试试:
作为根用户 运行:echo "user.max_user_namespaces=15000" >> /etc/sysctl.conf
重新加载 sysctl:sysctl -p