Skip to content

Commit 3b99120

Browse files
author
CodeBuddy Attribution Bot
committed
fix(attribution): Skill 文档未清晰说明 HTTP 云函数的代码编写规范 (issue_mns1xuxr_arrdn2)
1 parent c86a76a commit 3b99120

3 files changed

Lines changed: 73 additions & 7 deletions

File tree

config/source/skills/cloud-functions/SKILL.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ Keep local `references/...` paths for files that ship with the current skill dir
5454
- Forgetting that runtime cannot be changed after creation.
5555
- Using cloud functions as the first answer for Web login.
5656
- Forgetting that HTTP Functions must ship `scf_bootstrap`, listen on port `9000`, and include dependencies.
57+
- Forgetting to configure function security rules after creating an HTTP Function. Default rules reject anonymous callers with `EXCEED_AUTHORITY`. Use `managePermissions(action="updateResourcePermission", resourceType="function")` to allow public access.
58+
- Mismatching the `scf_bootstrap` Node.js binary path with the function runtime (e.g. using `/var/lang/node18/bin/node` but setting `runtime: "Nodejs16.13"`).
5759

5860
### Minimal checklist
5961

@@ -177,10 +179,21 @@ exports.main = async (event, context) => {
177179

178180
```js
179181
const http = require("http");
182+
const { URL } = require("url");
183+
184+
function sendJson(res, statusCode, data) {
185+
res.writeHead(statusCode, { "Content-Type": "application/json; charset=utf-8" });
186+
res.end(JSON.stringify(data));
187+
}
180188

181189
const server = http.createServer((req, res) => {
182-
res.writeHead(200, { "Content-Type": "application/json" });
183-
res.end(JSON.stringify({ ok: true, message: "hello from http function" }));
190+
const url = new URL(req.url || "/", "http://127.0.0.1");
191+
192+
if (req.method === "GET" && url.pathname === "/") {
193+
sendJson(res, 200, { ok: true, message: "hello from http function" });
194+
} else {
195+
sendJson(res, 404, { error: "Not Found" });
196+
}
184197
});
185198

186199
server.listen(9000);
@@ -193,6 +206,8 @@ server.listen(9000);
193206
/var/lang/node18/bin/node index.js
194207
```
195208

209+
The `scf_bootstrap` binary path must match the runtime — see the full mapping table in `./references/http-functions.md`.
210+
196211
`cloudfunctions/hello-http/package.json`
197212

198213
```json

config/source/skills/cloud-functions/checklist.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ Use this checklist before creating or updating a CloudBase function.
88
- Event Function: `exports.main(event, context)`, SDK/timer driven
99
- HTTP Function: `req` / `res`, listens on port `9000`
1010
2. Pick the runtime before creation and state it explicitly.
11-
3. For HTTP Functions, confirm `scf_bootstrap` exists and the service listens on port `9000`.
11+
3. For HTTP Functions, confirm `scf_bootstrap` exists and the Node.js binary path matches the runtime (e.g. `Nodejs18.15``/var/lang/node18/bin/node`).
1212
4. Confirm the function root path points to the parent directory, not the function directory itself.
13-
5. If the request is really for a long-running container service, reroute to `cloudrun-development`.
13+
5. For HTTP Functions that need anonymous access, configure the function security rule with `managePermissions(action="updateResourcePermission", resourceType="function")` after creation. Default rules reject anonymous callers with `EXCEED_AUTHORITY`.
14+
6. If the request is really for a long-running container service, reroute to `cloudrun-development`.
1415

1516
## Common failure patterns
1617

1718
- Choosing the wrong function type and compensating later.
1819
- Mixing Event Function and HTTP Function handler shapes in the same implementation.
1920
- Forgetting that runtime cannot be changed after creation.
21+
- Mismatching the `scf_bootstrap` Node.js binary path with the function runtime.
22+
- Forgetting to configure function security rules for HTTP Functions that need anonymous access.
2023
- Treating Cloud Functions as the default answer for Web authentication.
2124

2225
## Done criteria

config/source/skills/cloud-functions/references/http-functions.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ Requirements:
3535
- Use LF line endings.
3636
- Make it executable with `chmod +x scf_bootstrap`.
3737

38+
The `scf_bootstrap` Node.js binary path must match the function runtime. Use this mapping:
39+
40+
| Runtime value | `scf_bootstrap` binary path |
41+
| --- | --- |
42+
| `Nodejs20.19` | `/var/lang/node20/bin/node` |
43+
| `Nodejs18.15` | `/var/lang/node18/bin/node` |
44+
| `Nodejs16.13` | `/var/lang/node16/bin/node` |
45+
46+
If the user specifies "Node.js 18", use runtime `Nodejs18.15` and the path `/var/lang/node18/bin/node`.
47+
3848
## Minimal Node.js example
3949

4050
```javascript
@@ -203,6 +213,15 @@ app.all("/{*splat}", (req, res) => {
203213

204214
Express 5 note: `app.all("/{*splat}", (req, res) => {` is the safe catch-all form when you also need to match the root path `/`, because the router is based on `path-to-regexp` rather than the older Express 4 wildcard behavior.
205215

216+
## End-to-end deployment lifecycle
217+
218+
Follow these steps in order when creating an HTTP Function:
219+
220+
1. **Write the function code** — create the directory with `index.js`, `scf_bootstrap`, and `package.json`.
221+
2. **Deploy with `manageFunctions`** — set `type: "HTTP"`, `protocolType: "HTTP"`, and `runtime` explicitly.
222+
3. **Configure security rules** — HTTP Functions default to a restrictive security rule. If the function should be publicly accessible (anonymous access), call `managePermissions(action="updateResourcePermission")` with `resourceType="function"`.
223+
4. **Verify** — call the function URL and confirm it returns the expected response. If you get `EXCEED_AUTHORITY`, the security rule needs to be updated (step 3).
224+
206225
## Deployment flow
207226

208227
Prefer `manageFunctions` over CLI in agent flows.
@@ -214,12 +233,43 @@ manageFunctions({
214233
name: "myHttpFunction",
215234
type: "HTTP",
216235
protocolType: "HTTP",
236+
runtime: "Nodejs18.15",
217237
timeout: 60
218238
},
219239
functionRootPath: "/absolute/path/to/cloudfunctions"
220240
});
221241
```
222242

243+
**Important parameters:**
244+
245+
- `type: "HTTP"` — marks the function as an HTTP Function (not an Event Function).
246+
- `protocolType: "HTTP"` — the wire protocol. Use `"WS"` for WebSocket.
247+
- `runtime` — the execution runtime. Must match the `scf_bootstrap` binary path. Default is `"Nodejs18.15"` if omitted, but always set it explicitly to avoid ambiguity.
248+
- `functionRootPath` — the parent directory of the function folder (e.g. `/path/to/cloudfunctions` if the code lives in `/path/to/cloudfunctions/myHttpFunction/`).
249+
250+
### Security rule configuration
251+
252+
After creating an HTTP Function, it will reject anonymous callers with `EXCEED_AUTHORITY` by default. If the function should be publicly accessible:
253+
254+
```javascript
255+
managePermissions({
256+
action: "updateResourcePermission",
257+
resourceType: "function",
258+
resourceId: "myHttpFunction",
259+
permission: {
260+
aclTag: "CUSTOM",
261+
rule: "true"
262+
}
263+
});
264+
```
265+
266+
- `aclTag: "CUSTOM"` with `rule: "true"` allows all callers (anonymous access).
267+
- Do NOT use `readSecurityRule` / `writeSecurityRule` — those are removed. Use `queryPermissions` / `managePermissions` instead.
268+
- Security rule semantics for `resourceType="function"` differ from NoSQL database rules. Do not reuse `doc._openid` or `auth.openid` expressions from NoSQL security rules.
269+
- Official reference: `https://docs.cloudbase.net/cloud-function/security-rules`
270+
271+
If an external caller reports `EXCEED_AUTHORITY`, inspect the function permission first with `queryPermissions(action="getResourcePermission", resourceType="function", resourceId="myHttpFunction")` before widening access.
272+
223273
### WebSocket
224274

225275
For WebSocket workloads, keep the function type as HTTP and switch `protocolType`:
@@ -264,9 +314,7 @@ manageGateway({
264314
Before enabling anonymous access, confirm both of these:
265315

266316
1. The access path exists.
267-
2. The function security rule allows the intended caller identity.
268-
269-
If an external caller reports `EXCEED_AUTHORITY`, inspect the function permission first with `queryPermissions(action="getResourcePermission", resourceType="function")` before widening access.
317+
2. The function security rule allows the intended caller identity (see Security rule configuration above).
270318

271319
## SSE and WebSocket notes
272320

0 commit comments

Comments
 (0)