Skip to content

Make event tables inaccessible from views#4459

Open
joshua-spacetime wants to merge 1 commit intomasterfrom
joshua/fix/event-table-views
Open

Make event tables inaccessible from views#4459
joshua-spacetime wants to merge 1 commit intomasterfrom
joshua/fix/event-table-views

Conversation

@joshua-spacetime
Copy link
Collaborator

@joshua-spacetime joshua-spacetime commented Feb 25, 2026

Description of Changes

According to the docs, event tables should not be accessible from views, but in fact they are. This patch stops generating view bindings for event tables across rust, c#, and typescript.

API and ABI breaking changes

Technically a breaking change, but they were never supposed to be there in the first place and would not work correctly regardless.

Expected complexity level and risk

2

Testing

Added negative compile tests for trying to access event tables from within a view.

Comment on lines +27 to +31
var _ = ctx.Db.ViewAuditEvent;
^^^^^^^^^^^^^^
return null;
*/
Message: 'LocalReadOnly' does not contain a definition for 'ViewAuditEvent' and no accessible extension method 'ViewAuditEvent' accepting a first argument of type 'LocalReadOnly' could be found (are you missing a using directive or an assembly reference?),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No longer generating bindings for event tables.

Comment on lines +188 to +192
return ctx.From.ViewAuditEvent();
^^^^^^^^^^^^^^
}
*/
Message: 'QueryBuilder' does not contain a definition for 'ViewAuditEvent' and no accessible extension method 'ViewAuditEvent' accepting a first argument of type 'QueryBuilder' could be found (are you missing a using directive or an assembly reference?),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same is true for the query builder

}

foreach (var accessor in TableAccessors)
foreach (var accessor in TableAccessors.Where(accessor => !accessor.IsEvent))
Copy link
Collaborator Author

@joshua-spacetime joshua-spacetime Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restricting Ctx.Db in C# (procedural views)

var globalRowName = $"global::{FullName}";

foreach (var accessor in TableAccessors)
foreach (var accessor in TableAccessors.Where(accessor => !accessor.IsEvent))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restricting Ctx.From in C# (query builder views)

let trait_def_view = quote_spanned! {table_ident.span()=>
#[allow(non_camel_case_types, dead_code)]
#vis trait #view_trait_ident {
let trait_def_view = if args.event.is_none() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restricting ctx.db in rust (procedural views)

#vis trait #query_trait_ident {
fn #table_ident(&self) -> spacetimedb::query_builder::Table<#original_struct_ident> {
spacetimedb::query_builder::Table::new(stringify!(#table_ident))
let table_query_handle_def = if args.event.is_none() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restricting ctx.from in rust (query builder views)

}

get #viewSchema() {
return (this.#viewSchema_ ??= withoutEventTables(this.#schema.schemaType));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restrict ctx.db and ctx.from in typescript

return (this.#viewSchema_ ??= withoutEventTables(this.#schema.schemaType));
}

get #viewDbView() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is necessary, because with just the typing alone the users won't be able to access the event tables (and if they bypass it, it'll error).

Copy link
Collaborator Author

@joshua-spacetime joshua-spacetime Feb 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and if they bypass it, it'll error

Will it? Without these hooks, view contexts get access to the full schema at runtime, so it may not error if you bypass the type system, right?

@joshua-spacetime joshua-spacetime linked an issue Feb 25, 2026 that may be closed by this pull request
@joshua-spacetime joshua-spacetime force-pushed the joshua/fix/event-table-views branch from 442a4ec to 0a492c7 Compare February 26, 2026 17:34
Copy link
Contributor

@rekhoff rekhoff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing the C# changes, these look solid. I am approving the C# portion of the changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Event tables accessible from views

3 participants