Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow setting environment variables from the URL #371

Closed
billchurch opened this issue Aug 31, 2024 · 1 comment
Closed

feat: Allow setting environment variables from the URL #371

billchurch opened this issue Aug 31, 2024 · 1 comment

Comments

@billchurch
Copy link
Owner

via @israel-tsadok-silk from #368

Allow setting environment variables from the URL. I would like to be able to create a link that would open a session and (for example) immediately open vim on some file. I think the best way to implement this is to allow a URL like /ssh/host?env=VIM_FILE:somefile.config and then use that env var in .bashrc to decide what to do.

@billchurch
Copy link
Owner Author

billchurch commented Nov 30, 2024

Environment Variables via URL

WebSSH2 supports passing environment variables through URL parameters, allowing you to customize the SSH session environment. This feature enables scenarios like automatically opening specific files or setting custom environment variables.

Server Configuration

Before using this feature, you must configure your SSH server to accept the environment variables you want to pass. Edit your /etc/ssh/sshd_config file to include the desired variables in the AcceptEnv directive:

# Allow client to pass locale environment variables and custom vars
AcceptEnv LANG LC_* VIM_FILE CUSTOM_ENV

Remember to restart your SSH server after making changes:

sudo systemctl restart sshd  # For systemd-based systems
# or
sudo service sshd restart   # For init.d-based systems

Usage

Pass environment variables using the env query parameter:

# Single environment variable
http://localhost:2222/ssh/host/example.com?env=VIM_FILE:config.txt

# Multiple environment variables
http://localhost:2222/ssh/host/example.com?env=VIM_FILE:config.txt,CUSTOM_ENV:test

Security Considerations

To maintain security, environment variables must meet these criteria:

  • Variable names must:
    • Start with a capital letter
    • Contain only uppercase letters, numbers, and underscores
    • Be listed in the SSH server's AcceptEnv directive
  • Variable values cannot contain shell special characters (;, &, |, `, $)

Invalid environment variables will be silently ignored.

Example Usage

  1. Configure your SSH server as shown above.

  2. Create a URL with environment variables:

    http://localhost:2222/ssh/host/example.com?env=VIM_FILE:settings.conf,CUSTOM_ENV:production
    
  3. In your remote server's .bashrc or shell initialization file:

    if [ ! -z "$VIM_FILE" ]; then
      vim "$VIM_FILE"
    fi
    
    if [ ! -z "$CUSTOM_ENV" ]; then
      echo "Running in $CUSTOM_ENV environment"
    fi

Troubleshooting

If environment variables aren't being set:

  1. Verify the variables are permitted in /etc/ssh/sshd_config
  2. Check SSH server logs for any related errors
  3. Ensure variable names and values meet the security requirements
  4. Test with a simple variable first to isolate any issues

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

No branches or pull requests

1 participant