Skip to content

Commit 1ff54db

Browse files
Transpile 26b4b60
1 parent f542046 commit 1ff54db

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

.changeset/warm-geese-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-solidity': patch
3+
---
4+
5+
`Base64`: Fix issue where dirty memory located just after the input buffer is affecting the result.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
6+
import {Initializable} from "../proxy/utils/Initializable.sol";
7+
8+
contract Base64DirtyUpgradeable is Initializable {
9+
struct A {
10+
uint256 value;
11+
}
12+
13+
function __Base64Dirty_init() internal onlyInitializing {
14+
}
15+
16+
function __Base64Dirty_init_unchained() internal onlyInitializing {
17+
}
18+
function encode(bytes memory input) public pure returns (string memory) {
19+
A memory unused = A({value: type(uint256).max});
20+
// To silence warning
21+
unused;
22+
23+
return Base64.encode(input);
24+
}
25+
}

contracts/mocks/WithInit.sol

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ contract AuthoritiyObserveIsConsumingUpgradeableWithInit is AuthoritiyObserveIsC
142142
__AuthoritiyObserveIsConsuming_init();
143143
}
144144
}
145+
import "./Base64DirtyUpgradeable.sol";
146+
147+
contract Base64DirtyUpgradeableWithInit is Base64DirtyUpgradeable {
148+
constructor() payable initializer {
149+
__Base64Dirty_init();
150+
}
151+
}
145152
import "./CallReceiverMockUpgradeable.sol";
146153

147154
contract CallReceiverMockUpgradeableWithInit is CallReceiverMockUpgradeable {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/utils/Base64.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const { expect } = require('chai');
22

33
const Base64 = artifacts.require('$Base64');
4+
const Base64Dirty = artifacts.require('$Base64Dirty');
45

5-
contract('Strings', function () {
6+
contract('Base64', function () {
67
beforeEach(async function () {
78
this.base64 = await Base64.new();
89
});
@@ -30,4 +31,13 @@ contract('Strings', function () {
3031
expect(await this.base64.$encode([])).to.equal('');
3132
});
3233
});
34+
35+
it('Encode reads beyond the input buffer into dirty memory', async function () {
36+
const mock = await Base64Dirty.new();
37+
const buffer32 = Buffer.from(web3.utils.soliditySha3('example').replace(/0x/, ''), 'hex');
38+
const buffer31 = buffer32.slice(0, -2);
39+
40+
expect(await mock.encode(buffer31)).to.equal(buffer31.toString('base64'));
41+
expect(await mock.encode(buffer32)).to.equal(buffer32.toString('base64'));
42+
});
3343
});

0 commit comments

Comments
 (0)