Skip to content

chore: override codersdk.SessionTokenCookie in develop.sh#18991

Merged
johnstcn merged 7 commits into
mainfrom
cj/dev-session-token-cookie-prefix
Jul 23, 2025
Merged

chore: override codersdk.SessionTokenCookie in develop.sh#18991
johnstcn merged 7 commits into
mainfrom
cj/dev-session-token-cookie-prefix

Conversation

@johnstcn
Copy link
Copy Markdown
Member

@johnstcn johnstcn commented Jul 22, 2025

Enables viewing the Coder UI proxied by Coder.
Exposes an environment variable CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX which allows adding a prefix to the name of the session token cookie. This only works for development builds.

$ CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX=dev ./scripts/develop.sh
$ curl 'https://3000--dev--nonix--cian--apps.dev.coder.com/api/v2/users/me' -H "Cookie: dev_coder_session_token=$(cat .coderv2/session)"
{"id":"fddc81ff-7924-4628-a146-b799d2fb81b4","username":"admin","name":"Admin User","email":"admin@coder.com","created_at":"2025-07-22T13:39:16.670833+01:00","updated_at":"2025-07-22T17:49:38.313364+01:00","last_seen_at":"2025-07-22T16:49:38.313364Z","status":"active","login_type":"password","organization_ids":["c56e87ca-effa-43a6-a464-f25840a47483"],"roles":[{"name":"owner","display_name":"Owner"}]}

EDIT: I tried an alternative implementation instead using -ldflags -X that removes the need for most changes in codersdk.

@johnstcn johnstcn self-assigned this Jul 22, 2025
@johnstcn johnstcn changed the title Cj/dev session token cookie prefix chore: add CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX in development mode Jul 22, 2025
@johnstcn johnstcn requested review from Emyrk, code-asher and mtojek July 22, 2025 17:03
Comment thread site/vite.config.mts
},
},
allowedHosts: [".coder"],
allowedHosts: [".coder", ".dev.coder.com"],
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: also updated allowedHosts for dev URLs.

Comment thread scripts/develop.sh Outdated

cdroot
DEBUG_DELVE="${debug}" start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable --access-url "${CODER_DEV_ACCESS_URL}" --dangerous-allow-cors-requests=true --enable-terraform-debug-mode "$@"
DEBUG_DELVE="${debug}" CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX="${CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX}" start_cmd API "" "${CODER_DEV_SHIM}" server --http-address 0.0.0.0:3000 --swagger-enable --access-url "${CODER_DEV_ACCESS_URL}" --dangerous-allow-cors-requests=true --enable-terraform-debug-mode "$@"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: I'm setting the prefix to dev_ by default in develop.sh.

Copy link
Copy Markdown
Member

@code-asher code-asher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried it out and it is glorious we can run Coder over dev URLs now 🎉

Too bad we cannot get rid of codersdk.SessionTokenCookie or set it dynamically (at least not as a const), but what can ya do.

Comment thread codersdk/client.go Outdated
Comment on lines +109 to +116
func GetSessionTokenCookie() string {
if buildinfo.IsDev() {
if pfx, found := os.LookupEnv("CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX"); found && pfx != "" {
return pfx + "_" + SessionTokenCookie
}
}
return SessionTokenCookie
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should SessionTokenCookie just be a var? And a function updates the var?

At the very least, SessionTokenCookie should probably not be exported. Just this function

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was thinking of doing that but didn't want to make a breaking API change.
But you got me thinking... what do we do for buildinfo.tag?
If I change SessionTokenCookie to a var we can just do the -ldflags -X trick and not have to change anything else :)

@bpmct
Copy link
Copy Markdown
Member

bpmct commented Jul 22, 2025

woooooooooo!!!!

Comment on lines -2703 to -2705
// From codersdk/client.go
export const SessionTokenCookie = "coder_session_token";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't intentional; I need to figure out how to keep this around.
@Emyrk think it's possible to modify this behaviour in apitypings?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now I'm manually vendoring this into api.ts.

Comment thread scripts/build_go.sh
Comment on lines +153 to +158
if [[ "$develop_in_coder" == 1 ]]; then
echo "INFO : Overriding codersdk.SessionTokenCookie as we are developing inside a Coder workspace."
ldflags+=(
-X "'github.com/coder/coder/v2/codersdk.SessionTokenCookie=dev_coder_session_token'"
)
fi
Copy link
Copy Markdown
Member Author

@johnstcn johnstcn Jul 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to try this way instead. It's a little more involved but requires less code changes in codersdk. I can remove the echo message if it's annoying.

@johnstcn johnstcn changed the title chore: add CODER_DEV_SESSION_TOKEN_COOKIE_PREFIX in development mode chore: override codersdk.SessionTokenCookie in develop.sh Jul 22, 2025
Copy link
Copy Markdown
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!!

Comment thread site/src/api/api.ts
Comment on lines +110 to +115
/**
* Originally from codersdk/client.go.
* The below declaration is required to stop Knip from complaining.
* @public
*/
export const SessionTokenCookie = "coder_session_token";
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

@johnstcn johnstcn merged commit bb83071 into main Jul 23, 2025
65 of 71 checks passed
@johnstcn johnstcn deleted the cj/dev-session-token-cookie-prefix branch July 23, 2025 11:48
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 23, 2025
Comment thread scripts/coder-dev.sh
CODER_DELVE_DEBUG_BIN=$(realpath "./build/coder_debug_${GOOS}_${GOARCH}")
popd

if [ -n "${CODER_AGENT_URL}" ]; then
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also check if CODER=true. That's what I do in my dotfiles.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot about that one!

Copy link
Copy Markdown
Member

@code-asher code-asher Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also maybe be "${CODER_AGENT_URL:-}" in case the env var is not set, on account of the set -u.

Copy link
Copy Markdown
Member Author

@johnstcn johnstcn Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that here above (L13) but missed it in develop.sh. Will push a separate PR 👍

#19043

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right oops! I got lost in what script I was looking at lol

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants