From cdfb3a80bd40b1d6c08919504a705ac4491defe6 Mon Sep 17 00:00:00 2001
From: omkar podey <58183-omkar.podey@users.noreply.drupalcode.org>
Date: Thu, 18 May 2023 12:07:48 +0000
Subject: [PATCH] Issue #3360763 by omkar.podey, tedbow, Wim Leers: Switch
 failure marker file from *.json to *.yml to prevent it from being readable
 from the web

---
 package_manager/src/FailureMarker.php               | 13 +++++++------
 .../tests/src/Kernel/FailureMarkerTest.php          |  8 ++++----
 .../tests/src/Traits/AssertPreconditionsTrait.php   |  2 +-
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/package_manager/src/FailureMarker.php b/package_manager/src/FailureMarker.php
index c351c6f124..103232922c 100644
--- a/package_manager/src/FailureMarker.php
+++ b/package_manager/src/FailureMarker.php
@@ -4,7 +4,8 @@ declare(strict_types = 1);
 
 namespace Drupal\package_manager;
 
-use Drupal\Component\Serialization\Json;
+use Symfony\Component\Yaml\Exception\ParseException;
+use Symfony\Component\Yaml\Yaml;
 use Drupal\Core\StringTranslation\TranslatableMarkup;
 use Drupal\package_manager\Exception\StageFailureMarkerException;
 
@@ -40,7 +41,7 @@ final class FailureMarker {
    *   The absolute path of the marker file.
    */
   public function getPath(): string {
-    return $this->pathLocator->getProjectRoot() . '/PACKAGE_MANAGER_FAILURE.json';
+    return $this->pathLocator->getProjectRoot() . '/PACKAGE_MANAGER_FAILURE.yml';
   }
 
   /**
@@ -62,9 +63,9 @@ final class FailureMarker {
     $data = [
       'stage_class' => get_class($stage),
       'stage_file' => (new \ReflectionObject($stage))->getFileName(),
-      'message' => $message,
+      'message' => $message->render(),
     ];
-    file_put_contents($this->getPath(), Json::encode($data));
+    file_put_contents($this->getPath(), Yaml::dump($data));
   }
 
   /**
@@ -79,9 +80,9 @@ final class FailureMarker {
     if (file_exists($path)) {
       $data = file_get_contents($path);
       try {
-        $data = json_decode($data, TRUE, flags: JSON_THROW_ON_ERROR);
+        $data = Yaml::parse($data);
       }
-      catch (\JsonException $exception) {
+      catch (ParseException $exception) {
         throw new StageFailureMarkerException('Failure marker file exists but cannot be decoded.', $exception->getCode(), $exception);
       }
 
diff --git a/package_manager/tests/src/Kernel/FailureMarkerTest.php b/package_manager/tests/src/Kernel/FailureMarkerTest.php
index 5e524543eb..c246845964 100644
--- a/package_manager/tests/src/Kernel/FailureMarkerTest.php
+++ b/package_manager/tests/src/Kernel/FailureMarkerTest.php
@@ -29,14 +29,14 @@ class FailureMarkerTest extends PackageManagerKernelTestBase {
   }
 
   /**
-   * Tests that an exception is thrown if the marker file contains invalid JSON.
+   * Tests that an exception is thrown if the marker file contains invalid YAML.
    *
    * @covers ::assertNotExists
    */
-  public function testExceptionForInvalidJson(): void {
+  public function testExceptionForInvalidYaml(): void {
     $failure_marker = $this->container->get(FailureMarker::class);
-    // Write the failure marker with invalid JSON.
-    file_put_contents($failure_marker->getPath(), '{}}');
+    // Write the failure marker with invalid YAML.
+    file_put_contents($failure_marker->getPath(), 'message : something message : something1');
 
     $this->expectException(StageFailureMarkerException::class);
     $this->expectExceptionMessage('Failure marker file exists but cannot be decoded.');
diff --git a/package_manager/tests/src/Traits/AssertPreconditionsTrait.php b/package_manager/tests/src/Traits/AssertPreconditionsTrait.php
index 1f6bfb3d4b..b2aa5e6c9f 100644
--- a/package_manager/tests/src/Traits/AssertPreconditionsTrait.php
+++ b/package_manager/tests/src/Traits/AssertPreconditionsTrait.php
@@ -75,7 +75,7 @@ trait AssertPreconditionsTrait {
     // If the failure marker exists, it will be in the project root. The project
     // root is defined as the directory containing the `vendor` directory.
     // @see \Drupal\package_manager\FailureMarker::getPath()
-    $failure_marker = static::getProjectRoot() . '/PACKAGE_MANAGER_FAILURE.json';
+    $failure_marker = static::getProjectRoot() . '/PACKAGE_MANAGER_FAILURE.yml';
     if (file_exists($failure_marker)) {
       $suffix = $when === 'before'
         ? 'Remove it to continue.'
-- 
GitLab