Affects Version/s: 12.10.6
Fix Version/s: None
Component/s: Old Core
XWikiDocument.getAttachmentDiff is currently using XWikiDocument.retrieveDeletedAttachment (https://github.com/xwiki/xwiki-platform/blob/c8ef54e5edad98883ec37bea18edb78c39215348/xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocument.java#L6776-L6786) and the way that it is currently implemented leads to an inconsistent state simply because the retrieved attachment from the archive is not guaranteed to be the requested one.
The current logic just retrieves a deleted attachment that was:
- not deleted before the modification date of the requested attachment and
- that has the same version as the deleted attachment.
A check on the file name would have been helpful, as in my test, the code was looking for the just deleted attachment, but it returned the one deleted earlier, with a different file name, simply because they both had version 1.1. It is also possible that there can be multiple version 1.1 deleted attachments in the archive, even for the same file name.
To be fair, even if the file name was right, there is still no guarantee that you really get the attachment you expect, as long as you are not using deleted attachment IDs (which are kind of inaccessible at this point in the code flow). The entire approach is sketchy and can lead to weird results.
So, if you are using the AttachmentDiff resulting objects, you can be surprised to find that the file name says it belongs to the attachment you asked for, but if you call getOriginalAttachment() on it, you can get an XWikiAttachment corresponding to another deleted attachment file.
All of the above have been observed while debugging the behavior of AttachmentEventGeneratorListener when deleting an attachment, for XWIKI-18775.