runInternal($context); } catch (\Throwable $e) { // 업그레이드 중단 금지 — 잔존 파일 정리 실패가 업그레이드 전체를 막지 않는다. $context->logger->warning(sprintf( '[7.0.4] RedactResidualInstallerStateSecrets 실패 (state.json 보존, 계속 진행): %s', $e->getMessage(), )); Log::warning('7.0.4 RedactResidualInstallerStateSecrets 실패', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); } } private function runInternal(UpgradeContext $context): void { $basePath = base_path(); $statePath = $basePath.DIRECTORY_SEPARATOR.self::STATE_JSON_RELATIVE; if (! is_file($statePath)) { $context->logger->info('[7.0.4] installer-state.json 부재 — 정리 대상 없음, skip'); return; } $state = $this->readStateJson($statePath); if ($state === null) { $context->logger->warning( '[7.0.4] installer-state.json 파싱 실패 — 파일 보존 (파괴 금지). ' .'평문 비밀이 남아 있을 수 있으니 수동 검토 후 삭제 권장: '.$statePath ); return; } if ($this->installationIsComplete($basePath)) { if (@unlink($statePath)) { $context->logger->info('[7.0.4] 설치 완료 확인 — 잔존 installer-state.json 삭제 완료'); return; } $context->logger->warning(sprintf( '[7.0.4] installer-state.json 삭제 실패 (%s) — 비밀 필드 redact 로 폴백', error_get_last()['message'] ?? 'unknown', )); $this->redactAndSave($statePath, $state, $context); return; } // 완료 증거 없음 — 설치가 진행 중이거나 중단된 상태일 수 있으므로 파일을 삭제하면 // 진행 중인 설치를 파괴한다. 비밀만 제거하고 파일은 보존한다. $context->logger->info('[7.0.4] 설치 완료 증거 없음 — 파일 보존, 비밀 필드만 redact'); $this->redactAndSave($statePath, $state, $context); } /** * state 배열에서 비밀 4종을 제거한 뒤 재저장한다. * * 이미 비밀이 없으면 쓰기를 건너뛴다 (멱등 + 불필요한 mtime 갱신 회피). * * @param array $state */ private function redactAndSave(string $statePath, array $state, UpgradeContext $context): void { if (! isset($state['config']) || ! is_array($state['config'])) { $context->logger->info('[7.0.4] installer-state.json 에 config 섹션 없음 — redact 불필요'); return; } $removed = []; foreach (self::SECRET_CONFIG_KEYS as $key) { if (array_key_exists($key, $state['config'])) { unset($state['config'][$key]); $removed[] = $key; } } if ($removed === []) { $context->logger->info('[7.0.4] installer-state.json 에 잔존 비밀 없음 — redact 불필요 (멱등)'); return; } $encoded = json_encode($state, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); if ($encoded === false) { $context->logger->warning('[7.0.4] installer-state.json 재인코딩 실패 — 파일 보존'); return; } // file_put_contents — 기존 inode 의 권한·소유자를 그대로 유지 (CLI 컨텍스트에서 // tmp+rename 하면 새 inode 가 CLI 사용자 소유가 되어 웹 서버 읽기가 깨질 수 있다). if (@file_put_contents($statePath, $encoded, LOCK_EX) === false) { $context->logger->warning(sprintf( '[7.0.4] installer-state.json redact 재저장 실패 (%s) — 평문 잔존. 수동 삭제 권장: %s', error_get_last()['message'] ?? 'unknown', $statePath, )); return; } $context->logger->info(sprintf( '[7.0.4] installer-state.json 비밀 필드 redact 완료 — 제거된 키: %s', implode(', ', $removed), )); } /** * state.json 을 읽어 배열로 반환. 파싱 실패 시 null. * * @return array|null */ private function readStateJson(string $statePath): ?array { $content = @file_get_contents($statePath); if ($content === false) { return null; } $decoded = json_decode($content, true); return is_array($decoded) ? $decoded : null; } /** * 설치 완료 증거 판정. * * `.env` 의 INSTALLER_COMPLETED 가 truthy 이거나 storage/app/g7_installed 가 존재하면 * 설치가 끝난 것으로 본다 (installer-state.php 의 isInstallationCompleted() 와 동일 신호). */ private function installationIsComplete(string $basePath): bool { if (is_file($basePath.DIRECTORY_SEPARATOR.self::INSTALLED_FLAG_RELATIVE)) { return true; } return $this->envInstallerCompletedIsTrue($basePath.DIRECTORY_SEPARATOR.'.env'); } /** * `.env` 의 INSTALLER_COMPLETED 가 truthy 값인지 판정. * * `_guard.php` 의 installer_finalize_is_completed() 와 동일 정책. */ private function envInstallerCompletedIsTrue(string $envPath): bool { if (! is_file($envPath)) { return false; } $parsed = @parse_ini_file($envPath, false, INI_SCANNER_RAW); if (! is_array($parsed)) { return false; } $flag = strtolower(trim((string) ($parsed['INSTALLER_COMPLETED'] ?? ''))); $flag = trim($flag, "\"'"); return in_array($flag, ['true', '1', 'yes'], true); } }