Skip to content
TheChatArchiveThe Chat Archive
Geminigemini-2.0-flash-001· GoogleHugging Face LMArena Human Preference 140K

can erlang cowboy be used as a directory index fileserver?

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4.1-2025-04-14’s answer.
  1. User

    can erlang cowboy be used as a directory index fileserver?

  2. Gemini Assistant

    Yes, Erlang Cowboy can be used as a directory index fileserver. While Cowboy itself doesn't have built-in directory indexing functionality, it provides the tools and flexibility to build it relatively easily. Here's how you can approach it:

    Core Concept:

    You'll need to implement a request handler in Cowboy that intercepts requests for directories. This handler will:

    1. Check if the requested path is a directory.
    2. If it is, read the directory contents.
    3. Generate an HTML page listing the files and subdirectories.
    4. Send the HTML page as a response to the client.

    Key Erlang Modules & Libraries:

    • file: Erlang's built-in module for interacting with the filesystem. You'll use functions like file:list_dir/1 to get the contents of a directory and file:is_dir/1 to check if a path is a directory.
    • cowboy: Of course! This is your HTTP server.
    • cowlib: Cowboy's companion library, providing useful utilities like response building.
    • string or re (regular expressions): For manipulating strings and potentially cleaning up filenames for display.
    • html_safe (from the mochiweb or similar library): Crucial for properly escaping HTML to prevent cross-site scripting (XSS) vulnerabilities. Don't construct HTML strings without proper escaping!
    • Template Engine (Optional): While not strictly required, using a template engine like EEx (Erlang Embedded Erlang) or Mustache can make generating the HTML cleaner and more maintainable.

    Example Implementation (Conceptual - Requires Adaptation and Security Review):

    -module(index_handler).
    -export([init/2, handle/2, terminate/3]).
    
    init(_Req, State) ->
        {cowboy_websocket, undefined, State}. % Change to {cowboy_rest, ...} if not websocket
    
    handle(Req, State) ->
        {ok, Path} = cowboy_req:binding(path, Req), % Assuming you have a route that captures the path
        FullPath = filename:join(<<"/path/to/your/root/directory">>, Path), % Adjust this path!
    
        case filelib:is_dir(binary_to_list(FullPath)) of
            true ->
                Files = get_directory_listing(FullPath),
                Html = generate_html(Files, Path),  % Path here is the URL path
                {ok, Req2} = cowboy_req:reply(200,
                                              [{<<"content-type">>, <<"text/html; charset=utf-8">>}],
                                              Html,
                                              Req),
                {ok, Req2, State};
            false ->
                % Delegate to Cowboy's file serving logic or return a 404.
                %  Important:  You *must* ensure security here so that
                %  users can't escape your root directory using path traversal (e.g., "..").
                %  This often involves creating a route that *specifically* serves files
                %  from within your allowed directory.
                {halt, Req, State}  % or {cowboy_static, file, ...} if using static serving.
        end.
    
    terminate(_Reason, _Req, _State) ->
        ok.
    
    get_directory_listing(FullPath) ->
        case file:list_dir(binary_to_list(FullPath)) of
            {ok, Entries} -> Entries;
            {error, Reason} ->
                io:format("Error listing directory: ~p~n", [Reason]),
                []  % Or handle the error more gracefully.
        end.
    
    generate_html(Files, Path) ->
        % **IMPORTANT: Properly escape filenames and links to prevent XSS!**
        Header = "<html><head><title>Index of " ++ binary_to_list(Path) ++ "</title></head><body>",
        Links = lists:foldl(fun(File, Acc) ->
                               EscapedFile = html_safe:escape(File), % Properly escape for HTML
                               EscapedLink = html_safe:escape(filename:join(Path, File)),  %Properly escape for URL in link
                               Acc ++ "<a href='" ++ binary_to_list(EscapedLink) ++ "'>" ++ binary_to_list(EscapedFile) ++ "</a><br/>"
                           end,
                           "",
                           Files),
        Footer = "</body></html>",
        Header ++ Links ++ Footer.
    
    
    % Example Cowboy Route configuration (in your router.erl):
    % { "_/path/to/serve/[...path]", index_handler, [] }.  // Adjust path as needed.  The [...path] makes this a catch-all route.
    

    Explanation and Important Considerations:

    • init/2, handle/2, terminate/3: These are the standard callbacks for a Cowboy handler.
    • Path Traversal Protection: The code above is very basic and vulnerable to path traversal attacks if you're not extremely careful. A malicious user could request /../../etc/passwd if you haven't secured your path handling. You MUST implement proper input validation and sanitization.
      • Whitelist Approach: The safest approach is to have a whitelist of allowed directories and files. Refuse to serve anything outside those.
      • Path Normalization: Use filename:absname/1 or similar functions to normalize the path and remove .. components. Then, carefully check that the normalized path still falls within your allowed root directory.
    • Error Handling: The example has basic error handling. You'll need to add more robust error handling, logging, and potentially user-friendly error pages.
    • Security:
      • XSS Prevention: Use a library like html_safe from mochiweb (or a similar HTML escaping library) to properly escape filenames when generating the HTML. This prevents malicious filenames from injecting JavaScript into your directory listing page.
      • CSRF Protection (if applicable): If you have any forms on your page, implement CSRF protection.
    • File Serving: The example only covers directory listing. To serve actual files, you'll likely need to delegate to Cowboy's built-in static file serving (using cowboy_static) after verifying the path and ensuring it's safe. Alternatively, you could read the file contents and send them as a response.
    • Performance: For large directories, listing all files can be slow. Consider:
      • Caching: Cache the directory listings.
      • Pagination: Display the directory listing in pages.
      • Async Operations: Use asynchronous operations to avoid blocking the Cowboy handler while reading the directory.
    • Customization: You can customize the HTML generation to:
      • Add icons for different file types.
      • Display file sizes and modification times.
      • Implement sorting.
      • Use a more sophisticated template engine.
    • MIME Types: Make sure to set the correct Content-Type header for the HTML response (text/html; charset=utf-8) and for any files you serve.

    Simplified Example with cowboy_static (Requires Careful Path Validation):

    This example shows how to combine directory indexing with Cowboy's built-in static file serving after carefully validating the path. This approach requires rigorous checks to prevent path traversal.

    -module(index_handler).
    -export([init/2, handle/2, terminate/3]).
    
    -define(ROOT_DIR, "/path/to/your/root/directory").  % Change this!
    
    init(_Req, State) ->
        {cowboy_rest, undefined, State}.
    
    handle(Req, State) ->
        {ok, Path} = cowboy_req:binding(path, Req),
        FullPath = filename:join(?ROOT_DIR, Path),
    
        case is_valid_path(FullPath) of
            true ->
                case filelib:is_dir(binary_to_list(FullPath)) of
                    true ->
                        Files = get_directory_listing(FullPath,Path),  %Pass URL Path
                        Html = generate_html(Files, Path),
                        {ok, Req2} = cowboy_req:reply(200,
                                                       [{<<"content-type">>, <<"text/html; charset=utf-8">>}],
                                                       Html,
                                                       Req),
                        {ok, Req2, State};
                    false ->
                        % Serve the file using cowboy_static
                        {ok, Req2, State2} = cowboy_static:deliver(Req, State, FullPath),
                        {ok, Req2, State2};
                end;
            false ->
                % Invalid path: return a 403 Forbidden error
                {ok, Req2} = cowboy_req:reply(403, [], <<"Forbidden">>, Req),
                {ok, Req2, State}
        end.
    
    terminate(_Reason, _Req, _State) ->
        ok.
    
    is_valid_path(Path) ->
        AbsPath = filename:absname(Path), % Normalize path
        string:prefix(AbsPath, ?ROOT_DIR).  % MUST be a prefix.  DO NOT USE string:equal
                                        % If a user could get to files *adjacent* to the root dir, that is bad.
    
    
    get_directory_listing(FullPath,Path) -> %Path is URL Path
        case file:list_dir(binary_to_list(FullPath)) of
            {ok, Entries} -> Entries;
            {error, Reason} ->
                io:format("Error listing directory: ~p~n", [Reason]),
                []
        end.
    
    
    generate_html(Files, Path) ->
        Header = "<html><head><title>Index of " ++ binary_to_list(Path) ++ "</title></head><body>",
        Links = lists:foldl(fun(File, Acc) ->
                               EscapedFile = html_safe:escape(File),
                               EscapedLink = html_safe:escape(filename:join(Path, File)),
                               Acc ++ "<a href='" ++ binary_to_list(EscapedLink) ++ "'>" ++ binary_to_list(EscapedFile) ++ "</a><br/>"
                           end,
                           "",
                           Files),
        Footer = "</body></html>",
        Header ++ Links ++ Footer.
    
    
    
    % Example Cowboy Route (router.erl):
    % { "_/serve/[...path]", index_handler, [] }.
    

    Key Points About the cowboy_static Example:

    • is_valid_path/1 is CRITICAL: This function must thoroughly validate the path to prevent path traversal vulnerabilities. It must normalize the path and ensure it's a subdirectory of your allowed root. Using string:prefix/2 on the absolute path is generally a safe way to do this, but other methods are possible. DO NOT use string:equal to compare to the full path; this would allow access to files outside the root if the user types the exact path, including trailing slashes.
    • Route Configuration: The route is set up to catch-all paths under /serve/.
    • cowboy_static:deliver/3: This function is used to serve the file once you've validated that it's safe.
    • 403 Forbidden: If the path is invalid, a 403 Forbidden error is returned.

    In summary: Cowboy is perfectly capable of being a directory index fileserver. The challenge lies in implementing the directory listing logic and, most importantly, ensuring that your implementation is secure against path traversal and XSS vulnerabilities. Pay close attention to input validation, sanitization, and HTML escaping. Use a template engine for more maintainable HTML generation. Consider caching and pagination for performance with large directories.

  3. User

    Show a basic, but complete implementation. Include all build steps including rebar config files and steps to run program. files will be in ./public/ relative to the application base directory.

  4. Gemini Assistant

    Okay, here's a complete, basic (but functional) implementation of a directory index file server using Cowboy. This includes the rebar.config, source files, and instructions for building and running. WARNING: This is still a simplified example and requires careful review and hardening for production use, especially path validation.

    1. Project Structure:

    my_index_server/
    ├── rebar.config
    ├── src/
    │   ├── application.erl
    │   ├── handler.erl
    │   └── router.erl
    └── public/  (Empty initially, create this directory)
    

    2. rebar.config:

    {erl_opts, [debug_info]}.
    {deps, [
        {cowboy, "2.10.0"} % Or a more recent version
    ]}.
    
    {project_applications, [my_index_server]}.
    

    3. src/application.erl:

    -module(application).
    -behaviour(application).
    
    -export([start/2, stop/1]).
    
    start(_StartType, _StartArgs) ->
        Dispatch = router:routes(),
        {ok, _} = cowboy:start_clear(http_listener,
                                     [{port, 8080}],
                                     #{env => #{dispatch => Dispatch}}),
        my_index_server_sup:start_link().
    
    stop(_State) ->
        ok.
    

    4. src/handler.erl:

    -module(handler).
    -export([init/2, handle/2, terminate/3]).
    
    -define(ROOT_DIR, "./public"). % Relative to app base. Change to an absolute path for more security!
    
    init(_Req, State) ->
        {cowboy_rest, undefined, State}.
    
    handle(Req, State) ->
        {ok, Path} = cowboy_req:binding(path, Req),
        FullPath = filename:join(?ROOT_DIR, Path),
    
        case is_valid_path(FullPath) of
            true ->
                case filelib:is_dir(binary_to_list(FullPath)) of
                    true ->
                        Files = get_directory_listing(FullPath, Path),
                        Html = generate_html(Files, Path),
                        {ok, Req2} = cowboy_req:reply(200,
                                                       [{<<"content-type">>, <<"text/html; charset=utf-8">>}],
                                                       Html,
                                                       Req),
                        {ok, Req2, State};
                    false ->
                        {ok, Req2, State2} = cowboy_static:deliver(Req, State, FullPath),
                        {ok, Req2, State2};
                end;
            false ->
                {ok, Req2} = cowboy_req:reply(403, [], <<"Forbidden">>, Req),
                {ok, Req2, State}
        end.
    
    terminate(_Reason, _Req, _State) ->
        ok.
    
    is_valid_path(Path) ->
        AbsPath = filename:absname(Path),
        string:prefix(AbsPath, filename:absname(?ROOT_DIR)).
    
    get_directory_listing(FullPath, Path) ->
        case file:list_dir(binary_to_list(FullPath)) of
            {ok, Entries} -> Entries;
            {error, Reason} ->
                io:format("Error listing directory: ~p~n", [Reason]),
                []
        end.
    
    
    generate_html(Files, Path) ->
        Header = "<html><head><title>Index of " ++ binary_to_list(Path) ++ "</title></head><body>",
        Links = lists:foldl(fun(File, Acc) ->
                               EscapedFile = html_safe:escape(File), %Requires mochiweb
                               EscapedLink = html_safe:escape(filename:join(Path, File)), %Requires mochiweb
                               Acc ++ "<a href='" ++ binary_to_list(EscapedLink) ++ "'>" ++ binary_to_list(EscapedFile) ++ "</a><br/>"
                           end,
                           "",
                           Files),
        Footer = "</body></html>",
        Header ++ Links ++ Footer.
    

    5. src/router.erl:

    -module(router).
    -export([routes/0]).
    
    routes() ->
        [
            {'_/public/[...path]', handler, []},  % Serve from ./public
            {'_/[...path]', handler, []}  %Catch all for serving top level from public
        ].
    

    6. src/my_index_server_sup.erl:

    -module(my_index_server_sup).
    -behaviour(supervisor).
    -export([start_link/0, init/1]).
    
    start_link() ->
      supervisor:start_link({local, ?MODULE}, ?MODULE, []).
    
    init([]) ->
      SupFlags = {one_for_one, 5, 10},
      ChildSpecs = [
        %% Add your child processes here.  This simple example has none.
      ],
      {ok, {SupFlags, ChildSpecs}}.
    

    7. Add html_safe.erl (XSS protection):

    Since the handler.erl code depends on the html_safe:escape/1 function from mochiweb it needs to be included. Because adding an entire mochiweb dependency is heavy-handed, here's the minimum code to make it work:

    -module(html_safe).
    -export([escape/1]).
    
    escape(String) when is_binary(String) ->
        escape(binary_to_list(String));
    escape(String) when is_list(String) ->
        lists:flatten([convert(Char) || Char <- String]).
    
    convert(Quote) when Quote == $\" -> "&quot;";
    convert(Apostrophe) when Apostrophe == $\' -> "&#39;";
    convert(Amp) when Amp == $& -> "&amp;";
    convert(LessThan) when LessThan == $< -> "&lt;";
    convert(GreaterThan) when GreaterThan == $> -> "&gt;";
    convert(Other) -> Other.
    

    Save this as src/html_safe.erl.

    Build and Run:

    1. Install Rebar3: If you don't have it, install Rebar3. See https://www.rebar3.org/.

    2. Create the public/ directory: mkdir public

    3. Build: Run rebar3 compile in the my_index_server directory. This will download dependencies and compile the Erlang code.

    4. Start the application:

      • Run rebar3 shell (or erl -pa _build/default/lib/*/ebin if you prefer not to use rebar3 shell).
      • In the Erlang shell, type application:start(my_index_server).
    5. Test:

      • Create some files and subdirectories inside the public/ directory. For example:
        mkdir public/subdir
        touch public/file1.txt
        touch public/subdir/file2.html
        echo "<h1>Hello World</h1>" > public/subdir/file2.html
        
      • Open your web browser and go to http://localhost:8080/. You should see a directory listing for the public/ directory.
      • Navigate to http://localhost:8080/subdir/. You should see a directory listing for the public/subdir/ directory.
      • Navigate to http://localhost:8080/subdir/file2.html. You should see the HTML file rendered.

    Important Considerations and Security Notes:

    • ROOT_DIR and Path Validation: The is_valid_path/1 function and the ROOT_DIR definition are the most critical parts for security.
      • Using a relative path for ROOT_DIR (as shown here) is generally discouraged for production environments because the application's working directory can be unpredictable. An absolute path is much safer.
      • is_valid_path/1 must prevent path traversal attacks. The provided implementation is basic and might not be sufficient for all situations. Carefully review it and consider more robust validation methods. It currently normalizes the path and makes sure that is starts with the correct prefix. Consider making the check use the binary type for performance reasons if it's a concern
    • HTML Escaping: The html_safe:escape/1 function is essential to prevent XSS vulnerabilities. Make sure to escape all user-provided data (in this case, filenames) before including them in the HTML.
    • Error Handling: The code includes basic error handling, but you should add more robust logging and error reporting.
    • MIME Types: While cowboy_static generally handles MIME types correctly, double-check that the correct Content-Type header is being sent for files.
    • Content Security Policy (CSP): Consider using a Content Security Policy (CSP) header to further mitigate XSS risks.
    • Authentication/Authorization: This example provides no authentication or authorization. If you need to restrict access to certain files or directories, you'll need to implement appropriate authentication and authorization mechanisms.
    • Dependencies: Make sure to keep your dependencies up-to-date to benefit from security fixes.
    • Testing: Thoroughly test your implementation for security vulnerabilities. Use tools like OWASP ZAP or Burp Suite to identify potential issues.
    • Root path routing: Add routes like the {'/[...path]', handler, []} after the route that's more specific. Otherwise, the route will catch everything.

    This comprehensive example provides a good starting point, but remember that security is paramount. Carefully review and adapt the code to meet your specific needs and security requirements. Always err on the side of caution when handling user-provided data and file paths.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.