aboutsummaryrefslogtreecommitdiff
path: root/request
diff options
context:
space:
mode:
authorGravatar Miek Gieben <miek@miek.nl> 2017-08-31 16:24:11 +0200
committerGravatar GitHub <noreply@github.com> 2017-08-31 16:24:11 +0200
commit5eccfa2d1e4b76ac0320dc4123413cb0300a39b9 (patch)
tree365dd3669dbbb33dfdde6950c850ce626054de89 /request
parentc72084187c42a526e4b754741c392a38733ceec2 (diff)
downloadcoredns-5eccfa2d1e4b76ac0320dc4123413cb0300a39b9.tar.gz
coredns-5eccfa2d1e4b76ac0320dc4123413cb0300a39b9.tar.zst
coredns-5eccfa2d1e4b76ac0320dc4123413cb0300a39b9.zip
core: harden request.Request (#1009)
Check for a nil message and if we have a question section. Request is usually called with an external Msg that already saw validation checks, but we may also call it from message we create of our own, that may or may not adhire to this. Just be more robust in this case. This PR reverts a previous commit that was applied to master.
Diffstat (limited to 'request')
-rw-r--r--request/request.go76
-rw-r--r--request/request_test.go29
2 files changed, 98 insertions, 7 deletions
diff --git a/request/request.go b/request/request.go
index 89b4a4cf4..a2428c464 100644
--- a/request/request.go
+++ b/request/request.go
@@ -205,31 +205,93 @@ func (r *Request) Scrub(reply *dns.Msg) (*dns.Msg, Result) {
return reply, ScrubDone
}
-// Type returns the type of the question as a string.
-func (r *Request) Type() string { return dns.Type(r.Req.Question[0].Qtype).String() }
+// Type returns the type of the question as a string. If the request is malformed
+// the empty string is returned.
+func (r *Request) Type() string {
+ if r.Req == nil {
+ return ""
+ }
+ if len(r.Req.Question) == 0 {
+ return ""
+ }
-// QType returns the type of the question as an uint16.
-func (r *Request) QType() uint16 { return r.Req.Question[0].Qtype }
+ return dns.Type(r.Req.Question[0].Qtype).String()
+}
+
+// QType returns the type of the question as an uint16. If the request is malformed
+// 0 is returned.
+func (r *Request) QType() uint16 {
+ if r.Req == nil {
+ return 0
+ }
+ if len(r.Req.Question) == 0 {
+ return 0
+ }
+
+ return r.Req.Question[0].Qtype
+}
// Name returns the name of the question in the request. Note
// this name will always have a closing dot and will be lower cased. After a call Name
// the value will be cached. To clear this caching call Clear.
+// If the request is malformed the root zone is returned.
func (r *Request) Name() string {
if r.name != "" {
return r.name
}
+ if r.Req == nil {
+ r.name = "."
+ return "."
+ }
+ if len(r.Req.Question) == 0 {
+ r.name = "."
+ return "."
+ }
+
r.name = strings.ToLower(dns.Name(r.Req.Question[0].Name).String())
return r.name
}
// QName returns the name of the question in the request.
-func (r *Request) QName() string { return dns.Name(r.Req.Question[0].Name).String() }
+// If the request is malformed the root zone is returned.
+func (r *Request) QName() string {
+ if r.Req == nil {
+ return "."
+ }
+ if len(r.Req.Question) == 0 {
+ return "."
+ }
+
+ return dns.Name(r.Req.Question[0].Name).String()
+}
// Class returns the class of the question in the request.
-func (r *Request) Class() string { return dns.Class(r.Req.Question[0].Qclass).String() }
+// If the request is malformed the empty string is returned.
+func (r *Request) Class() string {
+ if r.Req == nil {
+ return ""
+ }
+ if len(r.Req.Question) == 0 {
+ return ""
+ }
+
+ return dns.Class(r.Req.Question[0].Qclass).String()
+
+}
// QClass returns the class of the question in the request.
-func (r *Request) QClass() uint16 { return r.Req.Question[0].Qclass }
+// If the request is malformed 0 returned.
+func (r *Request) QClass() uint16 {
+ if r.Req == nil {
+ return 0
+ }
+ if len(r.Req.Question) == 0 {
+ return 0
+ }
+
+ return r.Req.Question[0].Qclass
+
+}
// ErrorMessage returns an error message suitable for sending
// back to the client.
diff --git a/request/request_test.go b/request/request_test.go
index 563e6dfe2..bc08ea6ab 100644
--- a/request/request_test.go
+++ b/request/request_test.go
@@ -31,6 +31,35 @@ func TestRequestRemote(t *testing.T) {
}
}
+func TestRequestMalformed(t *testing.T) {
+ m := new(dns.Msg)
+ st := Request{Req: m}
+
+ if x := st.QType(); x != 0 {
+ t.Errorf("Expected 0 Qtype, got %d", x)
+ }
+
+ if x := st.QClass(); x != 0 {
+ t.Errorf("Expected 0 QClass, got %d", x)
+ }
+
+ if x := st.QName(); x != "." {
+ t.Errorf("Expected . Qname, got %s", x)
+ }
+
+ if x := st.Name(); x != "." {
+ t.Errorf("Expected . Name, got %s", x)
+ }
+
+ if x := st.Type(); x != "" {
+ t.Errorf("Expected empty Type, got %s", x)
+ }
+
+ if x := st.Class(); x != "" {
+ t.Errorf("Expected empty Class, got %s", x)
+ }
+}
+
func BenchmarkRequestDo(b *testing.B) {
st := testRequest()
s2 Unnamed repository; edit this file 'description' to name the repository.
aboutsummaryrefslogtreecommitdiff
path: root/examples/framework-react (unfollow)
AgeCommit message (Expand)AuthorFilesLines
2022-09-30Update seven-shrimps-hope.md (#4934)Gravatar Tony Sullivan 1-1/+1
2022-09-30[ci] update lockfile (#4927)Gravatar Fred K. Bot 1-280/+280
2022-09-30Move module declarations for Markdown and MDX so they're available everywhere...Gravatar Erika 4-35/+46
2022-09-29[ci] formatGravatar tony-sull 4-17/+29
2022-09-29[@astrojs/image] adding caching support for SSG builds (#4909)Gravatar Tony Sullivan 8-10/+240
2022-09-29[ci] release (#4903)astro@1.4.0@astrojs/vue@1.1.0@astrojs/vercel@2.1.0@astrojs/telemetry@1.0.1@astrojs/tailwind@2.0.2@astrojs/svelte@1.0.1@astrojs/rss@1.0.2@astrojs/preact@1.1.1@astrojs/node@1.1.0@astrojs/netlify@1.1.0@astrojs/mdx@0.11.3@astrojs/markdown-remark@1.1.3@astrojs/image@0.8.1@astrojs/deno@1.1.0@astrojs/cloudflare@2.1.0Gravatar Fred K. Bot 65-235/+512
2022-09-29[ci] update lockfile (#4899)Gravatar Fred K. Bot 1-735/+718
2022-09-29[ci] formatGravatar matthewp 1-1/+3
2022-09-29fix trailing slash mismatch in dev vs build in docs example (#4912)Gravatar Rishi Raj Jain 1-1/+1
2022-09-29[ci] formatGravatar bluwy 1-1/+1
2022-09-29Support Vue JSX (#4897)Gravatar Bjorn Lu 12-5/+329
2022-09-28[ci] formatGravatar matthewp 1-8/+4
2022-09-28Fix CSS ordering between imported and Astro styles (#4907)Gravatar Matthew Phillips 12-7/+218
2022-09-28[ci] formatGravatar matthewp 23-137/+127
2022-09-28Astro.cookies implementation (#4876)Gravatar Matthew Phillips 32-29/+943
2022-09-28Fix: let Squoosh default image quality internally (#4906)Gravatar Tony Sullivan 5-11/+20
2022-09-28Update README.md (#4898)Gravatar stijlmassi 1-2/+3
2022-09-28Fix test (#4904)Gravatar Bjorn Lu 2-1/+7
2022-09-28[ci] formatGravatar FredKSchott 2-4/+4
2022-09-28redesign basics template (#4879)Gravatar Fred K. Schott 3-88/+34
2022-09-28[ci] formatGravatar bluwy 1-2/+2
2022-09-28Remove shamefully-hoist (#4842)Gravatar Bjorn Lu 104-527/+768
2022-09-28[ci] formatGravatar matthewp 4-14/+16
2022-09-28Hoist hydration script out of slot templates (#4891)Gravatar Matthew Phillips 13-43/+165
2022-09-28Ensure head content rendered once with lazy layouts (#4892)Gravatar Matthew Phillips 9-3/+59
2022-09-27fixed typing (#4893)Gravatar tweenietomatoes 1-1/+1
2022-09-27[ci] release (#4846)create-astro@1.1.0astro@1.3.1@astrojs/webapi@1.1.0@astrojs/vercel@2.0.1@astrojs/mdx@0.11.2@astrojs/image@0.8.0Gravatar Fred K. Bot 60-185/+169
2022-09-27fix: post API routes in SSG should warn or error during dev mode (#4878)Gravatar Rishi Raj Jain 3-2/+17
2022-09-27docs: Fix links to Tailwind examples (#4883)Gravatar Deanmv 1-1/+1
2022-09-27Set SSR target webworker for Vercel edge (#4884)Gravatar Bjorn Lu 2-0/+6
2022-09-27[ci] update lockfile (#4885)Gravatar Fred K. Bot 1-86/+79
2022-09-26[ci] formatGravatar bholmesdev 3-23/+19
2022-09-26Fix: correctly transform `import.meta.env.*` in MDX (#4858)Gravatar Ben Holmes 12-233/+454
2022-09-26Change negative lookbehind to lookahead (#4866)Gravatar Rishi Raj Jain 1-1/+1
2022-09-26add double check on astro file return type to display more human readable err...Gravatar Steven Yung 6-2/+61
2022-09-26[ci] update lockfile (#4862)Gravatar Fred K. Bot 1-81/+81
2022-09-26fix: Script with innerHTML not working on Safari (#4861)Gravatar Rishi Raj Jain 3-3/+10
2022-09-26Prevent /undefined catch-all routes in dev (#4873)Gravatar Bjorn Lu 6-9/+66
2022-09-26fix: 🐛 BUG: class:list directive adding class attribute when undefined (#4...Gravatar Rishi Raj Jain 2-2/+9
2022-09-26docs: Standardize common integration READMEs (#4874)Gravatar Jake Strawn 7-6/+66
2022-09-26docs: Update references to support channel in Discord. (#4872)Gravatar Jake Strawn 12-12/+12
2022-09-26[ci] formatGravatar bluwy 1-1/+1
2022-09-26fix: "chunks" directory appears in build output, if custom modules are import...Gravatar Rishi Raj Jain 2-6/+34
2022-09-23[ci] formatGravatar matthewp 1-1/+1
2022-09-23Define toStringTag another way (#4855)Gravatar Matthew Phillips 2-4/+12
2022-09-23update SSR example to match recent change on Astro API Context (#4854)Gravatar Steven Yung 2-4/+6
2022-09-23[ci] update lockfile (#4852)Gravatar Fred K. Bot 1-373/+402