4747/**
4848 * Builder for multipart {@link HttpEntity}s.
4949 * <p>
50- * This class constructs multipart entities with a boundary determined by either a fixed
51- * value ("httpclient_boundary_7k9p2m4x8n5j3q6t1r0vwyzabcdefghi") or a random UUID. If no
52- * boundary is explicitly set via {@link #setBoundary(String)}, it defaults to the fixed
53- * value unless {@link #withRandomBoundary()} is called to request a random UUID at build
54- * time. Users can provide a custom boundary with {@link #setBoundary(String)}. A warning
55- * is logged when no explicit boundary is set via {@link #setBoundary(String)}, encouraging
56- * deliberate choice.
57- * </p>
58- * <p>
59- * IMPORTANT: it is responsibility of the caller to validate / sanitize content of body
60- * parts, for instance, to ensure they do not contain the boundary value that can prevent
61- * the consumer of the entity from correctly parsing / processing the body parts.
50+ * This class constructs multipart entities with a boundary determined by either a random UUID
51+ * or an explicit boundary set via {@link #setBoundary(String)}.
6252 * </p>
6353 *
6454 * @since 5.0
@@ -74,7 +64,6 @@ public class MultipartEntityBuilder {
7464
7565 private static final String BOUNDARY_PREFIX = "httpclient_boundary_" ;
7666
77- private boolean isRandomBoundaryRequested = false ;
7867 /**
7968 * The logger for this class.
8069 */
@@ -125,12 +114,14 @@ public MultipartEntityBuilder setStrictMode() {
125114 /**
126115 * Sets a custom boundary string for the multipart entity.
127116 * <p>
128- * If {@code null} is provided, the builder reverts to its default boundary logic:
129- * either using a boundary from the {@code contentType} if present, or falling back
130- * to a fixed or random boundary (depending on {@link #withRandomBoundary()}).
117+ * If {@code null} is provided, the builder reverts to its default logic of using a random UUID.
118+ * </p>
119+ * <p>
120+ * IMPORTANT: when setting an explicit boundary, it is responsibility of the caller to validate / sanitize content
121+ * of body parts to ensure they do not contain the boundary value.
131122 * </p>
132123 *
133- * @param boundary the boundary string, or {@code null} to use the default boundary logic
124+ * @param boundary the boundary string, or {@code null} to use a random UUID.
134125 * @return this builder instance
135126 */
136127 public MultipartEntityBuilder setBoundary (final String boundary ) {
@@ -234,14 +225,12 @@ public MultipartEntityBuilder addBinaryBody(final String name, final InputStream
234225 }
235226
236227 /**
237- * Returns the fixed default boundary value.
238- */
239- private String getFixedBoundary () {
240- return BOUNDARY_PREFIX + "7k9p2m4x8n5j3q6t1r0vwyzabcdefghi" ;
241- }
242-
243- /**
244- * Generates a random boundary using UUID.
228+ * Generates a random boundary using UUID. The UUID is a v4 random UUID generated from a cryptographically-secure
229+ * random source.
230+ * <p>
231+ * A cryptographically-secure random number source is used to avoid security issues similar to
232+ * CVE-2025-22150 (affecting the Node.JS ecosystem).
233+ * </p>
245234 */
246235 private String getRandomBoundary () {
247236 return BOUNDARY_PREFIX + UUID .randomUUID ();
@@ -274,29 +263,13 @@ public MultipartEntityBuilder addEpilogue(final String epilogue) {
274263 return this ;
275264 }
276265
277- /**
278- * Configures the builder to request a random boundary generated by UUID.randomUUID()
279- * at build time if no explicit boundary is set via {@link #setBoundary(String)}.
280- *
281- * @return this builder instance
282- * @since 5.5
283- */
284- public MultipartEntityBuilder withRandomBoundary () {
285- this .isRandomBoundaryRequested = true ;
286- this .boundary = null ;
287- return this ;
288- }
289-
290266 MultipartFormEntity buildEntity () {
291267 String boundaryCopy = boundary ;
292268 if (boundaryCopy == null && contentType != null ) {
293269 boundaryCopy = contentType .getParameter ("boundary" );
294270 }
295271 if (boundaryCopy == null ) {
296- boundaryCopy = isRandomBoundaryRequested ? getRandomBoundary () : getFixedBoundary ();
297- if (LOG .isWarnEnabled ()) {
298- LOG .warn ("No boundary explicitly set; using generated default: {}" , boundaryCopy );
299- }
272+ boundaryCopy = getRandomBoundary ();
300273 }
301274 Charset charsetCopy = charset ;
302275 if (charsetCopy == null && contentType != null ) {
0 commit comments