Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/api/notes_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,14 @@ func (h *notesHandlers) importTar(w http.ResponseWriter, r *http.Request) {
fmt.Sprintf("too many entries (cap %d)", MaxImportEntries), nil)
return
}
// Clean is for normalisation here, not the security boundary.
// The traversal defence is the explicit reject below (HasPrefix
// "/" + Contains "..") followed by an absolute-path containment
// check after filepath.Join into notesDir. All three layers must
// fail simultaneously for a traversal to land — Semgrep can't see
// the multi-statement defence, so the rule is suppressed inline.
// nosemgrep: go.lang.security.filepath-clean-misuse.filepath-clean-misuse
name := filepath.ToSlash(filepath.Clean(hdr.Name))
// Reject traversal / absolute paths in archive entries.
if strings.HasPrefix(name, "/") || strings.Contains(name, "..") {
writeError(w, r, http.StatusForbidden,
"tar entry traversal: "+hdr.Name, nil)
Expand All @@ -496,7 +502,6 @@ func (h *notesHandlers) importTar(w http.ResponseWriter, r *http.Request) {
continue
}
dest := filepath.Join(notesDir, filepath.FromSlash(name))
// Double-check containment (defense in depth).
absDest, _ := filepath.Abs(dest)
absBase, _ := filepath.Abs(notesDir)
if !strings.HasPrefix(absDest, absBase+string(os.PathSeparator)) {
Expand Down
7 changes: 7 additions & 0 deletions internal/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ func spaHandler(assets fs.FS, _ *config.Config) http.Handler {
return
}

// path.Clean here normalises a URL path for SPA-vs-asset
// classification — it is not the security boundary. The handler
// only reads from `assets fs.FS`, which is fs.Sub(embed.FS, "dist"):
// io/fs rejects any path containing ".." via fs.ValidPath, and
// embed.FS holds only compile-time files. Path traversal cannot
// reach the host filesystem regardless of what cleanPath becomes.
// nosemgrep: go.lang.security.filepath-clean-misuse.filepath-clean-misuse
cleanPath := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
if cleanPath == "." || cleanPath == "" {
cleanPath = "index.html"
Expand Down
Loading