pgaudit.git / summary / log / commit / refs

commit    be3ede072b2835c36d7bbccc898fedcbce247156
Author:   Christophe Pettus <xof@thebuild.com>
Commit:   GitHub <noreply@github.com>
Date:     Sat Apr 29 16:22:53 2023 +0000

    Add log_parameter_max_size to set maximum size of logged parameters.
    
    If setting is non-zero, any parameter value whose storage representation is longer in bytes than the setting is replaced by a placeholder, <long param suppressed>. We use the storage format size rather than number of characters to avoid having to pull in, potentially decompress, and character-encode or scan the value just to throw it away.


README.md | 6 ++++++ expected/pgaudit.out | 30 ++++++++++++++++++++++++++++ pgaudit.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++----- sql/pgaudit.sql | 31 +++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 538b22d..984fa30 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,12 @@ Specifies that audit logging should include the parameters that were passed with The default is `off`. +### pgaudit.log_parameter_max_size + +Specifies that parameter values longer than this setting (in bytes) should not be logged, but replaced with `<long param suppressed>`. This is set in bytes, not characters, so does not account for multi-byte characters in a text parameters's encoding. This setting has no effect if `log_parameter` is `off`. If this setting is 0 (the default), all parameters are logged regardless of length + +The default is 0. + ### pgaudit.log_relation Specifies whether session audit logging should create a separate log entry for each relation (`TABLE`, `VIEW`, etc.) referenced in a `SELECT` or `DML` statement. This is a useful shortcut for exhaustive logging without using object audit logging. diff --git a/expected/pgaudit.out b/expected/pgaudit.out index e3560ae..e552809 100644 --- a/expected/pgaudit.out +++ b/expected/pgaudit.out @@ -382,6 +382,34 @@ WARNING: AUDIT: OBJECT,3,1,WRITE,UPDATE,TABLE,public.test4,"UPDATE public.test4 update public.test4 set name = 'foo' where name = 'bar'; WARNING: AUDIT: OBJECT,4,1,WRITE,UPDATE,TABLE,public.test4,update public.test4 set name = 'foo' where name = 'bar';,<not logged> -- +-- Confirm that "long" parameter values will not be logged if pgaudit.log_parameter_max_size +-- is set. +\connect - :current_user +ALTER ROLE user2 SET pgaudit.log_parameter_max_size = 50; +NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_parameter_max_size = 50;,<not logged> +ALTER ROLE user2 SET pgaudit.log_parameter = 'on'; +NOTICE: AUDIT: SESSION,2,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 SET pgaudit.log_parameter = 'on';,<not logged> +\connect - user2 +PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2); +EXECUTE testinsert(1, '*******************************************************'); +WARNING: AUDIT: OBJECT,1,1,WRITE,INSERT,TABLE,public.test4,"PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2);","1,<long param suppressed>" +DEALLOCATE testinsert; +\connect - :current_user +ALTER ROLE user2 RESET pgaudit.log_parameter_max_size; +NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 RESET pgaudit.log_parameter_max_size;,<not logged> +\connect - user2 +PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2); +EXECUTE testinsert(2, '*******************************************************'); +WARNING: AUDIT: OBJECT,1,1,WRITE,INSERT,TABLE,public.test4,"PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2);","2,*******************************************************" +DEALLOCATE testinsert; +\connect - :current_user +ALTER ROLE user2 RESET pgaudit.log_parameter; +NOTICE: AUDIT: SESSION,1,1,ROLE,ALTER ROLE,,,ALTER ROLE user2 RESET pgaudit.log_parameter;,<not logged> +-- -- Change permissions of user 1 so that session logging will be done \connect - :current_user -- @@ -2538,6 +2566,7 @@ ALTER ROLE :current_user RESET pgaudit.log_catalog; ALTER ROLE :current_user RESET pgaudit.log_client; ALTER ROLE :current_user RESET pgaudit.log_level; ALTER ROLE :current_user RESET pgaudit.log_parameter; +ALTER ROLE :current_user RESET pgaudit.log_parameter_max_size; ALTER ROLE :current_user RESET pgaudit.log_relation; ALTER ROLE :current_user RESET pgaudit.log_statement; ALTER ROLE :current_user RESET pgaudit.log_statement_once; @@ -2546,6 +2575,7 @@ RESET pgaudit.log; RESET pgaudit.log_catalog; RESET pgaudit.log_level; RESET pgaudit.log_parameter; +RESET pgaudit.log_parameter_max_size; RESET pgaudit.log_relation; RESET pgaudit.log_statement; RESET pgaudit.log_statement_once; diff --git a/pgaudit.c b/pgaudit.c index 49ba60c..7050636 100644 --- a/pgaudit.c +++ b/pgaudit.c @@ -135,6 +135,17 @@ bool auditLogParameter = false; */ bool auditLogRelation = false; +/* + * GUC variable for pgaudit.log_parameter_max_size + * + * Administrators can choose to prevent the logging of large variable-length + * parameters. If set to 0 (the default), all parameters are logged. If set + * greater than 0, variable length parameters (before character output) whose + * size is greater than the specified number of bytes will be replaced + * by a placeholder. +*/ +int auditLogParameterMaxSize = 0; + /* * GUC variable for pgaudit.log_rows * @@ -736,7 +747,6 @@ log_audit_event(AuditEventStackItem *stackItem) ParamExternData *prm = &paramList->params[paramIdx]; Oid typeOutput; bool typeIsVarLena; - char *paramStr; /* Add a comma for each param */ if (paramIdx != 0) @@ -746,12 +756,27 @@ log_audit_event(AuditEventStackItem *stackItem) if (prm->isnull || !OidIsValid(prm->ptype)) continue; - /* Output the string */ + /* + * Append the param, suppressing long params if the appropriate + * GUC is set. + */ getTypeOutputInfo(prm->ptype, &typeOutput, &typeIsVarLena); - paramStr = OidOutputFunctionCall(typeOutput, prm->value); - append_valid_csv(&paramStrResult, paramStr); - pfree(paramStr); + if (auditLogParameterMaxSize > 0 && + typeIsVarLena && + VARSIZE_ANY_EXHDR(prm->value) > auditLogParameterMaxSize) + { + append_valid_csv(&paramStrResult, + "<long param suppressed>"); + } + else + { + char *paramStr = OidOutputFunctionCall(typeOutput, + prm->value); + + append_valid_csv(&paramStrResult, paramStr); + pfree(paramStr); + } } if (numParams == 0) @@ -2096,6 +2121,26 @@ _PG_init(void) GUC_NOT_IN_SAMPLE, NULL, NULL, NULL); + /* Define pgaudit.log_parameter_max_size */ + DefineCustomIntVariable( + "pgaudit.log_parameter_max_size", + + "Specifies, in bytes, the maximum length of variable-length parameters " + "to log. If 0 (the default), parameters are not checked for size. If " + "set, when the size of the parameter is longer than the setting, the " + "value in the audit log is replaced with a placeholder. Note that for " + "character types, the length is in bytes for the parameter's encoding, " + "not characters.", + + NULL, + &auditLogParameterMaxSize, + 0, + 0, + (1 << 30) - 1, + PGC_SUSET, + GUC_NOT_IN_SAMPLE, + NULL, NULL, NULL); + /* Define pgaudit.log_relation */ DefineCustomBoolVariable( "pgaudit.log_relation", diff --git a/sql/pgaudit.sql b/sql/pgaudit.sql index a6a7cc7..5cf9f4f 100644 --- a/sql/pgaudit.sql +++ b/sql/pgaudit.sql @@ -300,6 +300,35 @@ UPDATE public.test4 -- update (name) takes precedence over select (name) due to ordering update public.test4 set name = 'foo' where name = 'bar'; +-- +-- Confirm that "long" parameter values will not be logged if pgaudit.log_parameter_max_size +-- is set. +\connect - :current_user +ALTER ROLE user2 SET pgaudit.log_parameter_max_size = 50; +ALTER ROLE user2 SET pgaudit.log_parameter = 'on'; + +\connect - user2 +PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2); + +EXECUTE testinsert(1, '*******************************************************'); + +DEALLOCATE testinsert; + +\connect - :current_user +ALTER ROLE user2 RESET pgaudit.log_parameter_max_size; + +\connect - user2 +PREPARE testinsert(int, text) AS + INSERT INTO test4 VALUES($1, $2); + +EXECUTE testinsert(2, '*******************************************************'); + +DEALLOCATE testinsert; + +\connect - :current_user +ALTER ROLE user2 RESET pgaudit.log_parameter; + -- -- Change permissions of user 1 so that session logging will be done \connect - :current_user @@ -1611,6 +1640,7 @@ ALTER ROLE :current_user RESET pgaudit.log_catalog; ALTER ROLE :current_user RESET pgaudit.log_client; ALTER ROLE :current_user RESET pgaudit.log_level; ALTER ROLE :current_user RESET pgaudit.log_parameter; +ALTER ROLE :current_user RESET pgaudit.log_parameter_max_size; ALTER ROLE :current_user RESET pgaudit.log_relation; ALTER ROLE :current_user RESET pgaudit.log_statement; ALTER ROLE :current_user RESET pgaudit.log_statement_once; @@ -1620,6 +1650,7 @@ RESET pgaudit.log; RESET pgaudit.log_catalog; RESET pgaudit.log_level; RESET pgaudit.log_parameter; +RESET pgaudit.log_parameter_max_size; RESET pgaudit.log_relation; RESET pgaudit.log_statement; RESET pgaudit.log_statement_once; [parent: 1d07ac4851fe]