Skip to content

Instantly share code, notes, and snippets.

@qbit
Created December 5, 2022 21:24
Show Gist options
  • Save qbit/88590a3fabb87297f69d4759bb917e3c to your computer and use it in GitHub Desktop.
Save qbit/88590a3fabb87297f69d4759bb917e3c to your computer and use it in GitHub Desktop.
AI generated web server that is restricted to /tmp on OpenBSD and Linux
package main
import (
"log"
"net/http"
"os"
"golang.org/x/sys/unix"
)
func main() {
// Use OpenBSD's unveil system call to restrict file access to /tmp.
if err := unix.Unveil("/tmp", "r"); err != nil {
log.Fatal(err)
}
if err := unix.UnveilBlock(); err != nil {
log.Fatal(err)
}
// Use Linux's landlock system call to restrict visibility to /tmp.
if err := unix.Landlock(0, unix.MCL_CURRENT|unix.MCL_FUTURE, unix.LANDLOCK_VERBOSE); err != nil {
log.Fatal(err)
}
// Serve static files from /tmp.
http.Handle("/", http.FileServer(http.Dir("/tmp")))
// Listen and serve on localhost:8080.
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment