mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-11-30 19:21:36 +08:00
插件 API v1 的元数据校验、utils、moejs 执行层和任务钩子的结果解析搬到 嵌套模块 github.com/QuantumNous/new-api/pluginkit,按 pluginkit/vX.Y.Z 打标签发布,其他宿主可以直接引用同一份实现。 - pkg/jsplugin 只留注册表、路由和 CLI,现有导出名通过别名保留,行为不变 - 文档 v1.md、v1.d.ts、v1.schema.json 移到 pluginkit/,schema 和类型由模块内嵌 - 请求处理钩子新增宿主专有的 ctx.client、ctx.session,new-api 不提供 - 导出 MaxPluginKeyLength、NewAPIReservedKeys 和 CallPathJSONInto - CI、Makefile、Dockerfile 和 AGENTS.md 按 relaykit 的方式加上 pluginkit
87 lines
3.0 KiB
Go
87 lines
3.0 KiB
Go
package pluginkit
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"golang.org/x/net/http/httpguts"
|
|
)
|
|
|
|
// ValidateRequestURL prevents plugins from directing a channel credential to
|
|
// hosts other than the configured base URL or an administrator-approved host.
|
|
func ValidateRequestURL(requestURL, baseURL string, allowedHosts []string) error {
|
|
request, err := url.Parse(requestURL)
|
|
if err != nil || request.Scheme == "" || request.Host == "" {
|
|
return fmt.Errorf("plugin request URL must be absolute")
|
|
}
|
|
base, err := url.Parse(baseURL)
|
|
if err != nil || base.Host == "" {
|
|
return fmt.Errorf("channel base URL is invalid")
|
|
}
|
|
if canonicalHost(request) == canonicalHost(base) || hostAllowed(request, allowedHosts) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("plugin request host %q is not allowed", request.Host)
|
|
}
|
|
|
|
// ValidateServiceURL checks a utils.fetch URL of a hook that runs without a
|
|
// channel: its host must be one of hosts, each `host` or `host:port`.
|
|
func ValidateServiceURL(requestURL string, hosts []string) error {
|
|
request, err := url.Parse(requestURL)
|
|
if err != nil || request.Scheme == "" || request.Host == "" {
|
|
return fmt.Errorf("plugin request URL must be absolute")
|
|
}
|
|
if hostAllowed(request, hosts) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("plugin request host %q is not allowed", request.Host)
|
|
}
|
|
|
|
func hostAllowed(request *url.URL, allowedHosts []string) bool {
|
|
requestHost := canonicalHost(request)
|
|
for _, allowed := range allowedHosts {
|
|
// Parse with the request scheme so "host:443" matches an https request
|
|
// the same way an explicit default port on the request URL does.
|
|
allowedURL, parseErr := url.Parse(request.Scheme + "://" + strings.TrimSpace(allowed))
|
|
if parseErr == nil && requestHost == canonicalHost(allowedURL) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// ValidateRequestHeaders rejects plugin-chosen headers that the host
|
|
// transport owns or that change how a request is framed or proxied, and
|
|
// bounds their number and size.
|
|
func ValidateRequestHeaders(headers map[string]string) error {
|
|
if len(headers) > 64 {
|
|
return fmt.Errorf("at most 64 request headers are allowed")
|
|
}
|
|
for name, value := range headers {
|
|
name = strings.TrimSpace(name)
|
|
if !httpguts.ValidHeaderFieldName(name) {
|
|
return fmt.Errorf("request header name %q is invalid", name)
|
|
}
|
|
if !httpguts.ValidHeaderFieldValue(value) || len(value) > 8192 {
|
|
return fmt.Errorf("request header %q has an invalid or oversized value", name)
|
|
}
|
|
switch strings.ToLower(name) {
|
|
case "host", "content-length", "accept-encoding", "connection", "proxy-connection", "keep-alive",
|
|
"proxy-authorization", "te", "trailer", "transfer-encoding", "upgrade":
|
|
return fmt.Errorf("request header %q is not allowed", name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func canonicalHost(value *url.URL) string {
|
|
host := strings.ToLower(value.Hostname())
|
|
port := value.Port()
|
|
if port == "" || port == "80" && value.Scheme == "http" || port == "443" && value.Scheme == "https" {
|
|
return host
|
|
}
|
|
return net.JoinHostPort(host, port)
|
|
}
|