Skip to content
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

feat(api-service): decode special characters #7881

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions packages/framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import type {
} from './types';
import { WithPassthrough } from './types/provider.types';
import {
decodeHTML,
EMOJI,
log,
resolveApiUrl,
Expand Down Expand Up @@ -358,16 +359,19 @@ export class Client {
resolve: stepResolve as typeof step.resolve,
});

if (
Object.values(ChannelStepEnum).includes(step.type as ChannelStepEnum) &&
// TODO: Update return type to include ChannelStep and fix typings
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(options as any)?.disableOutputSanitization !== true
) {
const shouldSanitizeOutputs =
(options as { disableOutputSanitization?: boolean })?.disableOutputSanitization !== true;
const isChannelStep = (Object.values(ChannelStepEnum) as string[]).includes(step.type);

if (isChannelStep && shouldSanitizeOutputs) {
// Sanitize the outputs to avoid XSS attacks via Channel content.
const sanitizedOutputs = sanitizeHtmlInObject(stepResult.outputs);
// Decode string values that aren't meant to be HTML back to their original form
const decodedOutputs = decodeOutputs(step.type, sanitizedOutputs);

stepResult = {
...stepResult,
outputs: sanitizeHtmlInObject(stepResult.outputs),
outputs: decodedOutputs,
};
}

Expand Down Expand Up @@ -838,3 +842,16 @@ function buildSteps(stateArray: State[]) {

return result;
}

const decodeOutputs = (stepType: string, output: Record<string, unknown>) => {
const result: Record<string, unknown> = {};
for (const [key, value] of Object.entries(output)) {
if (key === 'subject' && stepType === 'email') {
result[key] = decodeHTML(value as string);
} else {
result[key] = value;
}
}

return result;
};
31 changes: 31 additions & 0 deletions packages/framework/src/utils/sanitize.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,34 @@
return acc;
}, {} as T);
};

export const decodeHTML = (html: string): string => {
if (!html) {
return html;
}

return (
html
// Basic entities
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'")
.replace(/&nbsp;/g, ' ')
// Additional common entities
.replace(/&copy;/g, '©')
.replace(/&reg;/g, '®')
.replace(/&trade;/g, '™')
.replace(/&cent;/g, '¢')
.replace(/&pound;/g, '£')
.replace(/&yen;/g, '¥')
.replace(/&euro;/g, '€')
.replace(/&sect;/g, '§')
.replace(/&mdash;/g, '—')
.replace(/&ndash;/g, '–')
.replace(/&lsquo;/g, `'`)
.replace(/&rsquo;/g, `'`)
.replace(/&rdquo;/g, `"`)
);
};
Loading