-
Notifications
You must be signed in to change notification settings - Fork 103
Create question pipeline filter #257
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 all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,32 @@ | ||
| -module(erldns_questions). | ||
| -moduledoc """ | ||
| Remove all redundant questions from a DNS message, | ||
| and parses the first question into a list of labels. | ||
|
|
||
| ## Telemetry events | ||
|
|
||
| - `[erldns, pipeline, questions]` with `#{count => non_neg_integer()}` | ||
| where `count` is the number of questions removed. | ||
| """. | ||
|
|
||
| -include_lib("dns_erlang/include/dns.hrl"). | ||
|
|
||
| -behaviour(erldns_pipeline). | ||
|
|
||
| -export([call/2]). | ||
|
|
||
| -doc "`c:erldns_pipeline:call/2` callback.". | ||
| -spec call(dns:message(), erldns_pipeline:opts()) -> erldns_pipeline:return(). | ||
| call(#dns_message{qc = 0} = Msg, _) -> | ||
| {stop, Msg#dns_message{qr = true}}; | ||
| call(#dns_message{qc = 1, questions = [#dns_query{name = Name, type = Type}]} = Msg, Opts) -> | ||
| Labels = dns:dname_to_lower_labels(Name), | ||
| {Msg, Opts#{query_labels := Labels, query_type := Type}}; | ||
| call(#dns_message{questions = [Q1 | Rest]} = Msg, #{host := Host} = Opts) -> | ||
| Labels = dns:dname_to_lower_labels(Q1#dns_query.name), | ||
| Measurements = #{count => length(Rest)}, | ||
| Metadata = #{host => Host, questions => [Q1 | Rest]}, | ||
| telemetry:execute([erldns, pipeline, questions], Measurements, Metadata), | ||
| Msg1 = Msg#dns_message{qc = 1, questions = [Q1]}, | ||
| Opts1 = Opts#{query_labels := Labels, query_type := Q1#dns_query.type}, | ||
| {Msg1, Opts1}. | ||
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,69 @@ | ||
| -module(questions_SUITE). | ||
| -compile([export_all, nowarn_export_all]). | ||
|
|
||
| -behaviour(ct_suite). | ||
|
|
||
| -include_lib("stdlib/include/assert.hrl"). | ||
| -include_lib("dns_erlang/include/dns.hrl"). | ||
|
|
||
| -spec all() -> [ct_suite:ct_test_def()]. | ||
| all() -> | ||
| [{group, all}]. | ||
|
|
||
| -spec groups() -> [ct_suite:ct_group_def()]. | ||
| groups() -> | ||
| [{all, [parallel], [empty, one_question, many_questions]}]. | ||
|
|
||
| -spec init_per_suite(ct_suite:ct_config()) -> ct_suite:ct_config(). | ||
| init_per_suite(Config) -> | ||
| application:ensure_all_started([telemetry]), | ||
| Events = [ | ||
| [erldns, pipeline, questions] | ||
| ], | ||
| ok = telemetry:attach_many(?MODULE, Events, fun ?MODULE:telemetry_handler/4, []), | ||
| Config. | ||
|
|
||
| -spec end_per_suite(ct_suite:ct_config()) -> term(). | ||
| end_per_suite(_) -> | ||
| application:stop(telemetry). | ||
|
|
||
| %% Tests | ||
| empty(_) -> | ||
| Msg = #dns_message{}, | ||
| ?assertMatch({stop, #dns_message{}}, erldns_questions:call(Msg, def_opts())), | ||
| assert_no_telemetry_event(). | ||
|
|
||
| one_question(_) -> | ||
| Q = #dns_query{name = ~"example.com", type = ?DNS_TYPE_ANY}, | ||
| Msg = #dns_message{qc = 1, questions = [Q]}, | ||
| ?assertMatch({Msg, _}, erldns_questions:call(Msg, def_opts())), | ||
| assert_no_telemetry_event(). | ||
|
|
||
| many_questions(_) -> | ||
| Q = #dns_query{name = ~"example.com", type = ?DNS_TYPE_ANY}, | ||
| Msg = #dns_message{qc = 2, questions = [Q, Q]}, | ||
| ?assertMatch({#dns_message{qc = 1}, _}, erldns_questions:call(Msg, def_opts())), | ||
| assert_telemetry_event(). | ||
|
|
||
| def_opts() -> | ||
| erldns_pipeline:def_opts(). | ||
|
|
||
| telemetry_handler(EventName, _, _, _) -> | ||
| ct:pal("EventName ~p~n", [EventName]), | ||
| self() ! EventName. | ||
|
|
||
| assert_telemetry_event() -> | ||
| receive | ||
| [erldns, pipeline, questions] -> | ||
| ok | ||
| after 1000 -> | ||
| ct:fail("Telemetry event not triggered: questions") | ||
| end. | ||
|
|
||
| assert_no_telemetry_event() -> | ||
| receive | ||
| [erldns, pipeline, questions] -> | ||
| ct:fail("Telemetry event not triggered: questions") | ||
| after 100 -> | ||
| ok | ||
| end. |
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.
Multi-question messages are not of great interest to us, since in practice we only support a single question, and any additional questions can be ignored safely.
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.
I know, the idea is that an attacking client can wilfully submit messages with many many questions, which no real DNS server really supports so very likely this is either a buggy client or a carefully crafted packet for evil purposes, which we will waste resources parsing, and we never drop them, the answer will contain them so again we waste resources encoding them. We might just drop them early enough and if it is of any interest for DDoS ideas this can be analysed.