Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Merge remote-tracking branch 'origin/next' into s3-restore

Andras Bacsai6d8144c18c8d
diff --cc bootstrap/helpers/shared.php
index 9e69906ac8,8a278476ed..29667a581e
--- a/bootstrap/helpers/shared.php
+++ b/bootstrap/helpers/shared.php
@@@ -3154,116 -3154,45 +3154,159 @@@ function generateDockerComposeServiceNa
return $collection;
}
+function formatBytes(?int $bytes, int $precision = 2): string
+{
+ if ($bytes === null || $bytes === 0) {
+ return '0 B';
+ }
+
+ // Handle negative numbers
+ if ($bytes < 0) {
+ return '0 B';
+ }
+
+ $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
+ $base = 1024;
+ $exponent = floor(log($bytes) / log($base));
+ $exponent = min($exponent, count($units) - 1);
+
+ $value = $bytes / pow($base, $exponent);
+
+ return round($value, $precision).' '.$units[$exponent];
+}
+
+/**
+ * Validates that a file path is safely within the /tmp/ directory.
+ * Protects against path traversal attacks by resolving the real path
+ * and verifying it stays within /tmp/.
+ *
+ * Note: On macOS, /tmp is often a symlink to /private/tmp, which is handled.
+ */
+function isSafeTmpPath(?string $path): bool
+{
+ if (blank($path)) {
+ return false;
+ }
+
+ // URL decode to catch encoded traversal attempts
+ $decodedPath = urldecode($path);
+
+ // Minimum length check - /tmp/x is 6 chars
+ if (strlen($decodedPath) < 6) {
+ return false;
+ }
+
+ // Must start with /tmp/
+ if (! str($decodedPath)->startsWith('/tmp/')) {
+ return false;
+ }
+
+ // Quick check for obvious traversal attempts
+ if (str($decodedPath)->contains('..')) {
+ return false;
+ }
+
+ // Check for null bytes (directory traversal technique)
+ if (str($decodedPath)->contains("\0")) {
+ return false;
+ }
+
+ // Remove any trailing slashes for consistent validation
+ $normalizedPath = rtrim($decodedPath, '/');
+
+ // Normalize the path by removing redundant separators and resolving . and ..
+ // We'll do this manually since realpath() requires the path to exist
+ $parts = explode('/', $normalizedPath);
+ $resolvedParts = [];
+
+ foreach ($parts as $part) {
+ if ($part === '' || $part === '.') {
+ // Skip empty parts (from //) and current directory references
+ continue;
+ } elseif ($part === '..') {
+ // Parent directory - this should have been caught earlier but double-check
+ return false;
+ } else {
+ $resolvedParts[] = $part;
+ }
+ }
+
+ $resolvedPath = '/'.implode('/', $resolvedParts);
+
+ // Final check: resolved path must start with /tmp/
+ // And must have at least one component after /tmp/
+ if (! str($resolvedPath)->startsWith('/tmp/') || $resolvedPath === '/tmp') {
+ return false;
+ }
+
+ // Resolve the canonical /tmp path (handles symlinks like /tmp -> /private/tmp on macOS)
+ $canonicalTmpPath = realpath('/tmp');
+ if ($canonicalTmpPath === false) {
+ // If /tmp doesn't exist, something is very wrong, but allow non-existing paths
+ $canonicalTmpPath = '/tmp';
+ }
+
+ // Calculate dirname once to avoid redundant calls
+ $dirPath = dirname($resolvedPath);
+
+ // If the directory exists, resolve it via realpath to catch symlink attacks
+ if (file_exists($resolvedPath) || is_dir($dirPath)) {
+ // For existing paths, resolve to absolute path to catch symlinks
+ if (is_dir($dirPath)) {
+ $realDir = realpath($dirPath);
+ if ($realDir === false) {
+ return false;
+ }
+
+ // Check if the real directory is within /tmp (or its canonical path)
+ if (! str($realDir)->startsWith('/tmp') && ! str($realDir)->startsWith($canonicalTmpPath)) {
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
++
+ /**
+ * Transform colon-delimited status format to human-readable parentheses format.
+ *
+ * Handles Docker container status formats with optional health check status and exclusion modifiers.
+ *
+ * Examples:
+ * - running:healthy → Running (healthy)
+ * - running:unhealthy:excluded → Running (unhealthy, excluded)
+ * - exited:excluded → Exited (excluded)
+ * - Proxy:running → Proxy:running (preserved as-is for headline formatting)
+ * - running → Running
+ *
+ * @param string $status The status string to format
+ * @return string The formatted status string
+ */
+ function formatContainerStatus(string $status): string
+ {
+ // Preserve Proxy statuses as-is (they follow different format)
+ if (str($status)->startsWith('Proxy')) {
+ return str($status)->headline()->value();
+ }
+
+ // Check for :excluded suffix
+ $isExcluded = str($status)->endsWith(':excluded');
+ $parts = explode(':', $status);
+
+ if ($isExcluded) {
+ if (count($parts) === 3) {
+ // Has health status: running:unhealthy:excluded → Running (unhealthy, excluded)
+ return str($parts[0])->headline().' ('.$parts[1].', excluded)';
+ } else {
+ // No health status: exited:excluded → Exited (excluded)
+ return str($parts[0])->headline().' (excluded)';
+ }
+ } elseif (count($parts) >= 2) {
+ // Regular colon format: running:healthy → Running (healthy)
+ return str($parts[0])->headline().' ('.$parts[1].')';
+ } else {
+ // Simple status: running → Running
+ return str($status)->headline()->value();
+ }
+ }