All guides
Developer Tools

How to Use a Localhost Tunnel Safely

Learn how to expose your local development server to the internet safely, with best practices for authentication, access control, and cleanup.

Published September 29, 2024

A localhost tunnel exposes your local development server to the internet via a public URL. This is useful for testing webhooks, sharing work-in-progress with clients, and connecting external services to your local environment. However, exposing a local server to the public internet carries inherent risk. This guide covers best practices for using localhost tunnels safely, from authentication to cleanup.

What is a localhost tunnel?

A localhost tunnel is a tool that creates a connection between your local development server and a public URL. When someone visits the public URL, the tunnel server forwards the request to your local machine, which processes it and sends the response back through the tunnel. This makes your local server accessible from anywhere on the internet without deploying it.

Tools like Explorme Tunnel at tunnel.explorme.com provide this functionality using a CLI client and a WebSocket-based transport. The client runs on your machine and maintains a persistent connection to the tunnel server, which forwards incoming HTTP requests to your local port.

Why use a localhost tunnel

Localhost tunnels are useful for several development scenarios. Testing webhook integrations requires a public URL that external services can call. Sharing a work-in-progress project with a client or teammate is easier with a public URL than with screen sharing. Running a local API server that needs a public callback URL for OAuth flows requires a tunnel. Connecting a mobile app to a local backend during development is simpler with a tunnel.

In all these cases, the tunnel provides a temporary public URL that you can use for the duration of the development task and then shut down. This is faster and more convenient than deploying the application to a staging server.

Security risks of exposing localhost

Exposing a local server to the public internet carries several risks. Anyone who knows the tunnel URL can access your local server, which may expose sensitive data or development endpoints that are not secured for public access. If your local server has debugging endpoints, database administration interfaces, or other sensitive features, these become accessible through the tunnel.

The tunnel server itself is another consideration. Traffic flowing through the tunnel passes through the tunnel server infrastructure. When using a hosted tunnel service, you need to trust the service provider. When self-hosting the tunnel server, you control the infrastructure but are responsible for its security.

Best practices for safe tunneling

Always enable authentication on your tunnel. Use token-based authentication to prevent unauthorized access. Even if the tunnel URL is hard to guess, authentication adds a critical layer of security.

Limit the exposed port to only what is needed. Do not expose your entire development environment. If you only need to test a web server on port 3000, tunnel only that port.

Avoid exposing sensitive services. Database administration interfaces, debugging endpoints, and development tools should not be accessible through a public tunnel. If they must be exposed, ensure they are protected with their own authentication.

Use HTTPS for the tunnel URL. Most tunnel tools provide HTTPS URLs, which encrypt the traffic between the visitor and the tunnel server. This protects credentials and sensitive data in transit.

Shut down the tunnel when you are done. Do not leave tunnels running indefinitely. Each active tunnel is a potential attack surface. Close it as soon as the development task is complete.

Step-by-step: starting a tunnel

1. Install the tunnel CLI tool, such as Explorme Tunnel at tunnel.explorme.com.

2. Start your local development server on a specific port (for example, port 3000).

3. Run the tunnel command, specifying the local port: tunnel start --port 3000.

4. The tool creates a tunnel and displays the public URL.

5. If authentication is available, enable it to protect the tunnel from unauthorized access.

6. Share the public URL with your client, or use it as the webhook URL for external services.

7. When you are done, stop the tunnel with the stop command or by closing the terminal.

When to shut down a tunnel

Shut down a tunnel as soon as the development task is complete. Do not leave tunnels running overnight or over weekends. Each active tunnel is a potential entry point to your local machine. Closing the tunnel when it is no longer needed is the simplest and most effective security measure.

If you need persistent access for testing, consider deploying the application to a staging server instead of using a long-running tunnel. Staging servers are designed for public access and typically have better security controls than a local development server.

Common mistakes to avoid

  • Leaving tunnels running when they are no longer needed. Shut down tunnels as soon as the task is complete.
  • Not enabling authentication. Always use token-based authentication to prevent unauthorized access.
  • Exposing sensitive services like database admin interfaces. Only tunnel the specific port you need.
  • Using HTTP instead of HTTPS for the tunnel URL. Use HTTPS to encrypt traffic in transit.
  • Sharing the tunnel URL publicly. Only share it with the people who need it, and revoke access when done.

FAQ

Looking for more tools? Explore our Developer Tools category.