Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,61 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.10.1] - 2026-12-02

A security release. Every version before it is affected: upgrade, then repair what is already on the disk. Read *Changed* first: some behaviour changes.

### Security

**Upgrading changes nothing on the disk by itself.** Follow *Repairing media left public* in the README: make each private media private again, then purge the CDN.

Private media (since 0.3.0 unless said otherwise):

- Anyone could read a private media attached to no post, and get its signed URL: over REST (`GET /wp/v2/media/{id}` and the collection), on its attachment page, in its embed and feed, and in the oEmbed answer
- The feed of a private media was also printed to a request that runs without themes, such as a direct one to `wp-blog-header.php`
- The same page, reached as a single post (`?post_type=attachment&p=N`, an attached media included) or with the posts page in the query, showed the private media; when WordPress has no attachment pages, it redirected to the signed URL of the file, and otherwise to its slug
- Any signed-in user, a subscriber included, could get the signed URL of a private media by asking WordPress to preview a `[gallery]` shortcode (`admin-ajax.php?action=parse-media-shortcode`)
- A private media that is not an image (PDF, archive, video, audio) was never made private on the disk, neither at upload nor by `wp pollora media visibility --private`, which reported a success with no file changed
- The files written after a media was made private (an edit in the media library, sizes made later, the new media the block editor saves a crop or a rotation as) were left public. They are made private when the metadata is saved, and the copy made by the editor is private
- The files WordPress keeps of an image before its edit, and its other companions (`source_image`, `animated_video`, `animated_video_poster`), did not follow the visibility of the media
- The sizes of a private media made at upload, when its image is edited, when its focal point moves or by `thumbs warm` kept the default visibility of the disk (declared sizes, since 0.4.0)
- The fallback route served the file of a declared size of a private media when that file existed, with a public cache of one year, and made or served sizes of the original a private image had before its edit (since 0.5.0)
- While the server is saturated, the fallback route could answer with a size of another media whose name starts like the one asked for, or that has the same name with another extension, private or not (since 0.5.0)

Networks:

- On a site other than the main one, no file of a private media was made private, and the files of the main site with the same names were changed instead
- Deleting a site whose uploads are the root of the library (a network created before WordPress 3.5, uploads moved out of `sites/N`) deleted every file of the library (since 0.1.0)
- On a site other than the main one, the declared sizes used the paths of the main site: deleting a media could delete the main site's files (since 0.4.0), `thumbs prune` run there could delete them too (since 0.4.0), and the fallback route could serve a private size of the main site (since 0.5.0)

Stream wrapper:

- `rmdir('pollora://.')` (since 0.1.0), `pollora://uploads/.` and spellings with a backslash (since 0.2.0) deleted the whole media library, because the disk reads them as its root after the wrapper had taken them for a folder. `.` segments and backslashes are now resolved by the wrapper first

### Changed

Behaviour changes to check before upgrading:

- **WordPress 6.2 or later** is required: the database queries use the `%i` placeholder
- **REST**: for a user without `upload_files` who cannot edit it, a private media answers `401` or `403` on `/wp/v2/media/{id}`, and is left out of the collection and of `_embed`. A headless front-end that reads private media anonymously, or a custom role that relied on it, has to authenticate or be given the capability
- **Attachment page and oEmbed**: for the same users, the attachment page of a private media, its embed and its feed are not found, and oEmbed answers `404`. A plugin that gates a download through that page has to give its users `edit_post` on the media, or serve it elsewhere
- **Shortcode preview**: `parse-media-shortcode` is refused to a user without `upload_files` who names no post they can edit
- **Networks**: a site other than the main one gets no declared size, including the images uploaded before: WordPress serves its own sizes, and the original under a declared name it has no size for. The declared size recorded in the metadata of such an image is no longer read
- **Networks**: on a site other than the main one, the fallback route serves nothing, and `wp pollora media thumbs prune` refuses to run
- **Networks**: when the uploads of a deleted site are the whole library, its files stay on the disk
- **Fallback route**: a size whose original is no longer on the disk is a `404`, even when its file exists. The route asks the database and the disk for a size already made, where it only asked the disk
- **Larger file list**: the files of a media now include the file of a media that is not an image, the attached file when it differs from the one of the metadata, the companions WordPress records and the files kept from before an edit. Making a media private or public changes them all, `wp pollora media files` lists them, and the `$paths` of the `pollora/media/deleted` action (and the `MediaDeleted` event of an application that relays it) holds them
- **`wp pollora media visibility` can report new failures**: a file WordPress still records for a media but that is no longer on the disk, such as one kept from before an edit, is reported as not changed, and the command then exits with `1`. A script that stops on a failure has to expect it
- Saving the metadata of a private media sets the visibility of each of its files, one call to the disk per file
- PHPStan runs at level 10. A value without a type from WordPress, WP-CLI, a filter or a constant that is neither a scalar nor stringable now reads as `''` or `0`
- `UrlReplacement` and the private media lookup pass table and column names to `$wpdb->prepare()` as `%i` identifiers (WordPress 6.2 or later)
- `UrlReplacement` and the private media lookup pass table and column names to `$wpdb->prepare()` as `%i` identifiers

