Skip to content
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

Handle tls connection during cancellation #227

Merged

Conversation

enidgjoleka
Copy link
Contributor

Hi,

Looking at the epgsql:cancel/x it appears like epgsql opens a new temporary TCP connection and sends the cancellation request. This is fine for applications which uses normal non-TLS connections.

However, if we are dealing with an application which uses TLS connections for all its queries except for the cancellation request then we might open ourselves to the man-in-the-middle-attack.
The attacker can in theory listen to our cancellation request and detect the previously generated secret key during the connection establishment of the original connection.
Once the attacker gets hold of that key while the original connection can still accept queries even after its last query was cancelled then the attacker can in theory cancel all the queries fired from the original connection.

This PR tries to address this issue.
I decided to rewrite part of epgsql_cmd_connect to better decouple opening a socket with the rest of the logic. This newly introduced function, open_socket/x, was then called from epgsql_sock.erl during the cancellation.

Please let me know if I missed something!

Copy link
Member

@seriyps seriyps left a comment

Choose a reason for hiding this comment

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

I will look more precisely a bit later, but this password thing is absolutely necessary to fix

src/commands/epgsql_cmd_connect.erl Outdated Show resolved Hide resolved
@enidgjoleka enidgjoleka force-pushed the handle-tls-connection-during-cancellation branch from 8826bac to 0709950 Compare March 25, 2020 08:31
Copy link
Member

@seriyps seriyps left a comment

Choose a reason for hiding this comment

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

I think it's fine now. Have some minor comments regarding the tests.

src/epgsql_sock.erl Outdated Show resolved Hide resolved
test/epgsql_SUITE.erl Outdated Show resolved Hide resolved
Self = self(),
spawn_link(fun() ->
?assertMatch(?QUERY_CANCELED, Module:equery(C, "SELECT pg_sleep(5)")),
Self ! done
Copy link
Member

Choose a reason for hiding this comment

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

another option is to send the result to Self and assert in receiver:

Self ! {done, Module:equery(C, "..")}
end,
<..>
receive {done, Result} ->
    ?assertMatch(?QUERY_CANCELLED, Result)
after ....

test/epgsql_SUITE.erl Outdated Show resolved Hide resolved
@seriyps seriyps requested a review from davidw March 25, 2020 18:14
…uced tests, handle open_socket/x results better while cancelling the query
@enidgjoleka enidgjoleka force-pushed the handle-tls-connection-during-cancellation branch from 07cd20f to 4ae561e Compare March 26, 2020 11:13
Copy link
Member

@seriyps seriyps left a comment

Choose a reason for hiding this comment

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

Looks fine. Thanks. Let's see if @davidw has some comments.

src/commands/epgsql_cmd_connect.erl Outdated Show resolved Hide resolved
src/epgsql_sock.erl Show resolved Hide resolved
@enidgjoleka
Copy link
Contributor Author

It would be nice if I could get some traction on this PR.

@seriyps seriyps merged commit f0d07e3 into epgsql:devel Apr 27, 2020
@seriyps
Copy link
Member

seriyps commented Apr 27, 2020

Merged! Sorry for the slow response.

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.

None yet

2 participants