mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-23 10:29:46 +01:00
chore(plugins): document requiredHosts rules and deprecate pdk.NewHTTPRequest (#6129)
* fix(plugins): align the Python HTTP example with the repo's host-call pattern
Bind http_send with raw memory offsets like nowplaying-py does, drop
guards for fields the host always sends, and document how plugins
without a PDK call host services and which built-in HTTP APIs are
disabled.
* docs(plugins): document the private-address rules for HTTP requiredHosts
Explain in the README and manifest schema that named hosts can't reach
private addresses while IP/CIDR entries and a bare "*" can.
* docs(plugins): document the private-address rules for requiredHosts
Explain in the README and manifest schema that named hosts can't reach
private addresses while IP/CIDR entries and a bare "*" can, for both
HTTP and WebSocket. Inline the single-use HTTP isHostAllowed wrapper.
* feat(plugins): derive Default for Rust host service structs
The ndpgen client.rs template now adds Default to the derive list of host
service structs, as the capability and shared types templates already do.
Plugin authors can now set only the fields they need, for example
HTTPRequest { method, url, ..Default::default() }. The webhook-rs and
discord-rich-presence-rs examples use this form now. The golden files and
the generated nd-pdk-host crate are updated to match.
* feat(plugins): deprecate pdk.NewHTTPRequest in the Go PDK
Navidrome no longer enables extism's http_request host function, so a
request built with pdk.NewHTTPRequest always fails. ndpgen now reads a small
deprecation table and writes a Deprecated: paragraph for the listed extism
functions, in both the WASM wrapper and the native stub. Linters and IDEs
now point plugin authors to host.HTTPSend. The PDK example tests used to
teach NewHTTPRequest. They now use host.HTTPSend and host.HTTPMock.
* docs(plugins): correct requiredHosts rules for websocket and private addresses
Two statements in the plugin docs did not match the code.
The WebSocket section claimed requiredHosts behaves like HTTP. It does not:
host_httpclient.go only consults the allowlist when the list is non-empty and
otherwise falls back to allowing public addresses, while host_websocket.go
always calls isHostInAllowlist, so an absent list blocks every connection.
The HTTP section claimed a named host can never reach a private address.
checkPrivateDial scans the whole requiredHosts list, so a named host does
reach a private address when the same list also holds a covering IP or CIDR.
Reworded both, plus the matching requiredHosts descriptions in
manifest-schema.json, and regenerated manifest_gen.go.
This commit is contained in:
parent
276d767ce5
commit
1072e9f7eb
27 changed files with 131 additions and 93 deletions
|
|
@ -8,6 +8,7 @@ package pdk_test
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/navidrome/navidrome/plugins/pdk/go/host"
|
||||
"github.com/navidrome/navidrome/plugins/pdk/go/pdk"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
|
@ -136,48 +137,39 @@ func TestProcessJSONRequest(t *testing.T) {
|
|||
}
|
||||
|
||||
// =============================================================================
|
||||
// Examples using stub types (Memory, HTTPRequest, HTTPResponse)
|
||||
// HTTP requests go through the host HTTP service (host.HTTPSend), not
|
||||
// pdk.NewHTTPRequest, which Navidrome does not enable.
|
||||
// =============================================================================
|
||||
|
||||
// FetchData demonstrates a plugin function that makes an HTTP request.
|
||||
func FetchData(url string) ([]byte, error) {
|
||||
// Create and configure the HTTP request
|
||||
// Note: SetHeader and SetBody work directly on the stub - no mocking needed!
|
||||
req := pdk.NewHTTPRequest(pdk.MethodGet, url)
|
||||
req.SetHeader("Accept", "application/json")
|
||||
req.SetHeader("User-Agent", "MyPlugin/1.0")
|
||||
|
||||
// Send the request - this is mocked because it requires host interaction
|
||||
resp := req.Send()
|
||||
|
||||
// Check status - works directly on the stub
|
||||
if resp.Status() != 200 {
|
||||
return nil, nil
|
||||
resp, err := host.HTTPSend(host.HTTPRequest{
|
||||
Method: "GET",
|
||||
URL: url,
|
||||
Headers: map[string]string{
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "MyPlugin/1.0",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Return body - works directly on the stub
|
||||
return resp.Body(), nil
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, nil
|
||||
}
|
||||
return resp.Body, nil
|
||||
}
|
||||
|
||||
func TestFetchData(t *testing.T) {
|
||||
pdk.ResetMock()
|
||||
host.HTTPMock.ExpectedCalls = nil
|
||||
|
||||
// Create a stub response with test data
|
||||
expectedBody := []byte(`{"result": "success"}`)
|
||||
stubResponse := pdk.NewStubHTTPResponse(200, map[string]string{
|
||||
"Content-Type": "application/json",
|
||||
}, expectedBody)
|
||||
host.HTTPMock.On("Send", mock.MatchedBy(func(req host.HTTPRequest) bool {
|
||||
return req.Method == "GET" && req.URL == "https://api.example.com/data" &&
|
||||
req.Headers["Accept"] == "application/json"
|
||||
})).Return(&host.HTTPResponse{StatusCode: 200, Body: expectedBody}, nil)
|
||||
|
||||
// Mock NewHTTPRequest to return a real HTTPRequest struct
|
||||
// The struct methods (SetHeader, SetBody) work without mocking
|
||||
pdk.PDKMock.On("NewHTTPRequest", pdk.MethodGet, "https://api.example.com/data").
|
||||
Return(&pdk.HTTPRequest{})
|
||||
|
||||
// Mock Send to return our stub response
|
||||
pdk.PDKMock.On("Send", mock.AnythingOfType("*pdk.HTTPRequest")).
|
||||
Return(stubResponse)
|
||||
|
||||
// Call the function
|
||||
body, err := FetchData("https://api.example.com/data")
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -188,21 +180,15 @@ func TestFetchData(t *testing.T) {
|
|||
t.Errorf("expected body %q, got %q", expectedBody, body)
|
||||
}
|
||||
|
||||
pdk.PDKMock.AssertExpectations(t)
|
||||
host.HTTPMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestFetchData_NonOKStatus(t *testing.T) {
|
||||
pdk.ResetMock()
|
||||
host.HTTPMock.ExpectedCalls = nil
|
||||
|
||||
// Create a stub response with 404 status
|
||||
stubResponse := pdk.NewStubHTTPResponse(404, nil, []byte("Not Found"))
|
||||
host.HTTPMock.On("Send", mock.Anything).
|
||||
Return(&host.HTTPResponse{StatusCode: 404, Body: []byte("Not Found")}, nil)
|
||||
|
||||
pdk.PDKMock.On("NewHTTPRequest", pdk.MethodGet, "https://api.example.com/missing").
|
||||
Return(&pdk.HTTPRequest{})
|
||||
pdk.PDKMock.On("Send", mock.AnythingOfType("*pdk.HTTPRequest")).
|
||||
Return(stubResponse)
|
||||
|
||||
// Call the function
|
||||
body, err := FetchData("https://api.example.com/missing")
|
||||
|
||||
if err != nil {
|
||||
|
|
@ -214,7 +200,7 @@ func TestFetchData_NonOKStatus(t *testing.T) {
|
|||
t.Errorf("expected nil body for 404, got %q", body)
|
||||
}
|
||||
|
||||
pdk.PDKMock.AssertExpectations(t)
|
||||
host.HTTPMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// ProcessMemoryData demonstrates working with Memory type.
|
||||
|
|
@ -292,23 +278,27 @@ func TestHTTPMethodString(t *testing.T) {
|
|||
|
||||
// PostJSON demonstrates a more complex HTTP request with body.
|
||||
func PostJSON(url string, data []byte) (int, error) {
|
||||
req := pdk.NewHTTPRequest(pdk.MethodPost, url)
|
||||
req.SetHeader("Content-Type", "application/json")
|
||||
req.SetBody(data) // Works directly on stub
|
||||
|
||||
resp := req.Send() // This is mocked
|
||||
return int(resp.Status()), nil
|
||||
resp, err := host.HTTPSend(host.HTTPRequest{
|
||||
Method: "POST",
|
||||
URL: url,
|
||||
Headers: map[string]string{"Content-Type": "application/json"},
|
||||
Body: data,
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(resp.StatusCode), nil
|
||||
}
|
||||
|
||||
func TestPostJSON(t *testing.T) {
|
||||
pdk.ResetMock()
|
||||
host.HTTPMock.ExpectedCalls = nil
|
||||
|
||||
stubResponse := pdk.NewStubHTTPResponse(201, nil, nil)
|
||||
|
||||
pdk.PDKMock.On("NewHTTPRequest", pdk.MethodPost, "https://api.example.com/items").
|
||||
Return(&pdk.HTTPRequest{})
|
||||
pdk.PDKMock.On("Send", mock.AnythingOfType("*pdk.HTTPRequest")).
|
||||
Return(stubResponse)
|
||||
host.HTTPMock.On("Send", host.HTTPRequest{
|
||||
Method: "POST",
|
||||
URL: "https://api.example.com/items",
|
||||
Headers: map[string]string{"Content-Type": "application/json"},
|
||||
Body: []byte(`{"name":"test"}`),
|
||||
}).Return(&host.HTTPResponse{StatusCode: 201}, nil)
|
||||
|
||||
status, err := PostJSON("https://api.example.com/items", []byte(`{"name":"test"}`))
|
||||
|
||||
|
|
@ -320,5 +310,5 @@ func TestPostJSON(t *testing.T) {
|
|||
t.Errorf("expected status 201, got %d", status)
|
||||
}
|
||||
|
||||
pdk.PDKMock.AssertExpectations(t)
|
||||
host.HTTPMock.AssertExpectations(t)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -111,6 +111,8 @@ func LogMemory(level LogLevel, m Memory) {
|
|||
}
|
||||
|
||||
// NewHTTPRequest NewHTTPRequest returns a new `HTTPRequest`.
|
||||
//
|
||||
// Deprecated: Navidrome does not enable extism's http_request host function, so every request sent this way fails. Use host.HTTPSend instead.
|
||||
func NewHTTPRequest(method HTTPMethod, url string) *HTTPRequest {
|
||||
return extism.NewHTTPRequest(method, url)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,6 +114,8 @@ func LogMemory(level LogLevel, m Memory) {
|
|||
}
|
||||
|
||||
// NewHTTPRequest NewHTTPRequest returns a new `HTTPRequest`.
|
||||
//
|
||||
// Deprecated: Navidrome does not enable extism's http_request host function, so every request sent this way fails. Use host.HTTPSend instead.
|
||||
func NewHTTPRequest(method HTTPMethod, url string) *HTTPRequest {
|
||||
args := PDKMock.Called(method, url)
|
||||
var r0 *HTTPRequest
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ mod base64_bytes {
|
|||
}
|
||||
|
||||
/// HTTPRequest represents an outbound HTTP request from a plugin.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HTTPRequest {
|
||||
pub method: String,
|
||||
|
|
@ -47,7 +47,7 @@ pub struct HTTPRequest {
|
|||
}
|
||||
|
||||
/// HTTPResponse represents the response from an outbound HTTP request.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct HTTPResponse {
|
||||
pub status_code: i32,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use extism_pdk::*;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// Library represents a music library with metadata.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Library {
|
||||
pub id: i32,
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use extism_pdk::*;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// MatchOptions carries optional parameters for a match request.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct MatchOptions {
|
||||
#[serde(default)]
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ use extism_pdk::*;
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// ScrobbleCountOptions carries optional parameters for counting user scrobbles
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ScrobbleCountOptions {
|
||||
#[serde(default)]
|
||||
|
|
@ -17,7 +17,7 @@ pub struct ScrobbleCountOptions {
|
|||
}
|
||||
|
||||
/// ScrobbleOptions carries optional parameters for retrieving user scrobbles
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ScrobbleOptions {
|
||||
#[serde(default)]
|
||||
|
|
@ -31,7 +31,7 @@ pub struct ScrobbleOptions {
|
|||
}
|
||||
|
||||
/// ScrobbleRef represents one instance of a scrobble (instance id, file id, submission time)
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ScrobbleRef {
|
||||
pub id: i64,
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ mod base64_bytes {
|
|||
}
|
||||
|
||||
/// QueueConfig holds configuration for a task queue.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct QueueConfig {
|
||||
pub concurrency: i32,
|
||||
|
|
@ -41,7 +41,7 @@ pub struct QueueConfig {
|
|||
}
|
||||
|
||||
/// TaskInfo holds the current state of a task.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct TaskInfo {
|
||||
pub status: String,
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
|
|||
|
||||
/// User represents a Navidrome user with minimal information exposed to plugins.
|
||||
/// Sensitive fields like password, email, and internal IDs are intentionally excluded.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct User {
|
||||
pub user_name: String,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue