Language: TypeScript
Label: safe
CWE: CWE-331 \u2014 Insufficient Entropy
The product uses an algorithm or scheme that produces insufficient entropy, leaving patterns or clusters of values that are more likely to occur than others.

Code:
function prepareMessagePayload(modules, messagePayload) {
  let stringifiedPayload = JSON.stringify(messagePayload);

  if (modules.cryptoModule) {
    const encrypted = modules.cryptoModule.encrypt(stringifiedPayload);
    stringifiedPayload = typeof encrypted === 'string' ? encrypted : encode(encrypted);
    stringifiedPayload = JSON.stringify(stringifiedPayload);
  }
  return stringifiedPayload || '';
}