@stagepass/cli 2.2.6 → 2.3.0-beta.1
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 +61 -13
- package/TESTING-macos.md +35 -8
- package/bin/index.js +4 -1
- package/package.json +9 -8
- package/src/cli.js +62 -7
- package/src/commands/certs.js +375 -19
- package/src/commands/daemon.js +75 -7
- package/src/commands/dev.js +419 -0
- package/src/commands/link.js +66 -25
- package/src/commands/new.js +397 -41
- package/src/commands/npm-run.js +8 -0
- package/src/commands/reload.js +2 -2
- package/src/commands/setup.js +5 -5
- package/src/commands/start.js +386 -83
- package/src/commands/stop.js +50 -31
- package/src/commands/unlink.js +8 -6
- package/src/commands/upgrade.js +51 -12
- package/src/lib/apply-site-block.js +48 -0
- package/src/lib/autostart.js +300 -3
- package/src/lib/caddyfile.js +72 -13
- package/src/lib/cdn-snippets.js +145 -3
- package/src/lib/certs.js +334 -47
- package/src/lib/dev-port.js +45 -0
- package/src/lib/doctor-site.js +61 -5
- package/src/lib/doctor.js +48 -4
- package/src/lib/ensure-running.js +89 -0
- package/src/lib/exec.js +72 -24
- package/src/lib/live-server.js +484 -0
- package/src/lib/node-engine.js +55 -0
- package/src/lib/notify.js +62 -0
- package/src/lib/origins.js +27 -5
- package/src/lib/package-versions.js +9 -0
- package/src/lib/paths.js +18 -0
- package/src/lib/php-fpm.js +33 -0
- package/src/lib/project-sync.js +165 -20
- package/src/lib/projects.js +137 -7
- package/src/lib/prompts.js +19 -0
- package/src/lib/registry.js +27 -6
- package/src/lib/scaffold.js +288 -11
- package/src/lib/site-detect.js +78 -25
- package/src/lib/snippets.js +14 -11
- package/src/lib/spinner.js +60 -0
- package/src/lib/versions.js +20 -8
- package/templates/project/AGENTS.md.tmpl +4 -3
- package/templates/project/README.md.tmpl +35 -6
- package/templates/project/api/README.md.tmpl +7 -0
- package/templates/project/examples/php/README.md.tmpl +18 -0
- package/templates/project/examples/php/form.php +105 -0
- package/templates/project/examples/php/proxy.php +171 -0
- package/templates/project/scripts/build.mjs +200 -37
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import ora from 'ora';
|
|
2
|
+
|
|
3
|
+
const FALLBACK_COLUMNS = 80;
|
|
4
|
+
const FALLBACK_ROWS = 24;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Ora uses `stream.columns ?? 80`; a literal `0` bypasses that fallback and
|
|
8
|
+
* yields Infinity line clears (runaway ANSI). Floor first so values in (0,1)
|
|
9
|
+
* cannot become 0 after Math.floor and re-enter the unsafe path.
|
|
10
|
+
* @param {unknown} columns
|
|
11
|
+
* @param {number} [fallback]
|
|
12
|
+
* @returns {number}
|
|
13
|
+
*/
|
|
14
|
+
export function resolveSpinnerColumns(columns, fallback = FALLBACK_COLUMNS) {
|
|
15
|
+
if (typeof columns !== 'number' || !Number.isFinite(columns)) return fallback;
|
|
16
|
+
const width = Math.floor(columns);
|
|
17
|
+
return width > 0 ? width : fallback;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @param {unknown} rows
|
|
22
|
+
* @param {number} [fallback]
|
|
23
|
+
* @returns {number}
|
|
24
|
+
*/
|
|
25
|
+
export function resolveSpinnerRows(rows, fallback = FALLBACK_ROWS) {
|
|
26
|
+
if (typeof rows !== 'number' || !Number.isFinite(rows)) return fallback;
|
|
27
|
+
const value = Math.floor(rows);
|
|
28
|
+
return value > 0 ? value : fallback;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Always wrap: TTY geometry can change after spinner creation (e.g. 80→0).
|
|
33
|
+
* @param {NodeJS.WritableStream & { columns?: number, rows?: number, isTTY?: boolean }} stream
|
|
34
|
+
*/
|
|
35
|
+
export function withSafeGeometry(stream) {
|
|
36
|
+
return new Proxy(stream, {
|
|
37
|
+
get(target, prop, receiver) {
|
|
38
|
+
if (prop === 'columns') return resolveSpinnerColumns(target.columns);
|
|
39
|
+
if (prop === 'rows') return resolveSpinnerRows(target.rows);
|
|
40
|
+
const value = Reflect.get(target, prop, receiver);
|
|
41
|
+
return typeof value === 'function' ? value.bind(target) : value;
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create an ora spinner that never treats TTY width 0/NaN/Infinity as valid.
|
|
48
|
+
* Positive finite TTY geometry stays positive; live resizes are re-checked.
|
|
49
|
+
* @param {string} [text]
|
|
50
|
+
* @param {import('ora').Options} [options]
|
|
51
|
+
*/
|
|
52
|
+
export function createSpinner(text = '', options = {}) {
|
|
53
|
+
const stream = options.stream ?? process.stderr;
|
|
54
|
+
const safeStream = withSafeGeometry(stream);
|
|
55
|
+
return ora({
|
|
56
|
+
...options,
|
|
57
|
+
text,
|
|
58
|
+
stream: safeStream,
|
|
59
|
+
});
|
|
60
|
+
}
|
package/src/lib/versions.js
CHANGED
|
@@ -1,22 +1,34 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Versions of @stagepass/loader and @stagepass/modules this CLI targets.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
4
|
+
* Values come from the generated `package-versions.js` (run
|
|
5
|
+
* `node scripts/sync-cli-versions.mjs`). Used for docs and snippet examples.
|
|
6
|
+
* Dependency ranges never come from the CLI version: new projects use the
|
|
7
|
+
* major range (`^2`) and `stagepass upgrade` only writes ranges a published
|
|
8
|
+
* version satisfies. A drift test keeps the generated file in sync with
|
|
9
|
+
* packages/loader and packages/modules.
|
|
8
10
|
*/
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
import { LOADER_VERSION, MODULES_VERSION } from './package-versions.js';
|
|
13
|
+
|
|
14
|
+
export { LOADER_VERSION, MODULES_VERSION };
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated Prefer `LOADER_VERSION` for loader snippets / pins /
|
|
18
|
+
* `data-stagepass-version`, and `MODULES_VERSION` where modules are meant.
|
|
19
|
+
* Kept as an alias of `LOADER_VERSION` for older call sites and tests.
|
|
20
|
+
*/
|
|
21
|
+
export const STAGEPASS_PACKAGES_VERSION = LOADER_VERSION;
|
|
10
22
|
|
|
11
23
|
/**
|
|
12
|
-
* Major caret range for a version (e.g. 2.
|
|
24
|
+
* Major caret range for a version (e.g. 2.3.1 → ^2), matching the `@2` CDN
|
|
13
25
|
* embed policy. Generated projects use it so `npm install` works with
|
|
14
26
|
* whatever 2.x is published; staged but unpublished releases never break it.
|
|
15
27
|
*
|
|
16
28
|
* @param {string} [version]
|
|
17
29
|
* @returns {string}
|
|
18
30
|
*/
|
|
19
|
-
export function packagesRange(version =
|
|
31
|
+
export function packagesRange(version = LOADER_VERSION) {
|
|
20
32
|
const match = /^[~^]?(\d+)(?:\.\d+){0,2}$/.exec(String(version).trim());
|
|
21
33
|
if (!match) {
|
|
22
34
|
throw new Error(`Invalid @stagepass package version: ${version}`);
|
|
@@ -20,20 +20,21 @@ Profile for AI coding agents and humans working on this Webflow + Stagepass proj
|
|
|
20
20
|
5. **Review before publishing.** Read the diff for asset tags, CORS origins, and anything that touches live Webflow Custom Code.
|
|
21
21
|
6. **Keep Stagepass tags intact.** Use `data-src` / `data-href` + `data-stagepass` (+ optional `data-stagepass-path`) so production users keep production URLs.
|
|
22
22
|
7. **Do not commit secrets.** Keep `.env*` out of git (see `.gitignore`).
|
|
23
|
+
8. **PHP (if enabled):** never hard-code secrets; read them from the environment. Keep proxy allowlists exact-host and https-only.
|
|
23
24
|
|
|
24
25
|
## Local workflow
|
|
25
26
|
|
|
26
27
|
```bash
|
|
27
28
|
npm run watch
|
|
29
|
+
# or: npm run dev (alias of watch)
|
|
28
30
|
stagepass start
|
|
29
|
-
stagepass link {{NAME}}
|
|
31
|
+
stagepass link {{NAME}}
|
|
30
32
|
```
|
|
31
33
|
|
|
32
|
-
Then open `https://
|
|
34
|
+
`stagepass.json` stores staging as `origin` ({{STAGING_HOST}}) and optional `production`. `link` without `--origin` uses those hosts. Then open `https://{{STAGING_HOST}}/?stagepass={{NAME}}.sp`.
|
|
33
35
|
|
|
34
36
|
## Types
|
|
35
37
|
|
|
36
38
|
`{{CONFIG_FILE}}` lists `types: {{TYPES_INLINE}}`, generated from `stagepass.json`. Modules that are not declared have no types, so the editor flags `sp.<module>` calls that would fail on the live site. After enabling a module (Webflow script tag + `stagepass.json`), run `stagepass upgrade`.
|
|
37
39
|
|
|
38
40
|
{{STAGEPASS_BLOCK}}
|
|
39
|
-
|
|
@@ -7,17 +7,18 @@ Local Stagepass project for Webflow.
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install
|
|
9
9
|
npm run build
|
|
10
|
-
stagepass link {{NAME}}
|
|
10
|
+
stagepass link {{NAME}}
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
Activate on your
|
|
13
|
+
`stagepass link` without `--origin` uses the hosts from `stagepass.json` (`origin` / optional `production`). Activate on your staging site: `https://{{STAGING_HOST}}/?stagepass={{NAME}}.sp`
|
|
14
14
|
|
|
15
15
|
## Scripts
|
|
16
16
|
|
|
17
17
|
| Script | Description |
|
|
18
18
|
| :--- | :--- |
|
|
19
|
-
| `npm run build` | Bundle JS/CSS into `dist/` with content hashes
|
|
19
|
+
| `npm run build` | Bundle JS/CSS into `dist/` with content hashes, SRI, and `manifest.json` |
|
|
20
20
|
| `npm run watch` | Rebuild on change |
|
|
21
|
+
| `npm run dev` | Alias of `watch` (until a real HMR server ships) |
|
|
21
22
|
|
|
22
23
|
## Webflow Custom Code
|
|
23
24
|
|
|
@@ -37,8 +38,36 @@ Optional exact pin (replace `@2` in the URL if you need a locked version): `@{{L
|
|
|
37
38
|
{{FOOTER_SNIPPET}}
|
|
38
39
|
```
|
|
39
40
|
|
|
40
|
-
After each production upload, set `data-src` / `data-href` to your hosted URLs. For content-hashed uploads, read
|
|
41
|
+
After each production upload, set `data-src` / `data-href` to your hosted URLs. For content-hashed uploads, read `file` from `dist/manifest.json`. Local Stagepass swapping can keep using the stable `dist/main.js` and `dist/styles.css` copies.
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
### Subresource Integrity (SRI)
|
|
43
44
|
|
|
44
|
-
|
|
45
|
+
`npm run build` writes a sha384 `integrity` per output in `dist/manifest.json`:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{
|
|
49
|
+
"main.js": {
|
|
50
|
+
"file": "main.a1b2c3d4.js",
|
|
51
|
+
"integrity": "sha384-..."
|
|
52
|
+
},
|
|
53
|
+
"styles.css": {
|
|
54
|
+
"file": "styles.e5f6a7b8.css",
|
|
55
|
+
"integrity": "sha384-..."
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Use those values on production tags (and keep `crossorigin="anonymous"` when the asset host sends CORS headers):
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<script
|
|
64
|
+
src="https://cdn.example.com/{{NAME}}/main.a1b2c3d4.js"
|
|
65
|
+
integrity="sha384-..."
|
|
66
|
+
crossorigin="anonymous"
|
|
67
|
+
data-stagepass
|
|
68
|
+
data-src="https://cdn.example.com/{{NAME}}/main.a1b2c3d4.js"
|
|
69
|
+
data-stagepass-path="/dist/main.js"
|
|
70
|
+
></script>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
{{PHP_SECTION}}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# API (PHP)
|
|
2
|
+
|
|
3
|
+
This folder is the project document root for PHP handlers served by Caddy `php_fastcgi` on your linked `.sp` domain.
|
|
4
|
+
|
|
5
|
+
- Put scripts here (for example `api/contact.php` → `https://{{NAME}}.sp/api/contact.php`).
|
|
6
|
+
- Do not commit secrets. Load credentials from environment variables.
|
|
7
|
+
- Prefer JSON responses and escape any HTML you render (`htmlspecialchars`).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# PHP examples
|
|
2
|
+
|
|
3
|
+
Sample handlers for Stagepass projects with `"php": true`. Copy into `api/` (or another path under the project root) before calling them on `https://{{NAME}}.sp`.
|
|
4
|
+
|
|
5
|
+
| File | Purpose |
|
|
6
|
+
| :--- | :--- |
|
|
7
|
+
| `form.php` | POST form handler with length limits and `htmlspecialchars` output |
|
|
8
|
+
| `proxy.php` | HTTPS proxy with an exact-host allowlist (no redirect following) |
|
|
9
|
+
|
|
10
|
+
## Proxy environment
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
export STAGEPASS_PROXY_ALLOWLIST=api.example.com
|
|
14
|
+
# optional:
|
|
15
|
+
export STAGEPASS_PROXY_TOKEN=...
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Never commit tokens. Keep allowlists exact hosts only (no wildcards, https only).
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Example form handler for Stagepass projects.
|
|
4
|
+
*
|
|
5
|
+
* Security notes:
|
|
6
|
+
* - POST only
|
|
7
|
+
* - Input length limits
|
|
8
|
+
* - HTML escaped with htmlspecialchars when rendering
|
|
9
|
+
* - No secrets in this file
|
|
10
|
+
*
|
|
11
|
+
* Copy into /api/ (or another path under the project root) before using on .sp.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
declare(strict_types=1);
|
|
15
|
+
|
|
16
|
+
header('X-Content-Type-Options: nosniff');
|
|
17
|
+
|
|
18
|
+
const MAX_NAME_LEN = 120;
|
|
19
|
+
const MAX_EMAIL_LEN = 254;
|
|
20
|
+
const MAX_MESSAGE_LEN = 4000;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @param mixed $value
|
|
24
|
+
*/
|
|
25
|
+
function field_string($value, int $maxLen): string
|
|
26
|
+
{
|
|
27
|
+
if (!is_string($value)) {
|
|
28
|
+
return '';
|
|
29
|
+
}
|
|
30
|
+
$trimmed = trim($value);
|
|
31
|
+
if (strlen($trimmed) > $maxLen) {
|
|
32
|
+
return substr($trimmed, 0, $maxLen);
|
|
33
|
+
}
|
|
34
|
+
return $trimmed;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function h(string $value): string
|
|
38
|
+
{
|
|
39
|
+
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
|
|
43
|
+
http_response_code(405);
|
|
44
|
+
header('Allow: POST');
|
|
45
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
46
|
+
echo json_encode(['ok' => false, 'error' => 'Method not allowed. Use POST.'], JSON_UNESCAPED_SLASHES);
|
|
47
|
+
exit;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
$name = field_string($_POST['name'] ?? '', MAX_NAME_LEN);
|
|
51
|
+
$email = field_string($_POST['email'] ?? '', MAX_EMAIL_LEN);
|
|
52
|
+
$message = field_string($_POST['message'] ?? '', MAX_MESSAGE_LEN);
|
|
53
|
+
|
|
54
|
+
$errors = [];
|
|
55
|
+
if ($name === '') {
|
|
56
|
+
$errors[] = 'name is required';
|
|
57
|
+
}
|
|
58
|
+
if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
59
|
+
$errors[] = 'a valid email is required';
|
|
60
|
+
}
|
|
61
|
+
if ($message === '') {
|
|
62
|
+
$errors[] = 'message is required';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
$wantsJson = isset($_SERVER['HTTP_ACCEPT'])
|
|
66
|
+
&& str_contains(strtolower((string) $_SERVER['HTTP_ACCEPT']), 'application/json');
|
|
67
|
+
|
|
68
|
+
if ($errors !== []) {
|
|
69
|
+
http_response_code(400);
|
|
70
|
+
if ($wantsJson) {
|
|
71
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
72
|
+
echo json_encode(['ok' => false, 'errors' => $errors], JSON_UNESCAPED_SLASHES);
|
|
73
|
+
exit;
|
|
74
|
+
}
|
|
75
|
+
header('Content-Type: text/html; charset=utf-8');
|
|
76
|
+
echo '<!doctype html><html><body><p>Invalid form:</p><ul>';
|
|
77
|
+
foreach ($errors as $err) {
|
|
78
|
+
echo '<li>' . h($err) . '</li>';
|
|
79
|
+
}
|
|
80
|
+
echo '</ul></body></html>';
|
|
81
|
+
exit;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Demo only: acknowledge receipt. Wire this to mail()/a queue using env secrets.
|
|
85
|
+
$payload = [
|
|
86
|
+
'ok' => true,
|
|
87
|
+
'received' => [
|
|
88
|
+
'name' => $name,
|
|
89
|
+
'email' => $email,
|
|
90
|
+
'message' => $message,
|
|
91
|
+
],
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
if ($wantsJson) {
|
|
95
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
96
|
+
echo json_encode($payload, JSON_UNESCAPED_SLASHES);
|
|
97
|
+
exit;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
header('Content-Type: text/html; charset=utf-8');
|
|
101
|
+
echo '<!doctype html><html><body>';
|
|
102
|
+
echo '<p>Thanks, ' . h($name) . '. We received your message.</p>';
|
|
103
|
+
echo '<p>Email: ' . h($email) . '</p>';
|
|
104
|
+
echo '<pre>' . h($message) . '</pre>';
|
|
105
|
+
echo '</body></html>';
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<?php
|
|
2
|
+
/**
|
|
3
|
+
* Example HTTPS API proxy with a strict host allowlist.
|
|
4
|
+
*
|
|
5
|
+
* Security notes:
|
|
6
|
+
* - GET/POST only
|
|
7
|
+
* - https only, exact host allowlist (no wildcards)
|
|
8
|
+
* - Redirects are not followed to other hosts
|
|
9
|
+
* - Timeouts and response size cap
|
|
10
|
+
* - Secrets (tokens) come from the environment, never from this file
|
|
11
|
+
*
|
|
12
|
+
* Env:
|
|
13
|
+
* STAGEPASS_PROXY_ALLOWLIST Comma-separated exact hosts (required), e.g. api.example.com
|
|
14
|
+
* STAGEPASS_PROXY_TOKEN Optional Bearer token forwarded to the upstream
|
|
15
|
+
*
|
|
16
|
+
* Usage (after copying to /api/proxy.php):
|
|
17
|
+
* GET /api/proxy.php?path=/v1/items
|
|
18
|
+
* The upstream base is https://<first-allowlisted-host> unless ?host= matches the allowlist.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
declare(strict_types=1);
|
|
22
|
+
|
|
23
|
+
header('X-Content-Type-Options: nosniff');
|
|
24
|
+
header('Content-Type: application/json; charset=utf-8');
|
|
25
|
+
|
|
26
|
+
const PROXY_TIMEOUT_SECONDS = 5;
|
|
27
|
+
const PROXY_MAX_BYTES = 1048576; // 1 MiB
|
|
28
|
+
const PROXY_MAX_PATH_LEN = 512;
|
|
29
|
+
const PROXY_MAX_BODY_LEN = 65536;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @return list<string>
|
|
33
|
+
*/
|
|
34
|
+
function proxy_allowlist(): array
|
|
35
|
+
{
|
|
36
|
+
$raw = getenv('STAGEPASS_PROXY_ALLOWLIST');
|
|
37
|
+
if (!is_string($raw) || trim($raw) === '') {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
$hosts = [];
|
|
41
|
+
foreach (explode(',', $raw) as $part) {
|
|
42
|
+
$host = strtolower(trim($part));
|
|
43
|
+
if ($host === '' || str_contains($host, '/') || str_contains($host, ':') || str_contains($host, '*')) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (!preg_match('/^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/', $host)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
$hosts[] = $host;
|
|
50
|
+
}
|
|
51
|
+
return array_values(array_unique($hosts));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function json_exit(int $status, array $payload): void
|
|
55
|
+
{
|
|
56
|
+
http_response_code($status);
|
|
57
|
+
echo json_encode($payload, JSON_UNESCAPED_SLASHES);
|
|
58
|
+
exit;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
$method = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET'));
|
|
62
|
+
if ($method !== 'GET' && $method !== 'POST') {
|
|
63
|
+
header('Allow: GET, POST');
|
|
64
|
+
json_exit(405, ['ok' => false, 'error' => 'Method not allowed.']);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
$allowlist = proxy_allowlist();
|
|
68
|
+
if ($allowlist === []) {
|
|
69
|
+
json_exit(500, [
|
|
70
|
+
'ok' => false,
|
|
71
|
+
'error' => 'Set STAGEPASS_PROXY_ALLOWLIST to a comma-separated list of exact https hosts.',
|
|
72
|
+
]);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
$requestedHost = isset($_GET['host']) ? strtolower(trim((string) $_GET['host'])) : $allowlist[0];
|
|
76
|
+
if (!in_array($requestedHost, $allowlist, true)) {
|
|
77
|
+
json_exit(403, ['ok' => false, 'error' => 'Host is not on the allowlist.']);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
$path = isset($_GET['path']) ? (string) $_GET['path'] : '/';
|
|
81
|
+
if ($path === '' || $path[0] !== '/') {
|
|
82
|
+
json_exit(400, ['ok' => false, 'error' => 'path must start with /.']);
|
|
83
|
+
}
|
|
84
|
+
if (strlen($path) > PROXY_MAX_PATH_LEN || str_contains($path, '\\') || preg_match('/[\x00-\x20\x7f]/', $path) === 1) {
|
|
85
|
+
json_exit(400, ['ok' => false, 'error' => 'Invalid path.']);
|
|
86
|
+
}
|
|
87
|
+
// Block scheme-relative and open-redirect style paths.
|
|
88
|
+
if (str_starts_with($path, '//') || preg_match('#^[a-z][a-z0-9+.-]*:#i', $path) === 1) {
|
|
89
|
+
json_exit(400, ['ok' => false, 'error' => 'Invalid path.']);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
$upstream = 'https://' . $requestedHost . $path;
|
|
93
|
+
$query = $_GET;
|
|
94
|
+
unset($query['host'], $query['path']);
|
|
95
|
+
if ($query !== []) {
|
|
96
|
+
$upstream .= (str_contains($path, '?') ? '&' : '?') . http_build_query($query);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
$body = null;
|
|
100
|
+
if ($method === 'POST') {
|
|
101
|
+
$body = file_get_contents('php://input', false, null, 0, PROXY_MAX_BODY_LEN + 1);
|
|
102
|
+
if ($body === false) {
|
|
103
|
+
json_exit(400, ['ok' => false, 'error' => 'Could not read request body.']);
|
|
104
|
+
}
|
|
105
|
+
if (strlen($body) > PROXY_MAX_BODY_LEN) {
|
|
106
|
+
json_exit(413, ['ok' => false, 'error' => 'Request body too large.']);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
$headers = ['Accept: application/json'];
|
|
111
|
+
$contentType = $_SERVER['CONTENT_TYPE'] ?? $_SERVER['HTTP_CONTENT_TYPE'] ?? '';
|
|
112
|
+
if (is_string($contentType) && $contentType !== '' && $method === 'POST') {
|
|
113
|
+
// Only forward a simple media type (no CR/LF injection).
|
|
114
|
+
if (preg_match('/^[a-z0-9!#$&.+\-^_]{1,127}\/[a-z0-9!#$&.+\-^_]{1,127}(?:\s*;\s*charset=utf-8)?$/i', $contentType) === 1) {
|
|
115
|
+
$headers[] = 'Content-Type: ' . $contentType;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
$token = getenv('STAGEPASS_PROXY_TOKEN');
|
|
119
|
+
if (is_string($token) && $token !== '') {
|
|
120
|
+
$headers[] = 'Authorization: Bearer ' . $token;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
$buffer = '';
|
|
124
|
+
$tooLarge = false;
|
|
125
|
+
$ch = curl_init($upstream);
|
|
126
|
+
if ($ch === false) {
|
|
127
|
+
json_exit(500, ['ok' => false, 'error' => 'Could not start upstream request.']);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
curl_setopt_array($ch, [
|
|
131
|
+
CURLOPT_CUSTOMREQUEST => $method,
|
|
132
|
+
CURLOPT_HTTPHEADER => $headers,
|
|
133
|
+
CURLOPT_RETURNTRANSFER => false,
|
|
134
|
+
// Stream into a buffer and abort once the cap is exceeded (no unbounded memory use).
|
|
135
|
+
CURLOPT_WRITEFUNCTION => static function ($handle, string $chunk) use (&$buffer, &$tooLarge): int {
|
|
136
|
+
if (strlen($buffer) + strlen($chunk) > PROXY_MAX_BYTES) {
|
|
137
|
+
$tooLarge = true;
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
$buffer .= $chunk;
|
|
141
|
+
return strlen($chunk);
|
|
142
|
+
},
|
|
143
|
+
CURLOPT_FOLLOWLOCATION => false, // never follow redirects (SSRF / host hop)
|
|
144
|
+
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
|
|
145
|
+
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTPS,
|
|
146
|
+
CURLOPT_TIMEOUT => PROXY_TIMEOUT_SECONDS,
|
|
147
|
+
CURLOPT_CONNECTTIMEOUT => PROXY_TIMEOUT_SECONDS,
|
|
148
|
+
CURLOPT_SSL_VERIFYPEER => true,
|
|
149
|
+
CURLOPT_SSL_VERIFYHOST => 2,
|
|
150
|
+
CURLOPT_USERAGENT => 'stagepass-php-proxy/1.0',
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
if ($method === 'POST') {
|
|
154
|
+
curl_setopt($ch, CURLOPT_POSTFIELDS, $body ?? '');
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
curl_exec($ch);
|
|
158
|
+
$errno = curl_errno($ch);
|
|
159
|
+
$status = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
|
|
160
|
+
curl_close($ch);
|
|
161
|
+
|
|
162
|
+
if ($tooLarge) {
|
|
163
|
+
json_exit(502, ['ok' => false, 'error' => 'Upstream response too large.']);
|
|
164
|
+
}
|
|
165
|
+
if ($errno !== 0) {
|
|
166
|
+
// Do not echo curl details to the client.
|
|
167
|
+
json_exit(502, ['ok' => false, 'error' => 'Upstream request failed.']);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
http_response_code($status >= 100 && $status <= 599 ? $status : 502);
|
|
171
|
+
echo $buffer;
|