@stagepass/cli 2.4.1 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -16
- package/package.json +1 -1
- package/src/cli.js +3 -1
- package/src/commands/dev.js +11 -2
- package/src/commands/new.js +10 -3
- package/src/commands/start.js +23 -1
- package/src/commands/upgrade.js +146 -43
- package/src/lib/ai-guide.js +497 -20
- package/src/lib/build-script-upgrade.js +24 -10
- package/src/lib/cdn-base.js +20 -0
- package/src/lib/cdn-snippets.js +7 -5
- package/src/lib/doctor-site.js +17 -0
- package/src/lib/live-server.js +141 -34
- package/src/lib/project-sync.js +527 -32
- package/src/lib/registry.js +8 -6
- package/src/lib/scaffold.js +38 -5
- package/src/lib/snippets.js +235 -29
- package/src/lib/versions.js +9 -8
- package/templates/project/AGENTS.md.tmpl +2 -35
- package/templates/project/README.md.tmpl +4 -2
- package/templates/project/api/README.md.tmpl +16 -195
- package/templates/project/api/action.php +11 -2
- package/templates/project/api/env.example +11 -1
- package/templates/project/api/lib/config.php +5 -1
- package/templates/project/api/lib/request.php +366 -3
- package/templates/project/api/lib/security.php +116 -34
- package/templates/project/php-private/config.php +2 -0
|
@@ -47,68 +47,150 @@ function honeypot_is_filled(array $input, string $field): bool
|
|
|
47
47
|
return $field !== '' && input_string($input, $field) !== '';
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
/** Swallow storage warnings so they cannot become HTTP 500. */
|
|
51
|
+
function rate_limit_call(callable $fn)
|
|
52
|
+
{
|
|
53
|
+
set_error_handler(static function (): bool {
|
|
54
|
+
return true;
|
|
55
|
+
});
|
|
56
|
+
try {
|
|
57
|
+
return $fn();
|
|
58
|
+
} finally {
|
|
59
|
+
restore_error_handler();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
50
63
|
/**
|
|
51
64
|
* File-based fixed-window limiter, keyed by IP and action.
|
|
65
|
+
* Storage faults fail closed unless APP_ENV is local, development, or dev.
|
|
52
66
|
*
|
|
53
67
|
* @return bool True when the request may proceed.
|
|
54
68
|
*/
|
|
55
|
-
function rate_limit_allows(string $ip, string $action, int $max, int $window): bool
|
|
69
|
+
function rate_limit_allows(string $ip, string $action, int $max, int $window, ?string $env = null): bool
|
|
56
70
|
{
|
|
57
71
|
if ($max <= 0 || $window <= 0) {
|
|
58
72
|
return true;
|
|
59
73
|
}
|
|
60
74
|
|
|
75
|
+
$env = rate_limit_env($env);
|
|
61
76
|
$dir = private_dir() . '/rate-limit';
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
$ready = rate_limit_call(static function () use ($dir): bool {
|
|
78
|
+
return is_dir($dir) || mkdir($dir, 0700, true) || is_dir($dir);
|
|
79
|
+
});
|
|
80
|
+
if ($ready !== true) {
|
|
81
|
+
rate_limit_refuse_storage($env, 'could not create the rate-limit directory.');
|
|
64
82
|
return true;
|
|
65
83
|
}
|
|
66
84
|
|
|
67
85
|
$file = $dir . '/' . hash('sha256', $ip . '|' . $action) . '.json';
|
|
68
|
-
$handle =
|
|
86
|
+
$handle = rate_limit_call(static function () use ($file) {
|
|
87
|
+
return fopen($file, 'c+');
|
|
88
|
+
});
|
|
69
89
|
if ($handle === false) {
|
|
70
|
-
|
|
90
|
+
rate_limit_refuse_storage($env, 'could not open a rate-limit file.');
|
|
71
91
|
return true;
|
|
72
92
|
}
|
|
73
93
|
|
|
94
|
+
$decision = 'allow';
|
|
74
95
|
try {
|
|
75
96
|
if (!flock($handle, LOCK_EX)) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
$
|
|
82
|
-
|
|
83
|
-
|
|
97
|
+
error_log('stagepass-php: could not lock a rate-limit file.');
|
|
98
|
+
$decision = 'unavailable';
|
|
99
|
+
} else {
|
|
100
|
+
$raw = stream_get_contents($handle);
|
|
101
|
+
$decoded = [];
|
|
102
|
+
if (is_string($raw) && $raw !== '') {
|
|
103
|
+
$parsed = json_decode($raw, true);
|
|
104
|
+
if (is_array($parsed)) {
|
|
105
|
+
$decoded = $parsed;
|
|
106
|
+
}
|
|
84
107
|
}
|
|
85
|
-
}
|
|
86
108
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
109
|
+
$now = time();
|
|
110
|
+
$cutoff = $now - $window;
|
|
111
|
+
$hits = [];
|
|
112
|
+
foreach ($decoded as $ts) {
|
|
113
|
+
if (is_numeric($ts) && (int) $ts > $cutoff) {
|
|
114
|
+
$hits[] = (int) $ts;
|
|
115
|
+
}
|
|
93
116
|
}
|
|
94
|
-
}
|
|
95
117
|
|
|
96
|
-
|
|
97
|
-
|
|
118
|
+
if (count($hits) >= $max) {
|
|
119
|
+
$decision = 'deny';
|
|
120
|
+
} else {
|
|
121
|
+
$hits[] = $now;
|
|
122
|
+
rewind($handle);
|
|
123
|
+
ftruncate($handle, 0);
|
|
124
|
+
fwrite($handle, json_encode($hits, JSON_THROW_ON_ERROR));
|
|
125
|
+
fflush($handle);
|
|
126
|
+
}
|
|
98
127
|
}
|
|
99
|
-
|
|
100
|
-
$hits[] = $now;
|
|
101
|
-
rewind($handle);
|
|
102
|
-
ftruncate($handle, 0);
|
|
103
|
-
fwrite($handle, json_encode($hits, JSON_THROW_ON_ERROR));
|
|
104
|
-
fflush($handle);
|
|
105
|
-
|
|
106
|
-
return true;
|
|
107
128
|
} catch (Throwable $e) {
|
|
108
129
|
error_log('stagepass-php: rate-limit error: ' . $e->getMessage());
|
|
109
|
-
|
|
130
|
+
$decision = 'unavailable';
|
|
110
131
|
} finally {
|
|
111
|
-
|
|
112
|
-
|
|
132
|
+
if (is_resource($handle)) {
|
|
133
|
+
rate_limit_call(static function () use ($handle): void {
|
|
134
|
+
flock($handle, LOCK_UN);
|
|
135
|
+
fclose($handle);
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if ($decision === 'unavailable') {
|
|
141
|
+
if (!rate_limit_fails_open($env)) {
|
|
142
|
+
rate_limit_unavailable_response();
|
|
143
|
+
}
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return $decision === 'allow';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function rate_limit_storage_available(string $env): bool
|
|
151
|
+
{
|
|
152
|
+
$dir = private_dir() . '/rate-limit';
|
|
153
|
+
$state = rate_limit_call(static function () use ($dir): array {
|
|
154
|
+
$ok = is_dir($dir) || mkdir($dir, 0700, true) || is_dir($dir);
|
|
155
|
+
return ['ok' => $ok, 'writable' => $ok && is_writable($dir)];
|
|
156
|
+
});
|
|
157
|
+
if ($state['ok'] !== true) {
|
|
158
|
+
return rate_limit_refuse_storage($env, 'could not create the rate-limit directory.');
|
|
159
|
+
}
|
|
160
|
+
if ($state['writable'] !== true) {
|
|
161
|
+
return rate_limit_refuse_storage($env, 'could not open a rate-limit file.');
|
|
162
|
+
}
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function rate_limit_env(?string $env): string
|
|
167
|
+
{
|
|
168
|
+
if (is_string($env)) {
|
|
169
|
+
return $env;
|
|
170
|
+
}
|
|
171
|
+
$loaded = load_config();
|
|
172
|
+
return (string) ($loaded['env'] ?? 'production');
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** True only for local, development, or dev. Every other value fails closed. */
|
|
176
|
+
function rate_limit_fails_open(string $env): bool
|
|
177
|
+
{
|
|
178
|
+
return in_array(strtolower(trim($env)), ['local', 'development', 'dev'], true);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function rate_limit_refuse_storage(string $env, string $message): bool
|
|
182
|
+
{
|
|
183
|
+
error_log('stagepass-php: ' . $message);
|
|
184
|
+
if (!rate_limit_fails_open($env)) {
|
|
185
|
+
rate_limit_unavailable_response();
|
|
186
|
+
}
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function rate_limit_unavailable_response(): never
|
|
191
|
+
{
|
|
192
|
+
if (!headers_sent()) {
|
|
193
|
+
header('Retry-After: 60');
|
|
113
194
|
}
|
|
195
|
+
respond_error('The service is temporarily unavailable. Try again later.', 503);
|
|
114
196
|
}
|
|
@@ -21,4 +21,6 @@ return [
|
|
|
21
21
|
'rate_limit_window' => env_int('RATE_LIMIT_WINDOW', 60),
|
|
22
22
|
'max_body_bytes' => env_int('MAX_BODY_BYTES', 65536),
|
|
23
23
|
'timezone' => env_string('APP_TIMEZONE', 'Europe/Berlin'),
|
|
24
|
+
'trusted_proxies' => env_list('TRUSTED_PROXIES'),
|
|
25
|
+
'trusted_proxy_header' => env_string('TRUSTED_PROXY_HEADER', 'x-forwarded-for'),
|
|
24
26
|
];
|