Harden alpha8 publication safety
This commit is contained in:
@@ -11,6 +11,7 @@ require "marcel" # SandboxFile#content_type uses Marcel::MimeType.for
|
||||
class Opencode::SandboxFileTest < Minitest::Test
|
||||
def setup
|
||||
@tmpdir = Dir.mktmpdir("opencode-rails-sandbox-file-test-")
|
||||
@outside_dir = Dir.mktmpdir("opencode-rails-sandbox-file-outside-")
|
||||
@path = File.join(@tmpdir, "notes.md")
|
||||
File.write(@path, "# hello\nworld\n")
|
||||
# SandboxFile uses `start_with?` against this prefix to detect path
|
||||
@@ -21,6 +22,7 @@ class Opencode::SandboxFileTest < Minitest::Test
|
||||
|
||||
def teardown
|
||||
FileUtils.remove_entry(@tmpdir) if @tmpdir && File.exist?(@tmpdir)
|
||||
FileUtils.remove_entry(@outside_dir) if @outside_dir && File.exist?(@outside_dir)
|
||||
end
|
||||
|
||||
def test_basic_readers
|
||||
@@ -59,4 +61,49 @@ class Opencode::SandboxFileTest < Minitest::Test
|
||||
assert_equal "notes.md", artifact.filename
|
||||
assert_equal "# hello\nworld\n", artifact.content
|
||||
end
|
||||
|
||||
def test_content_rejects_a_file_replaced_by_an_outside_symlink
|
||||
file = Opencode::SandboxFile.new(
|
||||
path: @path, sandbox_prefix: @sandbox_prefix, max_bytes: 10_000
|
||||
)
|
||||
outside = File.join(@outside_dir, "private.txt")
|
||||
File.write(outside, "private")
|
||||
assert file.safe?
|
||||
|
||||
File.unlink(@path)
|
||||
File.symlink(outside, @path)
|
||||
|
||||
assert_raises(Opencode::SandboxFile::UnsafeFileError) { file.content }
|
||||
end
|
||||
|
||||
def test_content_rejects_an_inode_that_grows_past_the_cap_while_reading
|
||||
file = Opencode::SandboxFile.new(
|
||||
path: @path, sandbox_prefix: @sandbox_prefix, max_bytes: 20
|
||||
)
|
||||
real_open = File.method(:open)
|
||||
grow_on_read = lambda do |path, *args, **kwargs, &block|
|
||||
real_open.call(path, *args, **kwargs) do |opened|
|
||||
proxy = Object.new
|
||||
proxy.define_singleton_method(:stat) { opened.stat }
|
||||
proxy.define_singleton_method(:read) do |length = nil|
|
||||
real_open.call(path, "ab") { |writer| writer.write("x" * 100) }
|
||||
opened.read(length)
|
||||
end
|
||||
block.call(proxy)
|
||||
end
|
||||
end
|
||||
|
||||
File.stub(:open, grow_on_read) do
|
||||
assert_raises(Opencode::SandboxFile::UnsafeFileError) { file.content }
|
||||
end
|
||||
end
|
||||
|
||||
def test_content_returns_binary_empty_string_for_a_zero_byte_file
|
||||
File.write(@path, "")
|
||||
file = Opencode::SandboxFile.new(
|
||||
path: @path, sandbox_prefix: @sandbox_prefix, max_bytes: 20
|
||||
)
|
||||
|
||||
assert_equal "".b, file.content
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user