10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/kompo/tasks/copy_project_files.rb', line 10
def run
work_dir = WorkDir.path
project_dir = Taski.args.fetch(:project_dir, Taski.env.working_directory) || Taski.env.working_directory
entrypoint = Taski.args.fetch(:entrypoint, "main.rb")
files = Taski.args.fetch(:files, [])
src_entrypoint = File.expand_path(File.join(project_dir, entrypoint))
raise "Entrypoint not found: #{src_entrypoint}" unless File.exist?(src_entrypoint)
real_project_dir = File.realpath(project_dir)
real_src = File.realpath(src_entrypoint)
unless real_src.start_with?(real_project_dir + File::SEPARATOR) || real_src == real_project_dir
raise "Entrypoint path escapes project directory: #{entrypoint}"
end
@entrypoint_path = File.join(work_dir, entrypoint)
FileUtils.mkdir_p(File.dirname(@entrypoint_path))
FileUtils.cp(src_entrypoint, @entrypoint_path)
puts "Copied entrypoint: #{entrypoint}"
@additional_paths = []
files.each do |file|
src = File.expand_path(File.join(project_dir, file))
next unless File.exist?(src)
real_src = File.realpath(src)
unless real_src.start_with?(real_project_dir + File::SEPARATOR) || real_src == real_project_dir
warn "Skipping path that escapes project directory: #{file}"
next
end
dest = File.join(work_dir, file)
real_work_dir = File.realpath(work_dir)
expanded_dest = File.expand_path(dest)
unless expanded_dest.start_with?(real_work_dir + File::SEPARATOR) || expanded_dest == real_work_dir
warn "Skipping path that would escape work directory: #{file}"
next
end
if File.directory?(src)
if file == "." || real_src == real_project_dir
copy_directory_contents(src, work_dir)
@additional_paths << work_dir
else
FileUtils.mkdir_p(dest)
FileUtils.cp_r(src, File.dirname(dest))
@additional_paths << dest
end
else
FileUtils.mkdir_p(File.dirname(dest))
FileUtils.cp(src, dest)
@additional_paths << dest
end
puts "Copied: #{file}"
end
end
|