cowboy_static.erl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. %% Copyright (c) 2013-2017, Loïc Hoguin <essen@ninenines.eu>
  2. %% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
  3. %%
  4. %% Permission to use, copy, modify, and/or distribute this software for any
  5. %% purpose with or without fee is hereby granted, provided that the above
  6. %% copyright notice and this permission notice appear in all copies.
  7. %%
  8. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. -module(cowboy_static).
  16. -export([init/2]).
  17. -export([malformed_request/2]).
  18. -export([forbidden/2]).
  19. -export([content_types_provided/2]).
  20. -export([charsets_provided/2]).
  21. -export([resource_exists/2]).
  22. -export([last_modified/2]).
  23. -export([generate_etag/2]).
  24. -export([get_file/2]).
  25. -type extra_charset() :: {charset, module(), function()} | {charset, binary()}.
  26. -type extra_etag() :: {etag, module(), function()} | {etag, false}.
  27. -type extra_mimetypes() :: {mimetypes, module(), function()}
  28. | {mimetypes, binary() | {binary(), binary(), [{binary(), binary()}]}}.
  29. -type extra() :: [extra_charset() | extra_etag() | extra_mimetypes()].
  30. -type opts() :: {file | dir, string() | binary()}
  31. | {file | dir, string() | binary(), extra()}
  32. | {priv_file | priv_dir, atom(), string() | binary()}
  33. | {priv_file | priv_dir, atom(), string() | binary(), extra()}.
  34. -export_type([opts/0]).
  35. -include_lib("kernel/include/file.hrl").
  36. -type state() :: {binary(), {direct | archive, #file_info{}}
  37. | {error, atom()}, extra()}.
  38. %% Resolve the file that will be sent and get its file information.
  39. %% If the handler is configured to manage a directory, check that the
  40. %% requested file is inside the configured directory.
  41. -spec init(Req, opts()) -> {cowboy_rest, Req, error | state()} when Req::cowboy_req:req().
  42. init(Req, {Name, Path}) ->
  43. init_opts(Req, {Name, Path, []});
  44. init(Req, {Name, App, Path})
  45. when Name =:= priv_file; Name =:= priv_dir ->
  46. init_opts(Req, {Name, App, Path, []});
  47. init(Req, Opts) ->
  48. init_opts(Req, Opts).
  49. init_opts(Req, {priv_file, App, Path, Extra}) ->
  50. {PrivPath, HowToAccess} = priv_path(App, Path),
  51. init_info(Req, absname(PrivPath), HowToAccess, Extra);
  52. init_opts(Req, {file, Path, Extra}) ->
  53. init_info(Req, absname(Path), direct, Extra);
  54. init_opts(Req, {priv_dir, App, Path, Extra}) ->
  55. {PrivPath, HowToAccess} = priv_path(App, Path),
  56. init_dir(Req, PrivPath, HowToAccess, Extra);
  57. init_opts(Req, {dir, Path, Extra}) ->
  58. init_dir(Req, Path, direct, Extra).
  59. priv_path(App, Path) ->
  60. case code:priv_dir(App) of
  61. {error, bad_name} ->
  62. error({badarg, "Can't resolve the priv_dir of application "
  63. ++ atom_to_list(App)});
  64. PrivDir when is_list(Path) ->
  65. {
  66. PrivDir ++ "/" ++ Path,
  67. how_to_access_app_priv(PrivDir)
  68. };
  69. PrivDir when is_binary(Path) ->
  70. {
  71. << (list_to_binary(PrivDir))/binary, $/, Path/binary >>,
  72. how_to_access_app_priv(PrivDir)
  73. }
  74. end.
  75. how_to_access_app_priv(PrivDir) ->
  76. %% If the priv directory is not a directory, it must be
  77. %% inside an Erlang application .ez archive. We call
  78. %% how_to_access_app_priv1() to find the corresponding archive.
  79. case filelib:is_dir(PrivDir) of
  80. true -> direct;
  81. false -> how_to_access_app_priv1(PrivDir)
  82. end.
  83. how_to_access_app_priv1(Dir) ->
  84. %% We go "up" by one path component at a time and look for a
  85. %% regular file.
  86. Archive = filename:dirname(Dir),
  87. case Archive of
  88. Dir ->
  89. %% filename:dirname() returned its argument:
  90. %% we reach the root directory. We found no
  91. %% archive so we return 'direct': the given priv
  92. %% directory doesn't exist.
  93. direct;
  94. _ ->
  95. case filelib:is_regular(Archive) of
  96. true -> {archive, Archive};
  97. false -> how_to_access_app_priv1(Archive)
  98. end
  99. end.
  100. absname(Path) when is_list(Path) ->
  101. filename:absname(list_to_binary(Path));
  102. absname(Path) when is_binary(Path) ->
  103. filename:absname(Path).
  104. init_dir(Req, Path, HowToAccess, Extra) when is_list(Path) ->
  105. init_dir(Req, list_to_binary(Path), HowToAccess, Extra);
  106. init_dir(Req, Path, HowToAccess, Extra) ->
  107. Dir = fullpath(filename:absname(Path)),
  108. PathInfo = cowboy_req:path_info(Req),
  109. Filepath = filename:join([Dir|[escape_reserved(P, <<>>) || P <- PathInfo]]),
  110. Len = byte_size(Dir),
  111. case fullpath(Filepath) of
  112. << Dir:Len/binary, $/, _/binary >> ->
  113. init_info(Req, Filepath, HowToAccess, Extra);
  114. << Dir:Len/binary >> ->
  115. init_info(Req, Filepath, HowToAccess, Extra);
  116. _ ->
  117. {cowboy_rest, Req, error}
  118. end.
  119. %% We escape the slash found in path segments because
  120. %% a segment corresponds to a directory entry, and
  121. %% therefore those slashes are expected to be part of
  122. %% the directory name.
  123. %%
  124. %% Note that on most systems the slash is prohibited
  125. %% and cannot appear in filenames, which means the
  126. %% requested file will end up being not found.
  127. escape_reserved(<<>>, Acc) ->
  128. Acc;
  129. escape_reserved(<< $/, Rest/bits >>, Acc) ->
  130. escape_reserved(Rest, << Acc/binary, $\\, $/ >>);
  131. escape_reserved(<< C, Rest/bits >>, Acc) ->
  132. escape_reserved(Rest, << Acc/binary, C >>).
  133. fullpath(Path) ->
  134. fullpath(filename:split(Path), []).
  135. fullpath([], Acc) ->
  136. filename:join(lists:reverse(Acc));
  137. fullpath([<<".">>|Tail], Acc) ->
  138. fullpath(Tail, Acc);
  139. fullpath([<<"..">>|Tail], Acc=[_]) ->
  140. fullpath(Tail, Acc);
  141. fullpath([<<"..">>|Tail], [_|Acc]) ->
  142. fullpath(Tail, Acc);
  143. fullpath([Segment|Tail], Acc) ->
  144. fullpath(Tail, [Segment|Acc]).
  145. init_info(Req, Path, HowToAccess, Extra) ->
  146. Info = read_file_info(Path, HowToAccess),
  147. {cowboy_rest, Req, {Path, Info, Extra}}.
  148. read_file_info(Path, direct) ->
  149. case file:read_file_info(Path, [{time, universal}]) of
  150. {ok, Info} -> {direct, Info};
  151. Error -> Error
  152. end;
  153. read_file_info(Path, {archive, Archive}) ->
  154. case file:read_file_info(Archive, [{time, universal}]) of
  155. {ok, ArchiveInfo} ->
  156. %% The Erlang application archive is fine.
  157. %% Now check if the requested file is in that
  158. %% archive. We also need the file_info to merge
  159. %% them with the archive's one.
  160. PathS = binary_to_list(Path),
  161. case erl_prim_loader:read_file_info(PathS) of
  162. {ok, ContainedFileInfo} ->
  163. Info = fix_archived_file_info(
  164. ArchiveInfo,
  165. ContainedFileInfo),
  166. {archive, Info};
  167. error ->
  168. {error, enoent}
  169. end;
  170. Error ->
  171. Error
  172. end.
  173. fix_archived_file_info(ArchiveInfo, ContainedFileInfo) ->
  174. %% We merge the archive and content #file_info because we are
  175. %% interested by the timestamps of the archive, but the type and
  176. %% size of the contained file/directory.
  177. %%
  178. %% We reset the access to 'read', because we won't rewrite the
  179. %% archive.
  180. ArchiveInfo#file_info{
  181. size = ContainedFileInfo#file_info.size,
  182. type = ContainedFileInfo#file_info.type,
  183. access = read
  184. }.
  185. -ifdef(TEST).
  186. fullpath_test_() ->
  187. Tests = [
  188. {<<"/home/cowboy">>, <<"/home/cowboy">>},
  189. {<<"/home/cowboy">>, <<"/home/cowboy/">>},
  190. {<<"/home/cowboy">>, <<"/home/cowboy/./">>},
  191. {<<"/home/cowboy">>, <<"/home/cowboy/./././././.">>},
  192. {<<"/home/cowboy">>, <<"/home/cowboy/abc/..">>},
  193. {<<"/home/cowboy">>, <<"/home/cowboy/abc/../">>},
  194. {<<"/home/cowboy">>, <<"/home/cowboy/abc/./../.">>},
  195. {<<"/">>, <<"/home/cowboy/../../../../../..">>},
  196. {<<"/etc/passwd">>, <<"/home/cowboy/../../etc/passwd">>}
  197. ],
  198. [{P, fun() -> R = fullpath(P) end} || {R, P} <- Tests].
  199. good_path_check_test_() ->
  200. Tests = [
  201. <<"/home/cowboy/file">>,
  202. <<"/home/cowboy/file/">>,
  203. <<"/home/cowboy/./file">>,
  204. <<"/home/cowboy/././././././file">>,
  205. <<"/home/cowboy/abc/../file">>,
  206. <<"/home/cowboy/abc/../file">>,
  207. <<"/home/cowboy/abc/./.././file">>
  208. ],
  209. [{P, fun() ->
  210. case fullpath(P) of
  211. << "/home/cowboy/", _/bits >> -> ok
  212. end
  213. end} || P <- Tests].
  214. bad_path_check_test_() ->
  215. Tests = [
  216. <<"/home/cowboy/../../../../../../file">>,
  217. <<"/home/cowboy/../../etc/passwd">>
  218. ],
  219. [{P, fun() ->
  220. error = case fullpath(P) of
  221. << "/home/cowboy/", _/bits >> -> ok;
  222. _ -> error
  223. end
  224. end} || P <- Tests].
  225. good_path_win32_check_test_() ->
  226. Tests = case os:type() of
  227. {unix, _} ->
  228. [];
  229. {win32, _} ->
  230. [
  231. <<"c:/home/cowboy/file">>,
  232. <<"c:/home/cowboy/file/">>,
  233. <<"c:/home/cowboy/./file">>,
  234. <<"c:/home/cowboy/././././././file">>,
  235. <<"c:/home/cowboy/abc/../file">>,
  236. <<"c:/home/cowboy/abc/../file">>,
  237. <<"c:/home/cowboy/abc/./.././file">>
  238. ]
  239. end,
  240. [{P, fun() ->
  241. case fullpath(P) of
  242. << "c:/home/cowboy/", _/bits >> -> ok
  243. end
  244. end} || P <- Tests].
  245. bad_path_win32_check_test_() ->
  246. Tests = case os:type() of
  247. {unix, _} ->
  248. [];
  249. {win32, _} ->
  250. [
  251. <<"c:/home/cowboy/../../secretfile.bat">>,
  252. <<"c:/home/cowboy/c:/secretfile.bat">>,
  253. <<"c:/home/cowboy/..\\..\\secretfile.bat">>,
  254. <<"c:/home/cowboy/c:\\secretfile.bat">>
  255. ]
  256. end,
  257. [{P, fun() ->
  258. error = case fullpath(P) of
  259. << "c:/home/cowboy/", _/bits >> -> ok;
  260. _ -> error
  261. end
  262. end} || P <- Tests].
  263. -endif.
  264. %% Reject requests that tried to access a file outside
  265. %% the target directory.
  266. -spec malformed_request(Req, State)
  267. -> {boolean(), Req, State}.
  268. malformed_request(Req, State) ->
  269. {State =:= error, Req, State}.
  270. %% Directories, files that can't be accessed at all and
  271. %% files with no read flag are forbidden.
  272. -spec forbidden(Req, State)
  273. -> {boolean(), Req, State}
  274. when State::state().
  275. forbidden(Req, State={_, {_, #file_info{type=directory}}, _}) ->
  276. {true, Req, State};
  277. forbidden(Req, State={_, {error, eacces}, _}) ->
  278. {true, Req, State};
  279. forbidden(Req, State={_, {_, #file_info{access=Access}}, _})
  280. when Access =:= write; Access =:= none ->
  281. {true, Req, State};
  282. forbidden(Req, State) ->
  283. {false, Req, State}.
  284. %% Detect the mimetype of the file.
  285. -spec content_types_provided(Req, State)
  286. -> {[{binary(), get_file}], Req, State}
  287. when State::state().
  288. content_types_provided(Req, State={Path, _, Extra}) ->
  289. case lists:keyfind(mimetypes, 1, Extra) of
  290. false ->
  291. {[{cow_mimetypes:web(Path), get_file}], Req, State};
  292. {mimetypes, Module, Function} ->
  293. {[{Module:Function(Path), get_file}], Req, State};
  294. {mimetypes, Type} ->
  295. {[{Type, get_file}], Req, State}
  296. end.
  297. %% Detect the charset of the file.
  298. -spec charsets_provided(Req, State)
  299. -> {[binary()], Req, State}
  300. when State::state().
  301. charsets_provided(Req, State={Path, _, Extra}) ->
  302. case lists:keyfind(charset, 1, Extra) of
  303. %% We simulate the callback not being exported.
  304. false ->
  305. no_call;
  306. {charset, Module, Function} ->
  307. {[Module:Function(Path)], Req, State};
  308. {charset, Charset} ->
  309. {[Charset], Req, State}
  310. end.
  311. %% Assume the resource doesn't exist if it's not a regular file.
  312. -spec resource_exists(Req, State)
  313. -> {boolean(), Req, State}
  314. when State::state().
  315. resource_exists(Req, State={_, {_, #file_info{type=regular}}, _}) ->
  316. {true, Req, State};
  317. resource_exists(Req, State) ->
  318. {false, Req, State}.
  319. %% Generate an etag for the file.
  320. -spec generate_etag(Req, State)
  321. -> {{strong | weak, binary()}, Req, State}
  322. when State::state().
  323. generate_etag(Req, State={Path, {_, #file_info{size=Size, mtime=Mtime}},
  324. Extra}) ->
  325. case lists:keyfind(etag, 1, Extra) of
  326. false ->
  327. {generate_default_etag(Size, Mtime), Req, State};
  328. {etag, Module, Function} ->
  329. {Module:Function(Path, Size, Mtime), Req, State};
  330. {etag, false} ->
  331. {undefined, Req, State}
  332. end.
  333. generate_default_etag(Size, Mtime) ->
  334. {strong, integer_to_binary(erlang:phash2({Size, Mtime}, 16#ffffffff))}.
  335. %% Return the time of last modification of the file.
  336. -spec last_modified(Req, State)
  337. -> {calendar:datetime(), Req, State}
  338. when State::state().
  339. last_modified(Req, State={_, {_, #file_info{mtime=Modified}}, _}) ->
  340. {Modified, Req, State}.
  341. %% Stream the file.
  342. -spec get_file(Req, State)
  343. -> {{sendfile, 0, non_neg_integer(), binary()}, Req, State}
  344. when State::state().
  345. get_file(Req, State={Path, {direct, #file_info{size=Size}}, _}) ->
  346. {{sendfile, 0, Size, Path}, Req, State};
  347. get_file(Req, State={Path, {archive, _}, _}) ->
  348. PathS = binary_to_list(Path),
  349. {ok, Bin, _} = erl_prim_loader:get_file(PathS),
  350. {Bin, Req, State}.