Skip to content

feat: commit_comment option for prepending a SQL comment to a COMMIT statement #669

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
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/postgrex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -578,11 +578,14 @@ defmodule Postgrex do
* `:timeout` - Transaction timeout (default: `#{@timeout}`);
* `:mode` - Set to `:savepoint` to use savepoints instead of an SQL
transaction, otherwise set to `:transaction` (default: `:transaction`);
* `:commit_comment` - When a binary string is provided, prepends the text as
a comment attached to the `COMMIT` statement issued to close the transaction (default: `nil`);

The `:timeout` is for the duration of the transaction and all nested
transactions and requests. This timeout overrides timeouts set by internal
transactions and requests. The `:mode` will be used for all requests inside
the transaction function.
the transaction function. The `:commit_comment` can be helpful in distinguishing
between transactions in query performance monitoring tools.

## Example

Expand Down
32 changes: 29 additions & 3 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ defmodule Postgrex.Protocol do
@nonposix_errors [:closed, :timeout]
@max_rows 500
@text_type_oid 25
@commit_comment_validation_error Postgrex.QueryError.exception(
"`:comment_comment` option cannot contain sequence \"*/\""
)

defstruct sock: nil,
connection_id: nil,
Expand Down Expand Up @@ -537,16 +540,19 @@ defmodule Postgrex.Protocol do
{:ok, Postgrex.Result.t(), state}
| {DBConnection.status(), state}
| {:disconnect, %RuntimeError{}, state}
| {:disconnect, %DBConnection.ConnectionError{} | Postgrex.Error.t(), state}
| {:disconnect,
%DBConnection.ConnectionError{} | Postgrex.Error.t() | Postgrex.QueryError.t(),
Copy link
Member

Choose a reason for hiding this comment

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

Let's return Postgrex.Error. It is not worth introducing a new exception type only for this case. WDYT? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fine with me!

Copy link
Member

Choose a reason for hiding this comment

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

We can also return a RuntimeError if you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went with Postgrex.Error in af319f9

state}
def handle_commit(_, %{postgres: {_, _}} = s) do
lock_error(s, :commit)
end

def handle_commit(opts, %{postgres: postgres} = s) do
case Keyword.get(opts, :mode, :transaction) do
:transaction when postgres == :transaction ->
statement = "COMMIT"
handle_transaction(statement, opts, s)
with {:ok, statement} <- build_commit_statement(opts, s) do
handle_transaction(statement, opts, s)
end

:savepoint when postgres == :transaction ->
statement = "RELEASE SAVEPOINT postgrex_savepoint"
Expand Down Expand Up @@ -2887,6 +2893,22 @@ defmodule Postgrex.Protocol do

## transaction

defp build_commit_statement(opts, %{buffer: buffer} = s) do
case Keyword.get(opts, :commit_comment) do
comment when is_binary(comment) ->
if String.contains?(comment, "*/") do
disconnect(s, @commit_comment_validation_error, buffer)
else
statement = "/* #{comment} */\nCOMMIT"
{:ok, statement}
end

_ ->
statement = "COMMIT"
{:ok, statement}
end
end

defp handle_transaction(statement, opts, %{buffer: buffer} = s) do
status = new_status(opts, mode: :transaction)
msgs = [msg_query(statement: statement)]
Expand Down Expand Up @@ -3368,6 +3390,10 @@ defmodule Postgrex.Protocol do
{:disconnect, %{err | connection_id: connection_id}, %{s | buffer: buffer}}
end

defp disconnect(s, %Postgrex.QueryError{} = err, buffer) do
{:disconnect, err, %{s | buffer: buffer}}
end

defp disconnect(s, %RuntimeError{} = err, buffer) do
{:disconnect, err, %{s | buffer: buffer}}
end
Expand Down
16 changes: 16 additions & 0 deletions test/transaction_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ defmodule TransactionTest do
end) == {:error, :rollback}
end

@tag mode: :transaction
test "commit comment with possible SQL injection returns error and disconnects", context do
assert_raise(
Postgrex.QueryError,
"`:comment_comment` option cannot contain sequence \"*/\"",
fn ->
transaction(
fn conn ->
assert {:ok, %Postgrex.Result{rows: [[42]]}} = P.query(conn, "SELECT 42", [])
end,
commit_comment: "invalid */ comment"
)
end
)
end

@tag mode: :transaction
@tag prepare: :unnamed
test "transaction commits with unnamed queries", context do
Expand Down