feat: report what an item reads and who reads it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-11-04 06:35:07 -08:00
co-authored by Claude Opus 5.5
parent 4fd5240be7
commit 30b78867f7
13 changed files with 1775 additions and 6 deletions
@@ -0,0 +1,92 @@
{
"db_name": "PostgreSQL",
"query": "SELECT path AS \"path!\", hash AS \"hash!\", language AS \"language!: ScriptLang\",\n content AS \"content!\", schema AS \"schema?\", modules AS \"modules?\",\n on_behalf_of AS \"on_behalf_of?\"\n FROM script\n WHERE workspace_id = $1 AND hash = ANY($2) AND deleted = false",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "path!",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "hash!",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "language!: ScriptLang",
"type_info": {
"Custom": {
"name": "script_lang",
"kind": {
"Enum": [
"python3",
"deno",
"go",
"bash",
"postgresql",
"nativets",
"bun",
"mysql",
"bigquery",
"snowflake",
"graphql",
"powershell",
"mssql",
"php",
"bunnative",
"rust",
"ansible",
"csharp",
"oracledb",
"nu",
"java",
"duckdb",
"ruby",
"rlang",
"dbt"
]
}
}
}
},
{
"ordinal": 3,
"name": "content!",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "schema?",
"type_info": "Json"
},
{
"ordinal": 5,
"name": "modules?",
"type_info": "Jsonb"
},
{
"ordinal": 6,
"name": "on_behalf_of?",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Text",
"Int8Array"
]
},
"nullable": [
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "096ee8a2f9a1cecf8c0f4302cc5f0c68b9ab2ed1998a4baa1114a78bfeada890"
}
@@ -0,0 +1,29 @@
{
"db_name": "PostgreSQL",
"query": "SELECT path AS \"path!\", value AS \"value?\"\n FROM resource\n WHERE workspace_id = $1 AND path = ANY($2)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "path!",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "value?",
"type_info": "Jsonb"
}
],
"parameters": {
"Left": [
"Text",
"TextArray"
]
},
"nullable": [
false,
true
]
},
"hash": "424247c6ef5bb5739a5f6894aaefdf47ac6dcbfd110401e0a924302581f7e206"
}
@@ -0,0 +1,92 @@
{
"db_name": "PostgreSQL",
"query": "SELECT DISTINCT ON (path) path AS \"path!\", hash AS \"hash!\",\n language AS \"language!: ScriptLang\", content AS \"content!\",\n schema AS \"schema?\", modules AS \"modules?\", on_behalf_of AS \"on_behalf_of?\"\n FROM script\n WHERE workspace_id = $1 AND path = ANY($2) AND deleted = false\n ORDER BY path, (lock IS NOT NULL AND lock_error_logs IS NULL) DESC, created_at DESC",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "path!",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "hash!",
"type_info": "Int8"
},
{
"ordinal": 2,
"name": "language!: ScriptLang",
"type_info": {
"Custom": {
"name": "script_lang",
"kind": {
"Enum": [
"python3",
"deno",
"go",
"bash",
"postgresql",
"nativets",
"bun",
"mysql",
"bigquery",
"snowflake",
"graphql",
"powershell",
"mssql",
"php",
"bunnative",
"rust",
"ansible",
"csharp",
"oracledb",
"nu",
"java",
"duckdb",
"ruby",
"rlang",
"dbt"
]
}
}
}
},
{
"ordinal": 3,
"name": "content!",
"type_info": "Text"
},
{
"ordinal": 4,
"name": "schema?",
"type_info": "Json"
},
{
"ordinal": 5,
"name": "modules?",
"type_info": "Jsonb"
},
{
"ordinal": 6,
"name": "on_behalf_of?",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Text",
"TextArray"
]
},
"nullable": [
false,
false,
false,
false,
true,
true,
true
]
},
"hash": "47ff60ebf7524c590818a7f61874966fde6268fe108fdfa6ee6703e804b4e799"
}
@@ -0,0 +1,29 @@
{
"db_name": "PostgreSQL",
"query": "SELECT app.policy AS \"policy!\", app_version.value AS \"value!\"\n FROM app\n JOIN app_version ON app_version.id = app.versions[array_upper(app.versions, 1)]\n WHERE app.workspace_id = $1 AND app.path = $2",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "policy!",
"type_info": "Jsonb"
},
{
"ordinal": 1,
"name": "value!",
"type_info": "Json"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false
]
},
"hash": "6f00489d74a831b482edf8eee6691a81041fef67f2c8cc5d8f95f99dd0eca317"
}
@@ -0,0 +1,41 @@
{
"db_name": "PostgreSQL",
"query": "SELECT flow.path AS \"path!\", flow.on_behalf_of AS \"on_behalf_of?\",\n flow_version.value AS \"value?\", flow_version.schema AS \"schema?\"\n FROM flow\n LEFT JOIN flow_version\n ON flow_version.id = flow.versions[array_upper(flow.versions, 1)]\n WHERE flow.workspace_id = $1 AND flow.path = ANY($2)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "path!",
"type_info": "Varchar"
},
{
"ordinal": 1,
"name": "on_behalf_of?",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "value?",
"type_info": "Jsonb"
},
{
"ordinal": 3,
"name": "schema?",
"type_info": "Json"
}
],
"parameters": {
"Left": [
"Text",
"TextArray"
]
},
"nullable": [
false,
true,
false,
true
]
},
"hash": "fc4f0ff56a6de0cbb24695b38f120b511c31f776f015cf20e2eb450c94956a03"
}
+2
View File
@@ -16104,9 +16104,11 @@ dependencies = [
"serde_json",
"sql-builder",
"sqlx",
"tokio",
"tracing",
"uuid",
"windmill-api-auth",
"windmill-api-scripts",
"windmill-api-workspaces",
"windmill-audit",
"windmill-common",
+134 -3
View File
@@ -1,6 +1,10 @@
//! Another member's access lists paths the caller may not be able to read, so only a workspace
//! admin may ask for it.
//!
//! The auth cache is process-wide and keyed by workspace and token, so a test that gives
//! test-user-2 other groups or folders than another test does signs in with a token of its own.
use serde_json::json;
use sqlx::{Pool, Postgres};
use windmill_test_utils::*;
@@ -76,7 +80,10 @@ async fn test_browse_items_kind_and_blocked(db: Pool<Postgres>) -> anyhow::Resul
INSERT INTO app (id, workspace_id, path, summary, policy, versions, extra_perms)
VALUES (1, 'test-workspace', 'f/fin/dash', '', '{}', '{1}', '{}');
INSERT INTO app_version (id, app_id, value, created_by, raw_app)
VALUES (1, 1, '{}', 'test-user', true);",
VALUES (1, 1, '{}', 'test-user', true);
INSERT INTO token (token_hash, token_prefix, token, email, label, super_admin)
VALUES (encode(sha256('BROWSE_TOKEN'::bytea), 'hex'), 'BROWSE_TOK', 'BROWSE_TOKEN',
'test2@windmill.dev', 'browse', false);",
)
.execute(&db)
.await?;
@@ -93,7 +100,7 @@ async fn test_browse_items_kind_and_blocked(db: Pool<Postgres>) -> anyhow::Resul
.send()
};
let member: serde_json::Value = browse("SECRET_TOKEN_2")
let member: serde_json::Value = browse("BROWSE_TOKEN")
.await?
.error_for_status()?
.json()
@@ -104,7 +111,7 @@ async fn test_browse_items_kind_and_blocked(db: Pool<Postgres>) -> anyhow::Resul
"kind": "raw_app",
"path": "f/fin/dash",
"grant": null,
"blocked": "Only workspace admins and owners of f/fin can share it"
"blocked": "Only admins of the workspace or of f/fin can share it"
}])
);
@@ -117,3 +124,127 @@ async fn test_browse_items_kind_and_blocked(db: Pool<Postgres>) -> anyhow::Resul
Ok(())
}
async fn reads(port: u16, body: serde_json::Value) -> anyhow::Result<(u16, serde_json::Value)> {
let resp = reqwest::Client::new()
.post(format!(
"http://localhost:{port}/api/w/test-workspace/access/reads"
))
.header("Authorization", "Bearer READS_TOKEN")
.json(&body)
.send()
.await?;
let status = resp.status().as_u16();
let text = resp.text().await?;
Ok((
status,
serde_json::from_str(&text).unwrap_or(serde_json::Value::String(text)),
))
}
/// A step runs as its target's own account when it has one, and only what the caller can read
/// is followed.
#[sqlx::test(fixtures("base", "access_reads"))]
async fn test_access_reads_flow(db: Pool<Postgres>) -> anyhow::Result<()> {
initialize_tracing().await;
let server = ApiServer::start(db.clone()).await?;
let port = server.addr.port();
let root = json!({"type": "root"});
let bot = json!({"type": "account", "principal": "u/test-user"});
let (status, flow) = reads(port, json!({"kind": "flow", "path": "u/test-user-2/flow"})).await?;
assert_eq!(status, 200, "{flow}");
assert_eq!(
flow,
json!({
"runs_as": {"type": "caller"},
"references": [
// A path step's static input is resolved by the step's own job.
{"kind": "resource", "path": "u/test-user-2/bot_input", "access": "read",
"reader": bot, "sources": ["u/test-user-2/flow › Bot"]},
{"kind": "variable", "path": "u/test-user-2/bot_token", "access": "read",
"reader": bot, "sources": ["u/test-user-2/bot"]},
{"kind": "variable", "path": "u/test-user-2/expr_var", "access": "read",
"reader": root, "sources": ["u/test-user-2/flow › Inline"]},
{"kind": "variable", "path": "u/test-user-2/linked", "access": "read",
"reader": bot, "sources": ["u/test-user-2/bot_input"]},
// The pinned version's read, not the latest version's.
{"kind": "variable", "path": "u/test-user-2/old_var", "access": "read",
"reader": root, "sources": ["u/test-user-2/versioned"]},
{"kind": "variable", "path": "u/test-user-2/state", "access": "write",
"reader": root, "sources": ["u/test-user-2/flow › Inline"]},
{"kind": "script", "path": "u/test-user-2/bot", "access": "run",
"reader": root, "sources": ["u/test-user-2/flow › Inline"]},
],
"others": [],
"errors": ["u/test-user/private: not found or not readable"],
})
);
// Unsaved content stands in for the saved item, including where it names its own path.
let (status, draft) = reads(
port,
json!({
"kind": "script",
"path": "u/test-user-2/versioned",
"source": {"language": "bun", "content": "import * as wmill from 'windmill-client'
await wmill.getVariable('u/test-user-2/draft_var')
await wmill.runScriptByPath('u/test-user-2/versioned')"},
}),
)
.await?;
assert_eq!(status, 200, "{draft}");
assert_eq!(
draft["references"],
json!([
{"kind": "variable", "path": "u/test-user-2/draft_var", "access": "read",
"reader": root, "sources": ["u/test-user-2/versioned"]},
{"kind": "script", "path": "u/test-user-2/versioned", "access": "run",
"reader": root, "sources": ["u/test-user-2/versioned"]},
])
);
let (status, private) = reads(
port,
json!({"kind": "script", "path": "u/test-user/private"}),
)
.await?;
assert_eq!(status, 404, "{private}");
Ok(())
}
/// A full-code app's frontend reads as whoever opens it, and its runnables read as the app
/// whatever account the script they call runs as.
#[sqlx::test(fixtures("base", "access_reads"))]
async fn test_access_reads_app(db: Pool<Postgres>) -> anyhow::Result<()> {
initialize_tracing().await;
let server = ApiServer::start(db.clone()).await?;
let root = json!({"type": "root"});
let viewer = json!({"type": "viewer"});
let (status, app) = reads(
server.addr.port(),
json!({"kind": "app", "path": "u/test-user-2/app"}),
)
.await?;
assert_eq!(status, 200, "{app}");
assert_eq!(
app,
json!({
"runs_as": {"type": "account", "principal": "u/test-user"},
"references": [
{"kind": "variable", "path": "u/test-user-2/bot_token", "access": "read",
"reader": root, "sources": ["u/test-user-2/bot"]},
{"kind": "variable", "path": "u/test-user-2/runnable_var", "access": "read",
"reader": root, "sources": ["u/test-user-2/app › a"]},
{"kind": "variable", "path": "u/test-user-2/svelte_var", "access": "read",
"reader": viewer, "sources": ["u/test-user-2/app › /Widget.svelte"]},
{"kind": "variable", "path": "u/test-user-2/tsx_var", "access": "read",
"reader": viewer, "sources": ["u/test-user-2/app › /index.tsx"]},
],
"others": [],
"errors": [],
})
);
Ok(())
}
+60
View File
@@ -0,0 +1,60 @@
-- Items `/access/reads` follows as test-user-2: its own, one private to test-user, and a script
-- that runs as test-user.
INSERT INTO token (token_hash, token_prefix, token, email, label, super_admin) VALUES
(encode(sha256('READS_TOKEN'::bytea), 'hex'), 'READS_TOKE', 'READS_TOKEN', 'test2@windmill.dev',
'reads', false);
INSERT INTO script (workspace_id, hash, path, content, language, created_by, created_at, schema,
summary, description, lock, archived, on_behalf_of) VALUES
('test-workspace', 1, 'u/test-user-2/versioned',
'import * as wmill from "windmill-client"
export async function main() { return await wmill.getVariable("u/test-user-2/old_var") }',
'bun', 'test-user-2', now() - interval '1 day', '{}', '', '', '', true, NULL),
('test-workspace', 2, 'u/test-user-2/versioned',
'import * as wmill from "windmill-client"
export async function main() { return await wmill.getVariable("u/test-user-2/new_var") }',
'bun', 'test-user-2', now(), '{}', '', '', '', false, NULL),
('test-workspace', 3, 'u/test-user-2/bot',
'import * as wmill from "windmill-client"
export async function main(input: object) { return await wmill.getVariable("u/test-user-2/bot_token") }',
'bun', 'test-user-2', now(), '{}', '', '', '', false, 'u/test-user'),
('test-workspace', 4, 'u/test-user/private', 'export async function main() {}',
'bun', 'test-user', now(), '{}', '', '', '', false, NULL);
INSERT INTO resource (workspace_id, path, value, resource_type, extra_perms, created_by) VALUES
('test-workspace', 'u/test-user-2/bot_input', '{"password": "$var:u/test-user-2/linked"}',
'object', '{}', 'test-user-2');
INSERT INTO flow (workspace_id, path, summary, description, value, edited_by, edited_at, schema,
extra_perms, versions) VALUES
('test-workspace', 'u/test-user-2/flow', '', '', '{}', 'test-user-2', now(), '{}', '{}', '{10}');
INSERT INTO flow_version (id, workspace_id, path, value, schema, created_by) VALUES
(10, 'test-workspace', 'u/test-user-2/flow', '{"modules": [
{"id": "a", "summary": "Pinned", "value": {"type": "script", "path": "u/test-user-2/versioned",
"hash": "0000000000000001", "input_transforms": {}}},
{"id": "b", "summary": "Inline", "value": {"type": "rawscript", "language": "bun",
"content": "import * as wmill from \"windmill-client\"\nexport async function main() {\n await wmill.setVariable(\"u/test-user-2/state\", \"x\")\n await wmill.runScriptByPath(\"u/test-user-2/bot\", {})\n}",
"input_transforms": {"token": {"type": "javascript", "expr": "variable(''u/test-user-2/expr_var'')"}}}},
{"id": "c", "summary": "Bot", "value": {"type": "script", "path": "u/test-user-2/bot",
"input_transforms": {"input": {"type": "static", "value": "$res:u/test-user-2/bot_input"}}}},
{"id": "d", "value": {"type": "script", "path": "u/test-user/private", "input_transforms": {}}}
]}', '{}', 'test-user-2');
INSERT INTO app (id, workspace_id, path, summary, policy, versions, extra_perms) VALUES
(20, 'test-workspace', 'u/test-user-2/app', '',
'{"execution_mode": "publisher", "on_behalf_of": "u/test-user"}', '{20}', '{}');
INSERT INTO app_version (id, app_id, value, created_by, raw_app) VALUES
(20, 20, '{
"runnables": {
"a": {"name": "a", "type": "inline", "inlineScript": {"language": "bun",
"content": "import * as wmill from \"windmill-client\"\nexport async function main() { return await wmill.getVariable(\"u/test-user-2/runnable_var\") }"}},
"b": {"name": "b", "type": "runnableByPath", "runType": "script", "path": "u/test-user-2/bot"}
},
"files": {
"/index.tsx": "import * as wmill from \"windmill-client\"\nexport default function App() { wmill.getVariable(\"u/test-user-2/tsx_var\"); return <div /> }",
"/Widget.svelte": "<script lang=\"ts\">import { getVariable } from \"windmill-client\"; const v = getVariable(\"u/test-user-2/svelte_var\")</script>\n<p>{v}</p>",
"/notes.md": "Call getVariable(\"u/test-user-2/md_var\") to read it."
}
}', 'test-user-2', true);
+2
View File
@@ -18,6 +18,7 @@ private = ["windmill-common/private", "windmill-api-workspaces/private"]
[dependencies]
windmill-common = { workspace = true, default-features = false }
windmill-api-auth.workspace = true
windmill-api-scripts.workspace = true
windmill-api-workspaces.workspace = true
windmill-audit.workspace = true
windmill-git-sync.workspace = true
@@ -31,5 +32,6 @@ serde.workspace = true
serde_json.workspace = true
sql-builder.workspace = true
sqlx.workspace = true
tokio.workspace = true
tracing.workspace = true
uuid.workspace = true
+8 -3
View File
@@ -9,8 +9,8 @@
//! What a workspace member or group can reach, and why.
use axum::{
extract::{Extension, Path, Query},
routing::get,
extract::{DefaultBodyLimit, Extension, Path, Query},
routing::{get, post},
Json, Router,
};
use serde::{Deserialize, Serialize};
@@ -28,6 +28,11 @@ pub fn workspaced_service() -> Router {
.route("/get", get(get_access))
.route("/browse_folders", get(browse_folders))
.route("/browse_items", get(browse_items))
// The body can carry a full-code app's files.
.route(
"/reads",
post(crate::access_reads::get_reads).layer(DefaultBodyLimit::max(16 << 20)),
)
}
const BROWSE_PAGE: i64 = 100;
@@ -437,7 +442,7 @@ fn grant_blocked(authed: &ApiAuthed, path: &str) -> Option<String> {
}
fn folder_blocked(folder: &str) -> String {
format!("Only workspace admins and owners of f/{folder} can share it")
format!("Only admins of the workspace or of f/{folder} can share it")
}
async fn browse_folders(
File diff suppressed because it is too large Load Diff
+1
View File
@@ -1,4 +1,5 @@
pub mod access;
mod access_reads;
pub mod folder_history;
pub mod folders;
pub mod granular_acls;
+143
View File
@@ -25348,6 +25348,31 @@ paths:
type: boolean
required: [items, has_more]
/w/{workspace}/access/reads:
post:
summary: list what an item reads when it runs, and who performs each read
description: >-
Reads only what the caller can read: anything else is listed under `errors`, with the
same line whether it is missing or hidden.
operationId: getAccessReads
tags:
- granular_acl
parameters:
- $ref: "#/components/parameters/WorkspaceId"
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/AccessReadsRequest"
responses:
"200":
description: what the item reads, each read with who performs it
content:
application/json:
schema:
$ref: "#/components/schemas/AccessReads"
/w/{workspace}/acls/get/{kind}/{path}:
get:
summary: get granular acls
@@ -37430,6 +37455,124 @@ components:
type: boolean
required: [type]
AccessReadsRequest:
type: object
properties:
kind:
type: string
enum: [script, flow, app]
path:
description: the saved item to scan or, with `source`, the path that labels it
type: string
source:
$ref: "#/components/schemas/AccessReadsSource"
required: [kind]
AccessReadsSource:
description: unsaved editor content, scanned in place of the saved item
type: object
properties:
language:
description: a script's language
type: string
content:
description: a script's code, required for a script
type: string
modules:
description: a script's other files, by name
value:
description: a flow's or an app's value, required for those
schema:
description: a script's or a flow's input schema
on_behalf_of:
description: the account a script or flow runs as, when it has one
type: string
policy:
description: an app's policy, which says who its runnables run as
AccessReads:
type: object
properties:
runs_as:
$ref: "#/components/schemas/AccessRunsAs"
references:
type: array
items:
$ref: "#/components/schemas/AccessReference"
others:
description: assets that are not workspace items, such as data tables and object storage files
type: array
items:
$ref: "#/components/schemas/AccessOtherAsset"
errors:
description: what could not be followed, one line each
type: array
items:
type: string
required: [runs_as, references, others, errors]
AccessRunsAs:
description: who the scanned item runs as
type: object
properties:
type:
type: string
enum: [caller, account]
principal:
description: the account, for `account`; missing for an app whose policy names none
type: string
required: [type]
AccessReader:
description: >-
who performs a read: `viewer` is whoever opens the app, `root` whoever the scanned item
runs as, and `account` the fixed identity of a runnable the item reaches
type: object
properties:
type:
type: string
enum: [viewer, root, account]
principal:
description: the account, for `account`
type: string
required: [type]
AccessReference:
type: object
properties:
kind:
type: string
enum: [resource, variable, script, flow]
path:
type: string
access:
description: "`run` is a script or flow started through the run API, which takes read on it"
type: string
enum: [read, write, run]
reader:
$ref: "#/components/schemas/AccessReader"
sources:
description: the parts of the item that make the read, as `<path> › <step or file>`
type: array
items:
type: string
required: [kind, path, access, reader, sources]
AccessOtherAsset:
type: object
properties:
kind:
$ref: "#/components/schemas/AssetKind"
path:
type: string
reader:
$ref: "#/components/schemas/AccessReader"
sources:
type: array
items:
type: string
required: [kind, path, reader, sources]
Group:
type: object
properties: