Index: src/wp-includes/media.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- src/wp-includes/media.php	(revision 35960)
+++ src/wp-includes/media.php	(revision )
@@ -987,7 +987,6 @@
 	}
 
 	$image_basename = wp_basename( $image_meta['file'] );
-	$image_baseurl = _wp_upload_dir_baseurl();
 
 	/*
 	 * WordPress flattens animated GIFs into one frame when generating intermediate sizes.
@@ -1004,16 +1003,36 @@
 		return false;
 	}
 
-	// Uploads are (or have been) in year/month sub-directories.
-	if ( $image_basename !== $image_meta['file'] ) {
-		$dirname = dirname( $image_meta['file'] );
+	// Retrieve the uploads sub-directory from the full size image.
+	$file_path = $image_meta['file'];
+	$dirname = dirname( $file_path );
+	$dirname = $dirname === '.' ? '' : trailingslashit( $dirname );
 
-		if ( $dirname !== '.' ) {
+	/**
+	 * Filter the image dirname used to locate images in image sizes.
+	 *
+	 * @since 4.4.1
+	 *
+	 * @param string    $dirname        The directory name of file.
+	 * @param int       $attachment_id  ID of attachment.
+     * @param string    $file_path      Path to file including filename.
+	 */
+	$dirname = apply_filters( 'srcset_image_dirname', $dirname, $attachment_id, $file_path );
+
+	$image_baseurl = _wp_upload_dir_baseurl();
-			$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
+	$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
-		}
-	}
 
-	$image_baseurl = trailingslashit( $image_baseurl );
+	/**
+	 * Filter the image base URL used in constructing srcset image size URLs.
+	 *
+	 * @since 4.4.1
+	 *
+	 * @param string    $image_baseurl  The base URL of the image src.
+	 * @param int       $attachment_id  ID of attachment.
+     * @param string    $image_src      Image src URL.
+     * @param string    $dirname        The directory name of file.
+	 */
+	$image_baseurl = apply_filters( 'srcset_image_baseurl', $image_baseurl, $attachment_id, $image_src, $dirname );
 
 	// Calculate the image aspect ratio.
 	$image_ratio = $image_height / $image_width;
@@ -1038,12 +1057,24 @@
 	// Array to hold URL candidates.
 	$sources = array();
 
+	/**
+	 * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
+	 * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
+	 * an incorrect image. See #35045.
+	 */
+	$src_matched = false;
+
 	/*
 	 * Loop through available images. Only use images that are resized
 	 * versions of the same edit.
 	 */
 	foreach ( $image_sizes as $image ) {
 
+		// If the file name is part of the `src`, we've confirmed a match.
+		if ( false !== strpos( $image_src, $dirname . $image['file'] ) ) {
+			$src_matched = true;
+		}
+
 		// Filter out images that are from previous edits.
 		if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
 			continue;
@@ -1096,7 +1127,7 @@
 	$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
 
 	// Only return a 'srcset' value if there is more than one source.
-	if ( count( $sources ) < 2 ) {
+	if ( ! $src_matched || count( $sources ) < 2 ) {
 		return false;
 	}
 
@@ -1275,28 +1306,6 @@
 	if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
 		strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
 
-		return $image;
-	}
-
-	$base_url = trailingslashit( _wp_upload_dir_baseurl() );
-	$image_base_url = $base_url;
-
-	$dirname = dirname( $image_meta['file'] );
-	if ( $dirname !== '.' ) {
-		$image_base_url .= trailingslashit( $dirname );
-	}
-
-	$all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
-
-	foreach ( $all_sizes as $key => $file ) {
-		$all_sizes[ $key ] = $image_base_url . $file;
-	}
-
-	// Add the original image.
-	$all_sizes[] = $base_url . $image_meta['file'];
-
-	// Bail early if the image src doesn't match any of the known image sizes.
-	if ( ! in_array( $image_src, $all_sizes ) ) {
 		return $image;
 	}
 
Index: tests/phpunit/tests/media.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- tests/phpunit/tests/media.php	(revision 35960)
+++ tests/phpunit/tests/media.php	(revision )
@@ -1133,4 +1133,42 @@
 		// Intermediate sized GIFs should not include the full size in the srcset.
 		$this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
 	}
+
+	/**
+	 * @ticket 35045
+	 * @ticket 33641
+	 */
+	function test_wp_make_content_images_responsive_schemes() {
+		$image_meta = wp_get_attachment_metadata( self::$large_id );
+		$size_array = $this->_get_image_size_array_from_name( 'medium' );
+
+		$srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
+		$sizes  = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
+
+		// Build HTML for the editor.
+		$img          = get_image_tag( self::$large_id, '', '', '', 'medium' );
+		$img_https    = str_replace( 'http://', 'https://', $img );
+		$img_relative = str_replace( 'http://', '//', $img );
+
+		// Manually add srcset and sizes to the markup from get_image_tag().
+		$respimg          = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
+		$respimg_https    = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
+		$respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
+
+		$content = '
+			<p>Image, http: protocol. Should have srcset and sizes.</p>
+			%1$s
+
+			<p>Image, http: protocol. Should have srcset and sizes.</p>
+			%2$s
+
+			<p>Image, protocol-relative. Should have srcset and sizes.</p>
+			%3$s';
+
+		$unfiltered = sprintf( $content, $img, $img_https, $img_relative );
+		$expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
+		$actual     = wp_make_content_images_responsive( $unfiltered );
+
+		$this->assertSame( $expected, $actual );
+	}
 }
