셸 명령어 개발 가이드라인
GitLab 코드베이스에서 프로세스와 파일을 안전하고 신뢰성 있게 다루기 위한 셸 명령어 개발 가이드라인을 설명합니다.
이 문서에는 GitLab 코드베이스에서 프로세스와 파일을 다루기 위한 가이드라인이 포함되어 있습니다. 이 가이드라인은 코드의 신뢰성과 보안을 높이기 위한 것입니다. 참고 자료 # Google Ruby Security Reviewer's Guide OWASP Command Injection Ruby on Rails Security Guide Command Line Injection 셸 명령어 대신 File과 FileUtils를 사용하세요 # 때로는 Ruby API가 존재함에도 불구하고 셸을 통해 기본적인 Unix 명령어를 실행하는 경우가 있습니다. Ruby API가 있다면 Ruby API 를 사용하세요. # Wrong system "mkdir -p tmp/special/directory" # Better (separate tokens) system *%W(mkdir -p tmp/special/directory) # Best (do not use a shell command) FileUtils.mkdir_p "tmp/special/directory" # Wrong contents = `cat #{filename}` # Correct contents = File.read(filename) # Sometimes a shell command is just the best solution. The example below has no # user input, and is hard to implement correctly in Ruby: delete all files and # directories older than 120 minutes under /some/path, but not /some/path # itself. Gitlab::Popen.popen(%W(find /some/path -not -path /some/path -mmin +120 -delete)) 이 코딩 스타일은 CVE-2013-4490을 예방할 수 있었습니다. Git 명령어에는 항상 설정 가능한 Git 바이너리 경로를 사용하세요 # # Wrong system(*%W(git branch -d -- #{branch_name})) # Correct system(*%W(#{Gitlab.config.git.bin_path} branch -d -- #{branch_name})) 명령어를 별도 토큰으로 분리하여 셸 우회 # Ruby에 셸 명령어를 단일 문자열로 전달하면 Ruby는 /bin/sh 가 전체 문자열을 평가하게 합니다. 이는 본질적으로 셸에 한 줄짜리 스크립트를 실행하도록 요청하는 것으로, 셸 인젝션 공격의 위험이 있습니다. 셸 명령어를 직접 토큰으로 분리하는 것이 더 안전합니다. 때로는 셸의 스크립트 기능을 사용하여 작업 디렉터리를 변경하거나 환경 변수를 설정하기도 합니다. 이러한 모든 작업은 Ruby에서 직접 안전하게 수행할 수 있습니다. # Wrong system "cd /home/git/gitlab && bundle exec rake db:#{somethi