LuaSandbox
LuaSandbox
언뜻 보면 Lua 확장과 비슷해 보이지만, LuaSandbox는 목적이 달라요. "신뢰할 수 없는(untrusted)" Lua 코드를 PHP 안에서 안전하게 실행해야 할 때가 있거든요. LuaSandbox는 PHP 7과 PHP 8을 대상으로, 그런 코드를 실행할 수 있는 안전한 샌드박스(sandbox) 환경을 제공하는 확장이에요.
출처: LuaSandbox
본문
LuaSandbox는 PHP 7과 PHP 8용 확장으로, PHP 안에서 신뢰할 수 없는 Lua 5.1 코드를 안전하게 실행할 수 있게 해 줘요.
Lua 확장과의 차이점(Differences compared to the Lua extension)
- 시간·메모리 제한 지원이 있어요(LuaSandbox has support for time and memory limits).
- 신뢰할 수 없는 코드를 실행하기 위한 기본 안전 환경을 제공해요.
- 기본 Lua 함수들을 보안 관점에서 검토했고, 일부는 그에 맞게 패치했어요.
- PHP 인터페이스는 더 복잡하고 정밀하며 강력하지만, 개발자가 쓰기에는 다소 불편할 수 있어요.
- Lua 5.1만 지원해요. LuaSandbox가 많이 수정된 Lua 표준 라이브러리를 쓰고, 주요 Lua 버전 사이에 하위 호환이 없어서 이를 바꾸기는 어려워요.
- 사용자가 제공한 스크립트와의 하위 호환성 최대화를 목표로 해요.
설치·설정(Installing/Configuring)
- 요구 사항(Requirements)
- 설치(Installation)
- 표준 Lua와의 차이점(Differences from Standard Lua)
- 예제(Examples) — LuaSandbox 기본 사용법(Basic usage for LuaSandbox)
LuaSandbox 클래스
LuaSandbox— LuaSandbox 클래스LuaSandbox::callFunction— Lua 전역 변수의 함수를 호출해요(Call a function in a Lua global variable).LuaSandbox::disableProfiler— 프로파일러를 비활성화해요(Disable the profiler).LuaSandbox::enableProfiler— 프로파일러를 활성화해요(Enable the profiler).LuaSandbox::getCPUUsage— Lua 환경의 현재 CPU 사용 시간을 가져와요(Fetch the current CPU time usage of the Lua environment).LuaSandbox::getMemoryUsage— Lua 환경의 현재 메모리 사용량을 가져와요(Fetch the current memory usage of the Lua environment).LuaSandbox::getPeakMemoryUsage— Lua 환경의 최대 메모리 사용량을 가져와요(Fetch the peak memory usage of the Lua environment).LuaSandbox::getProfilerFunctionReport— 프로파일러 데이터를 가져와요(Fetch profiler data).LuaSandbox::getVersionInfo— LuaSandbox와 Lua의 버전을 반환해요(Return the versions of LuaSandbox and Lua).LuaSandbox::loadBinary— 미리 컴파일된 바이너리 청크를 Lua 환경에 로드해요(Load a precompiled binary chunk into the Lua environment).LuaSandbox::loadString— Lua 코드를 Lua 환경에 로드해요(Load Lua code into the Lua environment).LuaSandbox::pauseUsageTimer— CPU 사용 타이머를 일시 중지해요(Pause the CPU usage timer).LuaSandbox::registerLibrary— PHP 함수 묶음을 Lua 라이브러리로 등록해요(Register a set of PHP functions as a Lua library).LuaSandbox::setCPULimit— Lua 환경의 CPU 시간 제한을 설정해요(Set the CPU time limit for the Lua environment).LuaSandbox::setMemoryLimit— Lua 환경의 메모리 제한을 설정해요(Set the memory limit for the Lua environment).LuaSandbox::unpauseUsageTimer—LuaSandbox::pauseUsageTimer가 일시 중지한 타이머를 재개해요(Unpause the timer paused by LuaSandbox::pauseUsageTimer).LuaSandbox::wrapPhpFunction— PHP 콜러블을 LuaSandboxFunction으로 감싸요(Wrap a PHP callable in a LuaSandboxFunction).
LuaSandboxFunction 클래스
LuaSandboxFunction— LuaSandboxFunction 클래스LuaSandboxFunction::call— Lua 함수를 호출해요(Call a Lua function).LuaSandboxFunction::__construct— 사용되지 않아요(Unused).LuaSandboxFunction::dump— 함수를 바이너리 블롭으로 덤프해요(Dump the function as a binary blob).
예외 클래스
LuaSandboxError— LuaSandboxError 클래스LuaSandboxErrorError— LuaSandboxErrorError 클래스LuaSandboxFatalError— LuaSandboxFatalError 클래스LuaSandboxMemoryError— LuaSandboxMemoryError 클래스LuaSandboxRuntimeError— LuaSandboxRuntimeError 클래스LuaSandboxSyntaxError— LuaSandboxSyntaxError 클래스LuaSandboxTimeoutError— LuaSandboxTimeoutError 클래스
더 알아보기
- Lua 확장과의 차이(특히 시간·메모리 제한과 기본 안전 환경)를 직접 비교해 보면 두 확장의 설계 철학이 분명해져요.
setCPULimit,setMemoryLimit을 실제로 낮춰서 샌드박스가 어떻게 제한을 걸어주는지 시험해 보는 것도 좋아요.