Created
December 5, 2022 21:24
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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