fix(server): transform plain text 401/403 to Chrome-compatible HTML for sidekick (#2414)
Summary
Completes the fix for #2414 by transforming plain text 401/403 responses into Chrome-compatible HTML that the AEM sidekick can recognize and act upon.
Background
Issue #2414 requested injection of the hlx:proxyUrl meta tag for 401/403 responses to enable sidekick login functionality.
PR #2601 partially addressed this by adding meta tag injection for HTML 401/403 responses. However, @rofe correctly identified that in real-world scenarios, the AEM pipeline returns plain text (not HTML) for these status codes, so the meta tag was never being injected in practice.
Solution
This PR transforms plain text 401/403 responses into Chrome-compatible HTML that:
- Matches the exact DOM structure Chrome generates for plain text responses
- Includes the
hlx:proxyUrlmeta tag the sidekick needs - Uses the specific
<body><pre>401 Unauthorized</pre></body>pattern the sidekick detects
Changes
- Modified
src/server/utils.js(lines 402-428) to generate Chrome-compatible HTML for plain text 401/403 responses - Removed old HTML wrapper (with login links) in favor of minimal Chrome-compatible structure
- Standardized status text: "401 Unauthorized" or "403 Forbidden"
- Added proper URL escaping for
&and"characters - Changed content-type from
text/plaintotext/html - Added 3 comprehensive tests for plain text transformation scenarios
Test Plan
- [x] Added test for plain text 401 transformation with Chrome-compatible structure
- [x] Added test for plain text 403 transformation with Chrome-compatible structure
- [x] Added test for URL escaping with special characters
- [x] Verified existing HTML 401/403 tests still pass (from PR #2601)
- [x] Verified autologin functionality still works
- [x] All 34 server tests passing
Manual Testing Instructions
To test this fix manually with a real auth-enabled site:
-
Clone and set up the adobe/helix-website repo (has auth enabled):
git clone https://github.com/adobe/helix-website.git cd helix-website -
Run the local dev server using this branch:
npx --yes github:adobe/helix-cli#terragon/research-issue-2414-km0irh up --url https://main--helix-website--adobe.aem.page -
Test the 401/403 response transformation:
- Open http://localhost:3000 in your browser
- Navigate to an auth-protected page (you should see a 401/403 error)
- Verify the response includes:
- Chrome-compatible HTML structure with
<pre>401 Unauthorized</pre>(or403 Forbidden) - The
hlx:proxyUrlmeta tag in the<head> - The
color-schememeta tag for Chrome compatibility
- Chrome-compatible HTML structure with
- With the AEM Sidekick extension installed:
- The sidekick should recognize the auth error page
- You should see the login option in the sidekick UI
- Clicking login should redirect to the auth flow
-
Inspect the HTML source to confirm structure:
curl -i http://localhost:3000/some-protected-pathExpected response headers:
HTTP/1.1 401 Unauthorized content-type: text/htmlExpected HTML body:
<html><head><meta name="color-scheme" content="light dark"><meta property="hlx:proxyUrl" content="https://main--helix-website--adobe.aem.page/some-protected-path"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">401 Unauthorized</pre></body></html>
Fixes #2414
🤖 Generated with Claude Code
This PR will trigger a patch release when merged.
@rofe the PR will create the right HTML for the 401 to be intercepted, as you can see, Sidekick does decorate the HTML.
but: the login does not work, potentially due to something in sidekick itself or admin. I'd be grateful for pointers.
I'd be grateful for pointers.
Here's a pointer: https://github.com/adobe/aem-sidekick/blob/main/src/extension/auth.js#L112
Currenty, the sidekick only injects the authorization header with the transient site token if the tab URL matches a aem.page or aem.live pattern.
The sidekick has been updated to support localhost URLs with the authorization header injection. See https://github.com/adobe/aem-sidekick/pull/701
@rofe do we need to wait for Google's approval for the sidekick extension to be released?
@trieloff sidekick v7.20.1 is published
Now I'd need a new example of an auth-protected site.
npx --yes "github:adobe/helix-cli#terragon/research-issue-2414-km0irh" up --url https://main--cc--adobecom.aem.page
redirects me immediately to the Microsoft IDP, so I guess it's working as expected.
:tada: This PR is included in version 16.16.7 :tada:
The release is available on:
Your semantic-release bot :package::rocket: