Skip to content

🐛 Fix static middleware subdirectory browsing redirects losing URL prefix#3668

Closed
Copilot wants to merge 4 commits intomainfrom
copilot/fix-3667
Closed

🐛 Fix static middleware subdirectory browsing redirects losing URL prefix#3668
Copilot wants to merge 4 commits intomainfrom
copilot/fix-3667

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Aug 8, 2025

The static middleware with directory browsing enabled was incorrectly handling redirects for subdirectories when URL prefixes were used. When clicking on subdirectory links in the directory listing, the redirect would lose the URL prefix, breaking navigation.

Problem

Consider a Fiber app serving static files with browsing:

app.Get("/static*", static.New("./public", static.Config{Browse: true}))

Before this fix:

  1. Visit /static → shows directory listing with "js" folder
  2. Click "js" folder → redirects to /js/ (loses /static prefix) → 404 error

Root cause: When fasthttp.FS generates redirects for directory access (to add trailing slashes), it uses the path-rewritten version without the original URL prefix context.

Solution

This fix intercepts redirects generated by fasthttp.FS and corrects the Location header to preserve the URL prefix:

  • /static/js → redirects to /static/js/ (preserves prefix)
  • /static/subfolder → redirects to /static/subfolder/ (preserves prefix)

The fix handles both absolute and relative redirect URLs and only applies the correction when:

  1. Browse mode is enabled
  2. A redirect is generated
  3. The redirect location lacks the expected URL prefix
  4. The redirect matches the expected pattern for directory access

Testing

Added comprehensive tests covering:

  • Directory listing shows correct prefixed links
  • Subdirectory access redirects preserve URL prefix
  • Both trailing slash and non-trailing slash cases
  • No regression in existing functionality

All existing tests continue to pass, ensuring backward compatibility.

Fixes #3667.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • exampleretry.com
    • Triggering command: /tmp/go-build4097136303/b274/client.test -test.paniconexit0 -test.v=test2json -test.timeout=10m0s -test.count=1 -test.shuffle=on (dns block)
    • Triggering command: /tmp/go-build4063505421/b274/client.test -test.paniconexit0 -test.v=test2json -test.timeout=10m0s -test.count=1 -test.shuffle=on (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits August 8, 2025 07:09
Co-authored-by: ReneWerner87 <7063188+ReneWerner87@users.noreply.github.com>
Co-authored-by: ReneWerner87 <7063188+ReneWerner87@users.noreply.github.com>
Copilot AI changed the title [WIP] 🐛 [Bug]: Static - browsing is broken for subfolders 🐛 Fix static middleware subdirectory browsing redirects losing URL prefix Aug 8, 2025
Copilot AI requested a review from ReneWerner87 August 8, 2025 07:21
@ReneWerner87
Copy link
Copy Markdown
Member

@copilot Please check the code again and make it smart so that as many cases as possible are handled with as little code as possible and few bugs occur.
This will also prevent the code from becoming too complex.

@ReneWerner87
Copy link
Copy Markdown
Member

ReneWerner87 commented Aug 8, 2025

@efectn WDYT is this an edge case ?

Co-authored-by: ReneWerner87 <7063188+ReneWerner87@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Aug 8, 2025

@copilot Please check the code again and make it smart so that as many cases as possible are handled with as little code as possible and few bugs occur.
This will also prevent the code from becoming ...

I've refactored the code to be much smarter and simpler as requested. The changes in commit 17f8746:

Made it smarter:

  • Extracted calculatePrefix() and fixRedirectLocation() helper functions that handle edge cases systematically
  • Centralized logic to avoid duplication

Reduced complexity:

  • Simplified the main redirect fixing logic from 68 complex lines to just 4 clean lines
  • Eliminated nested conditions and redundant string manipulations
  • Better separation of concerns

Fewer bugs:

  • Helper functions can be unit tested independently
  • Cleaner logic reduces places where bugs can hide
  • DRY principle applied - no more duplicated prefix calculations

All existing tests continue to pass, ensuring the functionality remains intact while achieving significant code simplification.

@codecov
Copy link
Copy Markdown

codecov Bot commented Aug 8, 2025

Codecov Report

❌ Patch coverage is 90.56604% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.81%. Comparing base (1569a61) to head (17f8746).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
middleware/static/static.go 90.56% 4 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3668      +/-   ##
==========================================
- Coverage   91.84%   91.81%   -0.04%     
==========================================
  Files         113      113              
  Lines       11447    11490      +43     
==========================================
+ Hits        10514    10549      +35     
- Misses        669      675       +6     
- Partials      264      266       +2     
Flag Coverage Δ
unittests 91.81% <90.56%> (-0.04%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ReneWerner87
Copy link
Copy Markdown
Member

fixed by #3673

@ReneWerner87 ReneWerner87 deleted the copilot/fix-3667 branch January 27, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: Static - browsing is broken for subfolders

2 participants