-
Notifications
You must be signed in to change notification settings - Fork 106
chore(splinter): initial commit #594
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 12 commits
Commits
Show all changes
15 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 7959d9e
progress
psteinroe d91f55b
progress
psteinroe a747f39
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-b869d517301aaf69d382f092c09d5a53712d68afd273423f9310cd793586f532.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
| 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 @@ | ||
| - implement ignore / include and config. try to refactor existing analyser infrastructure to be re-used. |
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,109 @@ | ||
| 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"; | ||
juleswritescode marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| fn main() { | ||
| let out_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); | ||
| let vendor_dir = Path::new(&out_dir).join("vendor"); | ||
| let sql_file = vendor_dir.join("splinter.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 !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(&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(dest_path: &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); | ||
|
|
||
| // Write to destination | ||
| fs::write(dest_path, processed_content).expect("Failed to write splinter.sql"); | ||
|
|
||
| println!("cargo:warning=Successfully downloaded and processed splinter.sql"); | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
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,136 @@ | ||
| use pgls_diagnostics::{Category, Severity, category}; | ||
| use serde_json::Value; | ||
|
|
||
| use crate::{SplinterAdvices, SplinterDiagnostic, SplinterQueryResult}; | ||
|
|
||
| impl From<SplinterQueryResult> for SplinterDiagnostic { | ||
| fn from(result: SplinterQueryResult) -> Self { | ||
| let severity = parse_severity(&result.level); | ||
|
|
||
| // Extract common fields from metadata | ||
| let (schema, object_name, object_type, additional_metadata) = | ||
| extract_metadata_fields(&result.metadata); | ||
|
|
||
| // for now, we just take the first category as the group | ||
| let group = result | ||
| .categories | ||
| .first() | ||
| .map(|s| s.to_lowercase()) | ||
| .unwrap_or_else(|| "unknown".to_string()); | ||
|
|
||
| SplinterDiagnostic { | ||
| category: rule_name_to_category(&result.name, &group), | ||
| message: result.detail.into(), | ||
| severity, | ||
| advices: SplinterAdvices { | ||
| description: result.description, | ||
| schema, | ||
| object_name, | ||
| object_type, | ||
| remediation_url: result.remediation, | ||
| additional_metadata, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Parse severity level from the query result | ||
| fn parse_severity(level: &str) -> Severity { | ||
| match level { | ||
| "INFO" => Severity::Information, | ||
| "WARN" => Severity::Warning, | ||
| "ERROR" => Severity::Error, | ||
| _ => Severity::Information, // default to info | ||
| } | ||
| } | ||
|
|
||
| /// Convert rule name and group to a Category | ||
| /// Note: Rule names use snake_case, but categories use camelCase | ||
| fn rule_name_to_category(name: &str, group: &str) -> &'static Category { | ||
| match (group, name) { | ||
| ("performance", "unindexed_foreign_keys") => { | ||
| category!("splinter/performance/unindexedForeignKeys") | ||
| } | ||
| ("performance", "auth_rls_initplan") => { | ||
| category!("splinter/performance/authRlsInitplan") | ||
| } | ||
| ("performance", "no_primary_key") => category!("splinter/performance/noPrimaryKey"), | ||
| ("performance", "unused_index") => category!("splinter/performance/unusedIndex"), | ||
| ("performance", "duplicate_index") => category!("splinter/performance/duplicateIndex"), | ||
| ("performance", "table_bloat") => category!("splinter/performance/tableBloat"), | ||
| ("performance", "multiple_permissive_policies") => { | ||
| category!("splinter/performance/multiplePermissivePolicies") | ||
| } | ||
| ("security", "auth_users_exposed") => category!("splinter/security/authUsersExposed"), | ||
| ("security", "extension_versions_outdated") => { | ||
| category!("splinter/security/extensionVersionsOutdated") | ||
| } | ||
| ("security", "policy_exists_rls_disabled") => { | ||
| category!("splinter/security/policyExistsRlsDisabled") | ||
| } | ||
| ("security", "rls_enabled_no_policy") => { | ||
| category!("splinter/security/rlsEnabledNoPolicy") | ||
| } | ||
| ("security", "security_definer_view") => { | ||
| category!("splinter/security/securityDefinerView") | ||
| } | ||
| ("security", "function_search_path_mutable") => { | ||
| category!("splinter/security/functionSearchPathMutable") | ||
| } | ||
| ("security", "rls_disabled_in_public") => { | ||
| category!("splinter/security/rlsDisabledInPublic") | ||
| } | ||
| ("security", "extension_in_public") => category!("splinter/security/extensionInPublic"), | ||
| ("security", "rls_references_user_metadata") => { | ||
| category!("splinter/security/rlsReferencesUserMetadata") | ||
| } | ||
| ("security", "materialized_view_in_api") => { | ||
| category!("splinter/security/materializedViewInApi") | ||
| } | ||
| ("security", "foreign_table_in_api") => { | ||
| category!("splinter/security/foreignTableInApi") | ||
| } | ||
| ("security", "unsupported_reg_types") => { | ||
| category!("splinter/security/unsupportedRegTypes") | ||
| } | ||
| ("security", "insecure_queue_exposed_in_api") => { | ||
| category!("splinter/security/insecureQueueExposedInApi") | ||
| } | ||
| ("security", "fkey_to_auth_unique") => category!("splinter/security/fkeyToAuthUnique"), | ||
| _ => category!("splinter/unknown/unknown"), | ||
| } | ||
| } | ||
|
|
||
| /// Extract common metadata fields and return the rest as additional_metadata | ||
| fn extract_metadata_fields( | ||
| metadata: &Value, | ||
| ) -> ( | ||
| Option<String>, | ||
| Option<String>, | ||
| Option<String>, | ||
| Option<Value>, | ||
| ) { | ||
| if let Some(obj) = metadata.as_object() { | ||
| let schema = obj.get("schema").and_then(|v| v.as_str()).map(String::from); | ||
|
|
||
| let object_name = obj.get("name").and_then(|v| v.as_str()).map(String::from); | ||
|
|
||
| let object_type = obj.get("type").and_then(|v| v.as_str()).map(String::from); | ||
|
|
||
| // Create a new object without the common fields | ||
| let mut additional = obj.clone(); | ||
| additional.remove("schema"); | ||
| additional.remove("name"); | ||
| additional.remove("type"); | ||
|
|
||
| let additional_metadata = if additional.is_empty() { | ||
| None | ||
| } else { | ||
| Some(Value::Object(additional)) | ||
| }; | ||
|
|
||
| (schema, object_name, object_type, additional_metadata) | ||
| } else { | ||
| (None, None, None, Some(metadata.clone())) | ||
| } | ||
| } |
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.
personal note?