-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Prevent newlines in environment variables from causing frontend syntax errors #4750
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
Docker builds report
|
Uffizzi Preview |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems reasonable to me.
Minor question: have we checked the behaviour of the python logic that also generates the same file in the single container image set up? It looks to me (based on this) like it would depend on how the settings attributes are generated, and whether there's any trimming built in there.
This bug only affects the FE image because it's trying to manually generate valid JavaScript. The backend instead generates a JSON payload, which is guaranteed to be valid JS and no trimming is necessary: Line 69 in fbfe317
The failure mode of having unwanted trailing/leading whitespace would be slightly different if using the unified image, as API requests could possibly fail, but at least it won't cause the whole FE to crash. The FE should probably scrap the current approach of generating JS and instead do the same thing the API is doing, but that would be a more intrusive fix. |
Thanks for submitting a PR! Please check the boxes below:
docs/
if required so people know about the feature!Changes
If a frontend environment variable such as
FLAGSMITH_ON_FLAGSMITH_API_KEY
includes a trailing newline, the response from/config/project-overrides
will be invalid JavaScript. For example, running the frontend like this causes a syntax error on the login page:results in this response, which is invalid and causes a frontend syntax error:
A customer ran into this when using a manually base64-encoded Kubernetes secret without realising it had a trailing newline, which broke their frontend.
String.prototype.trim
removes all surrounding whitespace, not just newlines, but I'm 99% certain we will never have environment variables that depend on trailing or leading whitespace.This can still break if environment variables contain
'
,/*
or other strings that interfere with JS. We should use a smarter mechanism to inject configuration that cannot cause a syntax error in the frontend, such as injecting a JSON payload and trying to decode that from JS, instead of injecting JS directly.How did you test this code?
Manually by running the FE locally, and confirming this works as expected with this change: