fleet/frontend/utilities/scripts_encoding.ts
Carlo e99ff3e046
Fix FMAs on Render (#37557)
Fixes #33732

Base64-encodes the `install_script` and `uninstall_script` payloads for
add and edit software to prevent being blocked by WAF rules and allow
FMAs for Windows to be added/edited in Fleet instances running on
Render.


![fix-33732-fma-on-render](https://github.com/user-attachments/assets/8293fa30-0739-4769-bd21-09733a23dadc)
2025-12-23 13:01:32 -05:00

28 lines
950 B
TypeScript

// Header name for signaling base64-encoded scripts to bypass WAF rules
// that may block requests containing shell/PowerShell script patterns.
export const SCRIPTS_ENCODED_HEADER = "X-Fleet-Scripts-Encoded";
/**
* Base64 encode a script string with proper UTF-8 handling.
* Returns undefined for undefined input and empty string for empty input,
* which allows callers to pass through empty/unset script fields without modification.
*/
export const encodeScriptBase64 = (
script: string | undefined
): string | undefined => {
if (script === undefined) {
return undefined;
}
if (script === "") {
return "";
}
// Use TextEncoder for proper UTF-8 handling of unicode characters
const encoder = new TextEncoder();
const data = encoder.encode(script);
// Convert Uint8Array to binary string, then base64
let binary = "";
data.forEach((byte) => {
binary += String.fromCharCode(byte);
});
return btoa(binary);
};