Skip to content

CGI process current working directory #25

Description

@kalvdans

The README states:

The current working directory will be the same as Caddy itself.

But when testing, the CGI process's CWD is set to the directory containing the CGI script, not to Caddy's CWD.

Steps to reproduce

  1. Create this directory structure:
/tmp/cgi-bug-report/
├── Caddyfile
└── subdir/
    └── test.sh
  1. Create /tmp/cgi-bug-report/subdir/test.sh:
#!/bin/sh
printf "Content-type: text/plain\r\n\r\n"
printf "CWD: %s\n" "$(pwd)"
chmod +x /tmp/cgi-bug-report/subdir/test.sh
  1. Create /tmp/cgi-bug-report/Caddyfile:
{
    debug
    auto_https disable_redirects
}

http://localhost:8080 {
    cgi / subdir/test.sh
}
  1. Run caddy from /tmp/cgi-bug-report:
cd /tmp/cgi-bug-report && caddy run --config Caddyfile
  1. curl http://localhost:8080/

Expected output

CWD should be /tmp/cgi-bug-report (Caddy's CWD).

Actual output

CWD: /tmp/cgi-bug-report/subdir

CWD is the script's directory, not Caddy's CWD.

Discussion

Adding dir . to the cgi block fixes it:

cgi / subdir/test.sh {
    dir .
}

Then output is:

CWD: /tmp/cgi-bug-report

I suggest that we fix the documentation to say that the cwd defaults to the script's directory, and that dir argument is relative to the cwd that caddy was started from.

Environment

  • caddy-cgi v2.2.7
  • Caddy v2.x
  • Go 1.21+

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions