Skip to content

Conversation

saho-odoo
Copy link
Contributor

Changing a product template's image with the can_image_1024_be_zoomed field present in the view produces a crash on save.

STEPS TO REPRODUCE

  1. Open a product form view
  2. Using studio, add the can_image_1024_be_zoomed field
  3. Change the image and save

The product will be updated, but this error will be thrown:

binascii.Error: Incorrect padding

CAUSE

def _compute_can_image_1024_be_zoomed(self):
for template in self:
template.can_image_1024_be_zoomed = template.image_1920 and tools.is_image_size_above(template.image_1920, template.image_1024)

In the RecordSet's context, bin_size=True, so accessing image_<size> returns a size instead of a binary. _compute_can_image_1024_be_zoomed calls tools.is_image_size_above with this binary size, while the method expects the full base64 encoded image. Ultimately, this causes base64.b64decode to crash with the above error.

FIX Update the RecordSet's context to bin_size=False. Accessing image_<size> from the RecordSet will then return the full binary instead of the size.

NOTES
In versions <17.0, read and write were done in separate network calls causing the recompute to happen during the flush of the write, which doesn't have bin_size=True in the context. Because the field is stored, the read will not cause a recompute and thus avoids any error. In >=17.0, because the read occurs before the flush, and the write has invalidated the field, the recompute will occur with the context of the read (bin_size=True), causing the issue.

opw-4413647

Changing a product template's image with the `can_image_1024_be_zoomed`
field present in the view produces a crash on save.

**STEPS TO REPRODUCE**
1. Open a product form view
2. Using studio, add the `can_image_1024_be_zoomed` field
3. Change the image and save

The product will be updated, but this error will be thrown:

```
binascii.Error: Incorrect padding
```

**CAUSE**
https://github.com/odoo/odoo/blob/a74684777c27e26639b75b0668bb15afadb78c4f/addons/product/models/product_template.py#L186-L188

In the `RecordSet`'s context, `bin_size=True`, so accessing
`image_<size>` returns a size instead of a binary.
`_compute_can_image_1024_be_zoomed` calls `tools.is_image_size_above`
with this binary size, while the method expects the full base64 encoded
image. Ultimately, this causes `base64.b64decode` to crash with the
above error.

**FIX** Update the `RecordSet`'s context to `bin_size=False`. Accessing
`image_<size>` from the `RecordSet` will then return the full binary
instead of the size.

**NOTES**
In versions <17.0, read and write were done in separate network calls
causing the recompute to happen during the flush of the write, which
doesn't have `bin_size=True` in the context. Because the field is
stored, the read will not cause a recompute and thus avoids any error.
In >=17.0, because the read occurs before the flush, and the write has
invalidated the field, the recompute will occur with the context of the
read (`bin_size=True`), causing the issue.

opw-4413647
@robodoo
Copy link
Contributor

robodoo commented Dec 30, 2024

Pull request status dashboard

@C3POdoo C3POdoo requested a review from a team December 30, 2024 16:49
@C3POdoo C3POdoo added the OE the report is linked to a support ticket (opw-...) label Dec 30, 2024
Copy link
Contributor

@Feyensv Feyensv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly opposed to this change, though it's kinda strange we have to fix a 'framework' issue in the business logic (cc @rco-odoo).

Do we know why the customer added the field in their view ?

@robodoo delegate+

@saho-odoo
Copy link
Contributor Author

I advised the customer that they can remove it but they haven't responded. Ticket for more context: https://www.odoo.com/odoo/project/49/tasks/4413647

That being said, this error could be triggered any time this compute method is invoked with bin_size=True in the context. Since this compute relies on reading specifically the binary data, and what is read for binary and image fields is context-dependent, it seems wise to force the context when expecting to read the data specifically. I'll leave it to the python framework team to decide if something more generic should be done about this. Thanks for the review!

@robodoo r+

robodoo pushed a commit that referenced this pull request Jan 3, 2025
Changing a product template's image with the `can_image_1024_be_zoomed`
field present in the view produces a crash on save.

**STEPS TO REPRODUCE**
1. Open a product form view
2. Using studio, add the `can_image_1024_be_zoomed` field
3. Change the image and save

The product will be updated, but this error will be thrown:

```
binascii.Error: Incorrect padding
```

**CAUSE**
https://github.com/odoo/odoo/blob/a74684777c27e26639b75b0668bb15afadb78c4f/addons/product/models/product_template.py#L186-L188

In the `RecordSet`'s context, `bin_size=True`, so accessing
`image_<size>` returns a size instead of a binary.
`_compute_can_image_1024_be_zoomed` calls `tools.is_image_size_above`
with this binary size, while the method expects the full base64 encoded
image. Ultimately, this causes `base64.b64decode` to crash with the
above error.

**FIX** Update the `RecordSet`'s context to `bin_size=False`. Accessing
`image_<size>` from the `RecordSet` will then return the full binary
instead of the size.

**NOTES**
In versions <17.0, read and write were done in separate network calls
causing the recompute to happen during the flush of the write, which
doesn't have `bin_size=True` in the context. Because the field is
stored, the read will not cause a recompute and thus avoids any error.
In >=17.0, because the read occurs before the flush, and the write has
invalidated the field, the recompute will occur with the context of the
read (`bin_size=True`), causing the issue.

opw-4413647

closes #192047

Signed-off-by: Samiul Hoque (saho) <saho@odoo.com>
@robodoo robodoo closed this Jan 3, 2025
@fw-bot fw-bot deleted the 17.0-opw-4413647-saho branch January 17, 2025 18:26
BurkhalterY pushed a commit to Burkhalter-IT/odoo that referenced this pull request Jan 28, 2025
Changing a product template's image with the `can_image_1024_be_zoomed`
field present in the view produces a crash on save.

**STEPS TO REPRODUCE**
1. Open a product form view
2. Using studio, add the `can_image_1024_be_zoomed` field
3. Change the image and save

The product will be updated, but this error will be thrown:

```
binascii.Error: Incorrect padding
```

**CAUSE**
https://github.com/odoo/odoo/blob/a74684777c27e26639b75b0668bb15afadb78c4f/addons/product/models/product_template.py#L186-L188

In the `RecordSet`'s context, `bin_size=True`, so accessing
`image_<size>` returns a size instead of a binary.
`_compute_can_image_1024_be_zoomed` calls `tools.is_image_size_above`
with this binary size, while the method expects the full base64 encoded
image. Ultimately, this causes `base64.b64decode` to crash with the
above error.

**FIX** Update the `RecordSet`'s context to `bin_size=False`. Accessing
`image_<size>` from the `RecordSet` will then return the full binary
instead of the size.

**NOTES**
In versions <17.0, read and write were done in separate network calls
causing the recompute to happen during the flush of the write, which
doesn't have `bin_size=True` in the context. Because the field is
stored, the read will not cause a recompute and thus avoids any error.
In >=17.0, because the read occurs before the flush, and the write has
invalidated the field, the recompute will occur with the context of the
read (`bin_size=True`), causing the issue.

opw-4413647

closes odoo#192047

Signed-off-by: Samiul Hoque (saho) <saho@odoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OE the report is linked to a support ticket (opw-...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants