-
Notifications
You must be signed in to change notification settings - Fork 554
refactor(code-help): escape snippets centrally instead of in source strings #8062
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
base: main
Are you sure you want to change the base?
Changes from all commits
ba46fe2
88dd8d4
90b56a6
7e217da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| export default () => `You can access the API directly with tools like <a href="https://curl.haxx.se/">curl</a> or <a href='https://httpie.org/'>httpie</a>, | ||
| export default () => `You can access the API directly with tools like curl or httpie, | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The dropped |
||
| or with clients for languages that we do not currently have SDKs for. | ||
| You can view the API via Swagger at <a href="https://api.flagsmith.com/api/v1/docs/">https://api.flagsmith.com/api/v1/docs/</a>. | ||
| You can view the API via Swagger at https://api.flagsmith.com/api/v1/docs/. | ||
| ` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,9 @@ | ||
| import Utils from 'common/utils/utils' | ||
|
|
||
| export default () => `// Maven | ||
| ${Utils.escapeHtml('<dependency>')} | ||
| ${Utils.escapeHtml('<groupId>com.flagsmith</groupId>')} | ||
| ${Utils.escapeHtml('<artifactId>flagsmith-java-client</artifactId>')} | ||
| ${Utils.escapeHtml('<version>7.4.1</version>')} | ||
| ${Utils.escapeHtml('</dependency>')} | ||
| <dependency> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Java and .NET didn't have literal entities in source; they escaped at runtime via |
||
| <groupId>com.flagsmith</groupId> | ||
| <artifactId>flagsmith-java-client</artifactId> | ||
| <version>7.4.1</version> | ||
| </dependency> | ||
|
|
||
| // Gradle | ||
| implementation 'com.flagsmith:flagsmith-java-client:7.4.1' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| export default () => | ||
| 'The package can be found at <a href="https://crates.io/crates/flagsmith">https://crates.io/crates/flagsmith</a>;' | ||
| 'The package can be found at https://crates.io/crates/flagsmith;' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -180,12 +180,6 @@ const Utils = Object.assign({}, BaseUtils, { | |
| } | ||
| return null | ||
| }, | ||
| escapeHtml(html: string) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zero callers after this branch (Highlight has its own local escapeHtml for rendering). Removed so the next snippet author doesn't reach for it to pre-escape content again, which is the pattern that caused the Java/.NET bug. |
||
| const text = document.createTextNode(html) | ||
| const p = document.createElement('p') | ||
| p.appendChild(text) | ||
| return p.innerHTML | ||
| }, | ||
| featureStateToValue(featureState: FeatureStateValue) { | ||
| if (!featureState) { | ||
| return null | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,7 +134,7 @@ class Highlight extends React.Component { | |
| } | ||
|
|
||
| const raw = this.getRawHtml() | ||
| const html = this.props.preventEscape ? raw : escapeHtml(raw) | ||
| const html = escapeHtml(raw) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always escaping is safe: the only two consumers that opted out ( |
||
| return ( | ||
| <div className={this.state.expandable ? 'expandable' : ''}> | ||
| <pre | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,8 @@ | ||
| // Snippets for every SDK we support, sourced from the maintained | ||
| // `Constants.codeHelp` (the same install/init the rest of the app uses, so | ||
| // they don't drift). Two adaptations for this page: | ||
| // 1. codeHelp snippets are authored for innerHTML rendering, so they carry | ||
| // HTML entities (< etc.). We render via <Highlight> (escaping on), so | ||
| // we unescape first. | ||
| // 2. They use a placeholder flag name; we swap it for the user's real flag, | ||
| // so the snippet references the flag this onboarding actually created. | ||
| // they don't drift). One adaptation for this page: codeHelp uses a placeholder | ||
| // flag name; we swap it for the user's real flag, so the snippet references | ||
| // the flag this onboarding actually created. | ||
| // The SDK list itself (labels, logos, codeHelp keys) lives in ./sdkLangs. | ||
| import Constants from 'common/constants' | ||
| import { SdkLang } from './sdkLangs' | ||
|
|
@@ -14,14 +11,6 @@ import { SdkLang } from './sdkLangs' | |
| // exported); if that placeholder ever changes, update this too. | ||
| const PLACEHOLDER_FLAG = 'my_cool_feature' | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The removed |
||
|
|
||
| const unescapeHtml = (s: string): string => | ||
| s | ||
| .replace(/</g, '<') | ||
| .replace(/>/g, '>') | ||
| .replace(/'/g, "'") | ||
| .replace(/"/g, '"') | ||
| .replace(/&/g, '&') | ||
|
|
||
| export type SdkSnippet = { | ||
| install: string | ||
| // Present for npm-based SDKs (codeHelp ships both managers in one block); | ||
|
|
@@ -55,12 +44,12 @@ export const getSdkSnippet = ( | |
| string, | ||
| string | ||
| > | ||
| const wire = unescapeHtml(inits[lang.initKey ?? lang.codeHelpKey] ?? '') | ||
| const wire = (inits[lang.initKey ?? lang.codeHelpKey] ?? '') | ||
| .split(PLACEHOLDER_FLAG) | ||
| .join(featureName) | ||
| return { | ||
| language: lang.language, | ||
| ...parseInstall(unescapeHtml(installs[lang.codeHelpKey] ?? '')), | ||
| ...parseInstall(installs[lang.codeHelpKey] ?? ''), | ||
| wire: wire.trim(), | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.