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.
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)}.
5752 * </p>
5853 * <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.
54+ * IMPORTANT: it is responsibility of the caller to validate / sanitize content of body
55+ * parts. For instance, when using an explicit boundary, it's the caller's responsibility to
56+ * ensure the body parts do not contain the boundary value, which can prevent the consumer of
57+ * the entity from correctly parsing / processing the body parts.
6258 * </p>
6359 *
6460 * @since 5.0
@@ -74,7 +70,6 @@ public class MultipartEntityBuilder {
7470
7571 private static final String BOUNDARY_PREFIX = "httpclient_boundary_" ;
7672
77- private boolean isRandomBoundaryRequested = false ;
7873 /**
7974 * The logger for this class.
8075 */
@@ -125,12 +120,14 @@ public MultipartEntityBuilder setStrictMode() {
125120 /**
126121 * Sets a custom boundary string for the multipart entity.
127122 * <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()}).
123+ * If {@code null} is provided, the builder reverts to its default logic of using a random UUID.
124+ * </p>
125+ * <p>
126+ * IMPORTANT: when setting an explicit boundary, it is responsibility of the caller to validate / sanitize content
127+ * of body parts to ensure they do not contain the boundary value.
131128 * </p>
132129 *
133- * @param boundary the boundary string, or {@code null} to use the default boundary logic
130+ * @param boundary the boundary string, or {@code null} to use a random UUID.
134131 * @return this builder instance
135132 */
136133 public MultipartEntityBuilder setBoundary (final String boundary ) {
@@ -234,14 +231,12 @@ public MultipartEntityBuilder addBinaryBody(final String name, final InputStream
234231 }
235232
236233 /**
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.
234+ * Generates a random boundary using UUID. The UUID is a v4 random UUID generated from a cryptographically-secure
235+ * random source.
236+ * <p>
237+ * A cryptographically-secure random number source is used to generate the UUID, to avoid a malicious actor crafting
238+ * a body part that contains the boundary value to tamper with the entity structure.
239+ * </p>
245240 */
246241 private String getRandomBoundary () {
247242 return BOUNDARY_PREFIX + UUID .randomUUID ();
@@ -274,29 +269,13 @@ public MultipartEntityBuilder addEpilogue(final String epilogue) {
274269 return this ;
275270 }
276271
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-
290272 MultipartFormEntity buildEntity () {
291273 String boundaryCopy = boundary ;
292274 if (boundaryCopy == null && contentType != null ) {
293275 boundaryCopy = contentType .getParameter ("boundary" );
294276 }
295277 if (boundaryCopy == null ) {
296- boundaryCopy = isRandomBoundaryRequested ? getRandomBoundary () : getFixedBoundary ();
297- if (LOG .isWarnEnabled ()) {
298- LOG .warn ("No boundary explicitly set; using generated default: {}" , boundaryCopy );
299- }
278+ boundaryCopy = getRandomBoundary ();
300279 }
301280 Charset charsetCopy = charset ;
302281 if (charsetCopy == null && contentType != null ) {
0 commit comments