### Fixed

- Development only: `antecedent/patchwork` is required at commit `d588654` of its `master` branch (antecedent/patchwork#235), aliased as 2.2.6, so that Pest mutants are no longer counted as caught when tests hang. This pin is temporary until the next Patchwork release
- A file is treated as private when the database query that looks its media up fails, instead of public
- A personal data export archive that cannot be read is no longer copied to the disk as an empty file before a `TypeError`: `PrivacyExportFailedException` is thrown and the disk is left alone
- A write through `pollora://` no longer ends in a `TypeError` when the temporary directory cannot be written: the file is stored, only its local copy is given up
- Development only: `antecedent/patchwork` is required at commit `d588654` of its `master` branch (antecedent/patchwork#235), aliased as 2.2.6, so that Pest mutants are no longer counted as caught when tests hang. This pin is temporary until the next Patchwork release

## [0.10.0] - 2026-11-24

Expand All @@ -30,7 +74,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- **BREAKING**: `PolloraThumbnailDriver` takes the declared sizes (`ImageSizes`) as a third constructor argument and `ImgproxyThumbnailDriver` as its third, before the base URL (`new ImgproxyThumbnailDriver($signer, $config, $sizes, $baseUrl, $bucket)`), to recognise only declared sizes as `BunnyThumbnailDriver` already does. `ThumbnailDrivers::make()` and `DeclaredSizes` pass them; only code that builds these drivers by hand changes.

||||||| d54194b
- The unit suite loads its stand-in for WP-CLI from `tests/bootstrap.php` instead of `tests/Pest.php`, which the integration suite also loads (a real WordPress must not find a `WP_CLI` that is not WP-CLI). No change to the package.

### Security
Expand Down
55 changes: 53 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ add_filter('pollora/media/filesystem', fn () => new Filesystem(new SftpAdapter(/
| `wp_privacy_exports_dir`, `wp_privacy_exports_url`, `wp_privacy_personal_data_export_file_created`, `wp_privacy_delete_old_export_files` | The personal data export builds its archive locally (`ZipArchive` cannot write to a wrapper), copies it to the disk, links it through the public URL, and the old archives are deleted from the disk |

| `wp_get_attachment_url`, `wp_get_attachment_image_src`, `wp_calculate_image_srcset` | Private media only: the URL, the URL of a size and every source of a `srcset` are signed and expire. A public media keeps its plain URL, and nothing is asked of the disk to tell them apart |
| `wp_generate_attachment_metadata` | A media created private (see below) is private on the disk from the start |
| `wp_generate_attachment_metadata` | A media created private (see below) is made private on the disk |
| `wp_update_attachment_metadata`, `wp_edited_image_metadata` | Private media only: the files written since are made private, and the copy the editor makes of a private image is private |
| `rest_request_before_callbacks`, `rest_attachment_query`, `posts_where`, `posts_results`, `wp`, `redirect_canonical`, `oembed_request_post_id`, `wp_ajax_parse-media-shortcode` | Private media only (the last one: signed-in users who write nothing): [who WordPress tells about it](#who-wordpress-tells-about-a-private-media) |
| `wpmu_delete_blog_upload_dir` | On a network, nothing is deleted when the folder of the deleted site is the whole library |

The `pollora://` wrapper sends a file to the disk when its stream is closed, in one write. Directories do not exist on an object store, so they are virtual and answer without asking the disk (a path with an extension is a file); what is asked about a file is remembered for the rest of the request. A file written or read during a request, up to 16 MiB, is also kept in a local copy until the request ends: WordPress reads an uploaded image again for its size, its EXIF data, its sizes and its metadata, and none of it goes back to the disk. One upload of an image with nine sizes costs ten writes and a handful of existence checks; a read of the original, a stat or a listing never happens. Both the GD and the Imagick editors of WordPress work on the wrapper as they are.

Expand Down Expand Up @@ -157,7 +160,7 @@ A size is made at upload only if it is declared `eager()`. The same goes for an
- **S3 or compatible service behind a CDN.** The origin of the CDN for the library is the site: one rule at the CDN. PHP sees each file the first time the CDN asks, then the cache lasts a year.
- **S3 without a CDN.** There is no fallback: declare the sizes you use `eager()` and run `thumbs warm`. The URL stays the same.

What the URL says is not trusted. First, without asking the disk: the name is one of a declared size, the signature is the one the site would have made, the extension is an image's, and the path does not climb out of the library. Then, against the original: its dimensions are those the name gives (nobody can name a size of their own), it is not private, it is not a GIF. A request that fails a check gets a 404 and costs nothing.
What the URL says is not trusted. First, without asking the disk: the name is one of a declared size, the signature is the one the site would have made, the extension is an image's, and the path does not climb out of the library. Then, against the original, for a size already made too: it is still there and it is not private. Before a size is made: the dimensions of the original give those of the name (nobody can name a size of their own), and it is not a GIF. A request that fails a check gets a 404 and costs nothing.

Making a size costs CPU, so it is limited, per server: one process per file (ten simultaneous requests for the same missing size make one), at most `POLLORA_MEDIA_THUMBNAILS_CONCURRENCY` sizes at the same time (4), and an original above `POLLORA_MEDIA_THUMBNAILS_MAX_PIXELS` (50 million pixels, imgproxy's default) or `POLLORA_MEDIA_THUMBNAILS_MAX_BYTES` (30 MiB) gets no size at all: it stays downloadable. A request that cannot get a place within `POLLORA_MEDIA_THUMBNAILS_WAIT` seconds (10) is handed a size that already exists, the smallest that is at least as wide, the original at worst, with `Cache-Control: no-store`: the visitor sees an image, the CDN does not keep the stand-in, and the real size is made on a later request. The answer for a size made is `200` with `Cache-Control: public, max-age=31536000, immutable`, never a redirect.

Expand Down Expand Up @@ -200,6 +203,8 @@ wp pollora media thumbs export --driver=imgproxy # the configuration to

For Bunny, the classes are read, merged by name and written back: the ones you made are kept, nothing is deleted, and the forced classes setting is never changed. The merge matters: Bunny replaces the whole list of classes with the one it is sent. The access key is only ever sent in the `AccessKey` header. imgproxy has no API for presets: the command prints `IMGPROXY_PRESETS`, `IMGPROXY_ONLY_PRESETS=true`, `IMGPROXY_MAX_SRC_RESOLUTION` (in megapixels) and `IMGPROXY_ALLOWED_SOURCES`, never the key or the salt.

**Networks.** The declared sizes are made for the main site only: another site gets the sizes of WordPress, and the original under a declared name WordPress has no size for. On another site, the fallback route serves nothing and `thumbs prune` refuses to run. A network whose sites do not upload under `sites/N` (created before WordPress 3.5, or with an `upload_path` of its own) shares the root of the library, which is not supported: its sites can overwrite each other's files, and deleting one deletes nothing from the disk.

## Private media

A media is private when its post meta `_pollora_media_private` is `1`, or when the `pollora/media/is_private` filter says so. A private media is served through a signed URL (`Flysystem`'s `temporaryUrl()`: an S3 pre-signed URL) that expires after `POLLORA_MEDIA_PRIVATE_EXPIRY` (`+6 hours` by default). The flag is read with the attachment and asked once per media in a request: the signature is computed for private media only, and no call to the disk is made to tell a private media from a public one.
Expand Down Expand Up @@ -234,6 +239,52 @@ Hooks to build on it:

A disk that cannot sign a URL (a plain local disk) serves the plain URL of a private media, rather than a broken one: a private media needs an object store, or a signature of your own through `pollora/media/signed_url`.

A file written for a media that is already private is written with the visibility of the disk, then made private when the metadata of the media is saved. A file a plugin writes without saving the metadata stays as written: declare it with `pollora/media/attachment_files` and run the command above. A flag set directly in the post meta, or a filter whose answer changes, does not touch the disk by itself either.

Visibility is the one the disk keeps for each file (on S3, the ACL of the object). A bucket with ACLs disabled (*Bucket owner enforced*), a bucket policy or a CDN that makes every object public, or a provider without object ACLs keeps a private media readable by its plain URL, whatever the module does.

### Who WordPress tells about a private media

WordPress shows a media attached to no post to anyone, with its URL, and that URL is signed for whoever asks. The module keeps a private media to the users who can upload files (`upload_files`, the ones the media library shows every media to) and to whoever can edit that media:

- over REST, `/wp/v2/media/{id}` and what lies under it answers `401` (not logged in) or `403`, and the collection leaves the media out, an `_embed` included;
- the attachment page, its embed and its feed are not found, however the request names the media, and WordPress does not redirect to its slug or its file (one exception: a request to `wp-trackback.php` that names the media can still redirect to its slug, where nothing is found);
- oEmbed does not answer about it.

WordPress also renders any shortcode a signed-in user sends to `admin-ajax.php?action=parse-media-shortcode`: only whoever can upload files, or edit the post the request names, gets an answer.

This is not access control over the content that uses the media: a published page that displays a private media hands its signed URL to every visitor, and so does a theme or a plugin that lists attachments. Whoever can write content, a contributor included, can display a private media in it by its id (a gallery in a draft and its preview, for instance). A headless front-end that reads private media over REST has to authenticate as a user who may. With a `pollora/media/is_private` rule, the totals of a REST collection (`X-WP-Total`) still count the media the rule hides.

### Repairing media left public

Up to 0.10.0, some files of private media were left public (see the CHANGELOG). Upgrading changes nothing on the disk by itself: making each private media private again changes every file of it, and can be repeated.

```bash
# The media flagged private, the trashed ones included
for id in $(wp post list --post_type=attachment --post_status=inherit,private,trash --meta_key=_pollora_media_private --meta_value=1 --field=ID); do
wp pollora media visibility "$id" --private || echo "Not repaired: media $id"
done

# Only with a `pollora/media/is_private` rule: the media private by the rule. It looks at every media, which takes
# a while on a large library. The command also flags them, which a rule that lets a media become public again has
# to take into account
for id in $(wp post list --post_type=attachment --post_status=inherit,private,trash --field=ID); do
if wp pollora media files "$id" | grep "Media $id is private" > /dev/null; then
wp pollora media visibility "$id" --private || echo "Not repaired: media $id"
fi
done
```

A file the command reports as not changed is missing from the disk, or is still as it was: check it. Run the first loop again after a `migrate`, since files copied from a local folder keep the visibility they had there.

Then:

- `wp pollora media thumbs prune` lists the files of the declared sizes that were changed or removed since, which no command can attribute to a media; `--execute` deletes them. On a network, run it from the main site only.
- On a network, run the loops once per site (`--url=<site>`). Up to 0.10.0, making private a media of another site changed the files of the main site that have the same names: for a media of the main site whose files are private by mistake, `wp pollora media visibility <id> --public`.
- Purge the CDN, which may keep what it already read.

The files of the declared sizes made on demand for an image as it was before an edit stay on the disk once the media is deleted. The module no longer serves them, but a web server or a bucket that makes files public does.

## Commands

```bash
Expand Down
Loading
Loading