Skip to content

docs(cookie): http_only is documented as opt-in, but the component sets HttpOnly by default #1600

Closed

Description

@amrutadotorg

Hi @lovasoa, please take a look at this one.

SQL file that shows the bug

select 'cookie' as component, 'session' as name, 'abc123' as value;
select 'text'   as component, 'ok' as contents;

Requested with:

curl -sD - -o /dev/null http://localhost:8080/test_cookie.sql | grep -i set-cookie

What happens

set-cookie: session=abc123; HttpOnly; SameSite=Strict; Secure; Path=/

The HttpOnly attribute is added even though http_only was not specified. In the browser console
afterwards:

document.cookie                        // "session" is not there
document.cookie = 'session=x; path=/'  // silently ignored

So the cookie is neither readable nor writable from client-side JavaScript.

What you expected

No HttpOnly attribute, as the documentation promises:

Whether the cookie should only be accessible via HTTP and not via client-side scripts. If not
specified, the cookie will be accessible via both HTTP and client-side scripts.

— https://sql-page.com/component.sql?component=cookie

Database

SQLite (3.x, embedded)

SQLPage version

v0.48.0 (official lovasoa/sqlpage:latest Docker image)

Last SQLPage version where it worked

I don't know when the docs and the code diverged; the behaviour is already pinned by tests in v0.48.0.

Anything else

Where the code and the docs disagree

src/render.rs:233-234 — HttpOnly unless explicitly disabled:

let http_only = obj.get("http_only");
cookie.set_http_only(http_only != Some(&json!(false)) && http_only != Some(&json!(0)));

tests/cookies/mod.rs:16 — cookies_are_http_only_secure_and_same_site_strict_by_default() asserts
HttpOnly in the default header, so the runtime behaviour looks deliberate (the test came from #1429)
and only the documentation is stale.

The stale text lives in
examples/official-site/sqlpage/migrations/05_cookie.sql:68.

Why it matters

I use the component to persist a UI preference (light/dark theme) that is rendered server-side, and —
following the docs — expected JavaScript to be able to read and update the same cookie. Instead:

  1. document.cookie never sees it, so client-side code cannot detect the stored preference.
  2. Writes from JavaScript are dropped (RFC 6265bis §5.7, storage model: a cookie received from a
    non-HTTP API is ignored when an HttpOnly cookie with the same name/domain/path already exists).

That combination produces silent failures. On my site, a small client-side fallback that
initialises the preference on the first visit could never see — nor write — the cookie it was supposed
to create, so it re-ran on every page load and reloaded the page each time. Nothing in the logs,
nothing visible on the page; finding the cause required reading render.rs.

Suggested fix

Update the parameter description in examples/official-site/sqlpage/migrations/05_cookie.sql (and
therefore the generated docs table) to match the implementation, e.g.:

Whether the cookie should only be accessible via HTTP and not via client-side scripts.
Defaults to TRUE; set it to false (or 0) to let client-side JavaScript read and modify the cookie.

Two adjacent details are also worth documenting while touching that entry:

  • http_only and secure accept 0 as well as false (see tests/cookies/set_cookie_opt_out.sql),
    which is useful on MySQL/MSSQL/Oracle, where there is no boolean literal.
  • path defaults to / when omitted (src/render.rs:209-213); the docs only say "the cookie will be
    sent for all paths", which is equivalent but less explicit.

Note: the docs site seeds this text through a migration, so the fix needs a new migration file
(05_cookie.sql is already applied) using ON CONFLICT ... DO UPDATE, otherwise the change is
silently skipped.

If instead the intent is that cookies should be JS-readable by default, then the default in
src/render.rs and the assertion in tests/cookies/mod.rs would have to change too — but given that
#1429 deliberately pinned the secure defaults, correcting the documentation looks like the right fix.

Workaround for other users hitting this

select 'cookie' as component,
    'lightdarkstatus' as name,
    'dark'            as value,
    false             as http_only,  -- let JavaScript read and write it
    'lax'             as same_site;  -- 'strict' (the default) drops the cookie on cross-site navigation

Activity

  1. lovasoa commented on Nov 13, 2026

    @lovasoa
    Collaborator

    thanks for letting me know, I updated the docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions