Skip to content

[make:user] Hash passwords using crc32c and deprecate eraseCredentials() #1703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/http-client": "^6.4|^7.0",
"symfony/phpunit-bridge": "^6.4.1|^7.0",
"symfony/security-core": "^6.4|^7.0",
"symfony/security-http": "^6.4|^7.0",
"symfony/yaml": "^6.4|^7.0",
"twig/twig": "^3.0|^4.x-dev"
},
Expand Down
82 changes: 76 additions & 6 deletions src/Security/UserClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\IsGrantedContext;

/**
* Adds logic to implement UserInterface to an existing User class.
Expand All @@ -37,7 +38,13 @@ public function addUserInterfaceImplementation(ClassSourceManipulator $manipulat

$this->addPasswordImplementation($manipulator, $userClassConfig);

$this->addEraseCredentials($manipulator);
if (class_exists(IsGrantedContext::class)) {
$this->addSerialize($manipulator);
}

if (method_exists(UserInterface::class, 'eraseCredentials')) {
$this->addEraseCredentials($manipulator);
}
}

private function addPasswordImplementation(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig): void
Expand Down Expand Up @@ -245,17 +252,80 @@ private function addEraseCredentials(ClassSourceManipulator $manipulator): void
$builder = $manipulator->createMethodBuilder(
'eraseCredentials',
'void',
false,
['@see UserInterface']
false
);
$builder->addAttribute(new Node\Attribute(new Node\Name('\Deprecated')));
$builder->addStmt(
$manipulator->createMethodLevelCommentNode(
'If you store any temporary, sensitive data on the user, clear it here'
'@deprecated, to be removed when upgrading to Symfony 8'
)
);

$manipulator->addMethodBuilder($builder);
}

private function addSerialize(ClassSourceManipulator $manipulator): void
{
$builder = $manipulator->createMethodBuilder(
'__serialize',
'array',
false,
[
'Ensure the session doesn\'t contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.',
]
);

// $data = (array) $this;
$builder->addStmt(
$manipulator->createMethodLevelCommentNode(
'$this->plainPassword = null;'
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\Variable('data'),
new Node\Expr\Cast\Array_(
new Node\Expr\Variable('this')
)
)
)
);

// $data["\0".self::class."\0password"] = hash('crc32c', $this->password);
$builder->addStmt(
new Node\Stmt\Expression(
new Node\Expr\Assign(
new Node\Expr\ArrayDimFetch(
new Node\Expr\Variable('data'),
new Node\Expr\BinaryOp\Concat(
new Node\Expr\BinaryOp\Concat(
new Node\Scalar\String_("\0", ['kind' => Node\Scalar\String_::KIND_DOUBLE_QUOTED]),
new Node\Expr\ClassConstFetch(
new Node\Name('self'),
'class'
)
),
new Node\Scalar\String_("\0password", ['kind' => Node\Scalar\String_::KIND_DOUBLE_QUOTED]),
)
),
new Node\Expr\FuncCall(
new Node\Name('hash'),
[
new Node\Arg(new Node\Scalar\String_('crc32c')),
new Node\Arg(
new Node\Expr\PropertyFetch(
new Node\Expr\Variable('this'),
'password'
)
),
]
)
)
)
);

$builder->addStmt(new Node\Stmt\Nop());

// return $data;
$builder->addStmt(
new Node\Stmt\Return_(
new Node\Expr\Variable('data')
)
);

Expand Down
1 change: 1 addition & 0 deletions src/Util/PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function pStmt_ClassMethod(Stmt\ClassMethod $node): string
if ($node->returnType) {
$classMethod = str_replace(') :', '):', $classMethod);
}
$classMethod = str_replace('\x00', '\0', $classMethod);

return $classMethod;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Security/UserClassBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Bundle\MakerBundle\Security\UserClassBuilder;
use Symfony\Bundle\MakerBundle\Security\UserClassConfiguration;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Attribute\IsGrantedContext;

class UserClassBuilderTest extends TestCase
{
Expand All @@ -31,6 +33,14 @@ public function testAddUserInterfaceImplementation(UserClassConfiguration $userC
$expectedPath = $this->getExpectedPath($expectedFilename, null);
$expectedSource = file_get_contents($expectedPath);

if (!class_exists(IsGrantedContext::class)) {
$expectedSource = preg_replace('/\n\n(.+\n)+.+function __serialize[^}]++}/', '', $expectedSource);
}

if (!method_exists(UserInterface::class, 'eraseCredentials')) {
$expectedSource = preg_replace('/\n\n(.+\n)+.+function eraseCredentials[^}]++}/', '', $expectedSource);
}

self::assertSame($expectedSource, $manipulator->getSourceCode());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,19 @@ public function setPassword(string $password): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
14 changes: 11 additions & 3 deletions tests/Security/fixtures/expected/UserEntityWithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ public function setPassword(string $password): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ public function setPassword(string $password): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
14 changes: 11 additions & 3 deletions tests/Security/fixtures/expected/UserEntityWithoutPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,19 @@ public function setRoles(array $roles): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ public function setPassword(string $password): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
14 changes: 11 additions & 3 deletions tests/Security/fixtures/expected/UserModelWithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,19 @@ public function setPassword(string $password): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}
14 changes: 11 additions & 3 deletions tests/Security/fixtures/expected/UserModelWithoutPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,19 @@ public function setRoles(array $roles): static
}

/**
* @see UserInterface
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);

return $data;
}

#[\Deprecated]
public function eraseCredentials(): void
{
// If you store any temporary, sensitive data on the user, clear it here
// $this->plainPassword = null;
// @deprecated, to be removed when upgrading to Symfony 8
}
}