Rewriting a Spring Boot + Thymeleaf app in Go
· 4 min
gospring-bootsecuritysqliteweb
VetPet is a small veterinary clinic application: owners and their pets, with search, create, edit and delete screens behind a login. It was a Spring Boot and Thymeleaf application. I rewrote it in Go and kept the pages the same. This post is about what that move looks like in a real, if small, web application.
Run it
git clone https://github.com/ergulcu/vetpet
cd vetpet
go run ./cmd/vetpet # http://localhost:8080, data in memory
docker compose up --build # data in a volume
There are two demo accounts: admin / admin, who can delete, and user / user, who can read, create and edit. They are demo passwords and the log warns while they are in use:
level=WARN msg="demo passwords are in use; set VETPET_SECURITY_ADMIN_PASSWORD and VETPET_SECURITY_USER_PASSWORD"
level=INFO msg="the data is kept in memory; set VETPET_DATABASE to keep it in a file"
Set VETPET_DATABASE=./vetpet.db to keep the data in a SQLite file. Without it, the data is in memory and gone after a restart, as with H2 in the Java version.
One binary
The result of go build is a single executable of about 20 MB on Windows. It contains the web server, the HTML templates, Bootstrap 5 and the database driver. SQLite comes from the pure Go modernc.org/sqlite, so no C compiler is involved, and there is no Java runtime to install next to it. In the Java version Bootstrap came from a WebJar; now it is embedded in the binary.
The security, checked from the outside
A web application's security is only worth what it does when someone pushes on it. I started the binary and pushed, with a small script that acts like a browser (it keeps cookies and does not follow redirects, so I can see each answer):
1. owner list without login -> 303 Location: /login
2. wrong password -> 303 Location: /login?error
3. admin/admin -> 303 Location: /
Set-Cookie: vetpet_session=<id>; Path=/; HttpOnly; SameSite=Lax
4. create owner "Şükrü Yılmaz" -> 303 Location: /owner/list
Every page except the login page, the static files and /healthz needs a signed-in user, and the session cookie is HttpOnly and SameSite=Lax. Behind that, the session is kept on the server, a new session id is issued at every sign-in, and passwords are stored as bcrypt hashes.
Roles. A user signs in, creates an owner, and then tries to delete one:
8. user creates an owner -> 303 Location: /owner/list
9. user tries to delete owner 1 -> 403 (access denied)
10. admin deletes owner 1 -> 303 Location: /owner/list
Deleting is a POST and only for admins; everyone else gets the access denied page.
Another site making your browser post. This is the classic CSRF attack: a page on another site makes your browser submit a form to the application, with your cookie attached. Java frameworks usually answer with a token hidden in every form. Go's standard library has CrossOriginProtection, which looks at the Sec-Fetch-Site and Origin headers that browsers send, so the pages carry no token field at all. I sent the same create request, once as a normal request and once marked as coming from another site:
6. POST /owner/create, Sec-Fetch-Site: cross-site, Origin: https://evil.example -> 403
was "Evil Owner" saved? false
It was refused and nothing was saved. This protection relies on the browser sending those headers, which modern browsers do; an old browser that does not would fall outside it.
Small differences, on purpose
- Search ignores case for every letter, including the Turkish ones. I searched
şükandŞÜKand both foundŞükrü Yılmaz. - Owners with pets. As before, an owner who still has pets is kept with a message. Now a foreign key also refuses it in the database, so no code path can get around it.
- Data can be kept in a file. The Java version kept its data in memory (H2).
Tests that behave like a browser
go test ./...
The web tests drive the application over HTTP with a cookie jar, the way a browser does: login, the forms and their errors, the admin-only delete, cross-site posts and escaping. They test what a user could actually do, not the internals.
Source code: github.com/ergulcu/vetpet