Skip to content

Commit bb133e8

Browse files
committed
Build test tarballs without hex_core path validation
hex_core 0.16.0 validates paths in `:hex_tarball.create_docs/1`, rejecting unsafe paths like `dir/../../baz.html`. The `Hexdocs.Tar.create/1` test helper relies on being able to produce such tarballs to exercise the unpacker's defense against malicious uploads, so build the tarball with `:hex_erl_tar` directly and gzip it.
1 parent 507ba75 commit bb133e8

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/hexdocs/tar.ex

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,20 @@ defmodule Hexdocs.Tar do
22
require Logger
33

44
def create(files) do
5-
files = for {path, contents} <- files, do: {String.to_charlist(path), contents}
6-
{:ok, tarball} = :hex_tarball.create_docs(files)
7-
tarball
5+
path = Path.join(System.tmp_dir!(), "hexdocs-tar-#{System.unique_integer([:positive])}")
6+
{:ok, tar} = :hex_erl_tar.open(path, [:write])
7+
8+
try do
9+
Enum.each(files, fn {name, contents} ->
10+
:ok = :hex_erl_tar.add(tar, contents, String.to_charlist(name), [])
11+
end)
12+
after
13+
:ok = :hex_erl_tar.close(tar)
14+
end
15+
16+
data = File.read!(path)
17+
File.rm!(path)
18+
:zlib.gzip(data)
819
end
920

1021
def unpack_to_dir({:file, path}, opts \\ []) do

0 commit comments

Comments
 (0)