-
Notifications
You must be signed in to change notification settings - Fork 106
refractor(splinter): allow non-supabase rules to run always #597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
59d1c1d
chore(splinter): initial commit
psteinroe 9d34d82
progress
psteinroe f3fd30c
progress
psteinroe 964de40
progress
psteinroe 09c5718
progress
psteinroe bc721f7
fix: test
psteinroe 92005d8
refactor: use categories as group
psteinroe 1dcb8d7
refactor: use categories as group
psteinroe 60fd54e
fix: format
psteinroe e7a2dd5
fix: unknown url and add groups to categories
psteinroe c35affc
fix: unknown url and add groups to categories
psteinroe 81b811c
fix: lint
psteinroe 79f4c9f
refactor: split splinter rules
psteinroe 154e3bb
fix: codegen
psteinroe 6babd1a
fix: resolve clippy warnings and improve code quality
psteinroe 600f273
refactor: no manual url building
psteinroe 3469bbf
progress
psteinroe 2cdf3e2
progress
psteinroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
.sqlx/query-02927e584e85871ba6f84c58e8b5e4454b2c36eaf034657d5d2d95633fb85bdb.json
Large diffs are not rendered by default.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
.sqlx/query-425fc6118e76cea42cf256b3b0f11046dc8b77d84c94314a0ed0716e5803df69.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...plinter/.sqlx/query-02927e584e85871ba6f84c58e8b5e4454b2c36eaf034657d5d2d95633fb85bdb.json
Large diffs are not rendered by default.
Oops, something went wrong.
74 changes: 74 additions & 0 deletions
74
...plinter/.sqlx/query-425fc6118e76cea42cf256b3b0f11046dc8b77d84c94314a0ed0716e5803df69.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| [package] | ||
| authors.workspace = true | ||
| categories.workspace = true | ||
| description = "<DESCRIPTION>" | ||
| edition.workspace = true | ||
| homepage.workspace = true | ||
| keywords.workspace = true | ||
| license.workspace = true | ||
| name = "pgls_splinter" | ||
| repository.workspace = true | ||
| version = "0.0.0" | ||
|
|
||
| [dependencies] | ||
| pgls_diagnostics.workspace = true | ||
| serde.workspace = true | ||
| serde_json.workspace = true | ||
| sqlx.workspace = true | ||
|
|
||
| [build-dependencies] | ||
| ureq = "2.10" | ||
|
|
||
| [dev-dependencies] | ||
| insta.workspace = true | ||
| pgls_console.workspace = true | ||
| pgls_test_utils.workspace = true | ||
|
|
||
| [lib] | ||
| doctest = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| use std::env; | ||
| use std::fs; | ||
| use std::path::Path; | ||
|
|
||
| // Update this commit SHA to pull in a new version of splinter.sql | ||
| const SPLINTER_COMMIT_SHA: &str = "27ea2ece65464213e466cd969cc61b6940d16219"; | ||
|
|
||
| // Rules that require Supabase-specific infrastructure (auth schema, anon/authenticated roles, pgrst.db_schemas) | ||
| const SUPABASE_ONLY_RULES: &[&str] = &[ | ||
| "auth_users_exposed", | ||
| "auth_rls_initplan", | ||
| "rls_disabled_in_public", | ||
| "security_definer_view", | ||
| "rls_references_user_metadata", | ||
| "materialized_view_in_api", | ||
| "foreign_table_in_api", | ||
| "insecure_queue_exposed_in_api", | ||
| "fkey_to_auth_unique", | ||
| ]; | ||
|
|
||
| fn main() { | ||
| let out_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| let vendor_dir = Path::new(&out_dir).join("vendor"); | ||
| let generic_sql_file = vendor_dir.join("splinter_generic.sql"); | ||
| let supabase_sql_file = vendor_dir.join("splinter_supabase.sql"); | ||
| let sha_file = vendor_dir.join("COMMIT_SHA.txt"); | ||
|
|
||
| // Create vendor directory if it doesn't exist | ||
| if !vendor_dir.exists() { | ||
| fs::create_dir_all(&vendor_dir).expect("Failed to create vendor directory"); | ||
| } | ||
|
|
||
| // Check if we need to download | ||
| let needs_download = | ||
| if !generic_sql_file.exists() || !supabase_sql_file.exists() || !sha_file.exists() { | ||
| true | ||
| } else { | ||
| // Check if stored SHA matches current constant | ||
| let stored_sha = fs::read_to_string(&sha_file) | ||
| .expect("Failed to read COMMIT_SHA.txt") | ||
| .trim() | ||
| .to_string(); | ||
| stored_sha != SPLINTER_COMMIT_SHA | ||
| }; | ||
|
|
||
| if needs_download { | ||
| println!( | ||
| "cargo:warning=Downloading splinter.sql from GitHub (commit: {SPLINTER_COMMIT_SHA})" | ||
| ); | ||
| download_and_process_sql(&generic_sql_file, &supabase_sql_file); | ||
| fs::write(&sha_file, SPLINTER_COMMIT_SHA).expect("Failed to write COMMIT_SHA.txt"); | ||
| } | ||
|
|
||
| // Tell cargo to rerun if build.rs or SHA file changes | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| println!("cargo:rerun-if-changed=vendor/COMMIT_SHA.txt"); | ||
| } | ||
|
|
||
| fn download_and_process_sql(generic_dest: &Path, supabase_dest: &Path) { | ||
| let url = format!( | ||
| "https://raw.githubusercontent.com/supabase/splinter/{SPLINTER_COMMIT_SHA}/splinter.sql" | ||
| ); | ||
|
|
||
| // Download the file | ||
| let response = ureq::get(&url) | ||
| .call() | ||
| .expect("Failed to download splinter.sql"); | ||
|
|
||
| let content = response | ||
| .into_string() | ||
| .expect("Failed to read response body"); | ||
|
|
||
| // Remove the SET LOCAL search_path section | ||
| let mut processed_content = remove_set_search_path(&content); | ||
|
|
||
| // Add "!" suffix to column aliases for sqlx non-null checking | ||
| processed_content = add_not_null_markers(&processed_content); | ||
|
|
||
| // Split into generic and Supabase-specific queries | ||
| let (generic_queries, supabase_queries) = split_queries(&processed_content); | ||
|
|
||
| // Write to destination files | ||
| fs::write(generic_dest, generic_queries).expect("Failed to write splinter_generic.sql"); | ||
| fs::write(supabase_dest, supabase_queries).expect("Failed to write splinter_supabase.sql"); | ||
|
|
||
| println!( | ||
| "cargo:warning=Successfully downloaded and processed splinter.sql into generic and Supabase-specific files" | ||
| ); | ||
| } | ||
|
|
||
| fn remove_set_search_path(content: &str) -> String { | ||
| content | ||
| .lines() | ||
| .filter(|line| { | ||
| let trimmed = line.trim(); | ||
| !trimmed.to_lowercase().starts_with("set local search_path") | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join("\n") | ||
| } | ||
|
|
||
| fn add_not_null_markers(content: &str) -> String { | ||
| // Add "!" suffix to all column aliases to mark them as non-null for sqlx | ||
| // This transforms patterns like: 'value' as name | ||
| // Into: 'value' as "name!" | ||
|
|
||
| let columns_to_mark = [ | ||
| "name", | ||
| "title", | ||
| "level", | ||
| "facing", | ||
| "categories", | ||
| "description", | ||
| "detail", | ||
| "remediation", | ||
| "metadata", | ||
| "cache_key", | ||
| ]; | ||
|
|
||
| let mut result = content.to_string(); | ||
|
|
||
| for column in &columns_to_mark { | ||
| // Match patterns like: as name, as name) | ||
| let pattern_comma = format!(" as {column}"); | ||
| let replacement_comma = format!(" as \"{column}!\""); | ||
| result = result.replace(&pattern_comma, &replacement_comma); | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| fn split_queries(content: &str) -> (String, String) { | ||
| // Split the union all queries based on rule names | ||
| let queries: Vec<&str> = content.split("union all").collect(); | ||
|
|
||
| let mut generic_queries = Vec::new(); | ||
| let mut supabase_queries = Vec::new(); | ||
|
|
||
| for query in queries { | ||
| // Extract the rule name from the query (it's the first 'name' field) | ||
| let is_supabase = SUPABASE_ONLY_RULES | ||
| .iter() | ||
| .any(|rule| query.contains(&format!("'{rule}' as \"name!\""))); | ||
|
|
||
| if is_supabase { | ||
| supabase_queries.push(query); | ||
| } else { | ||
| generic_queries.push(query); | ||
| } | ||
| } | ||
|
|
||
| // Join queries with "union all" and wrap in parentheses | ||
| let generic_sql = if generic_queries.is_empty() { | ||
| String::new() | ||
| } else { | ||
| generic_queries.join("union all\n") | ||
| }; | ||
|
|
||
| let supabase_sql = if supabase_queries.is_empty() { | ||
| String::new() | ||
| } else { | ||
| supabase_queries.join("union all\n") | ||
| }; | ||
|
|
||
| (generic_sql, supabase_sql) | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as I understand it, rules are per default put into the generic category.
what happens if supabase adds a new rule to the spliter that's supabase-specific?
that might break linting in case a table isn't defined for non-supabase databases, right?
should we throw during build if there's an unknown rule in the splinter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great catch!