4
0
Эх сурвалжийг харах

Add check when using --https without a nameserver

Here, we add an explicit check when the user passes the --https argument without a nameserver. What currently happens is that dog tries to use the system default resolver, which won't be a URL when it should be.

This was raised in GH-5. The panic is still there, and it's still simple to trigger, so it's not 100% fixed yet, but at least it's now harder to trigger _accidentally_.
Benjamin Sago 4 жил өмнө
parent
commit
506bf49401
2 өөрчлөгдсөн 25 нэмэгдсэн , 0 устгасан
  1. 18 0
      src/options.rs
  2. 7 0
      xtests/errors.toml

+ 18 - 0
src/options.rs

@@ -132,6 +132,7 @@ impl Inputs {
         inputs.load_transport_types(&matches);
         inputs.load_named_args(&matches)?;
         inputs.load_free_args(matches)?;
+        inputs.check_for_missing_nameserver()?;
         inputs.load_fallbacks();
         Ok(inputs)
     }
@@ -261,6 +262,15 @@ impl Inputs {
             self.transport_types.push(TransportType::Automatic);
         }
     }
+
+    fn check_for_missing_nameserver(&self) -> Result<(), OptionsError> {
+        if self.resolvers.is_empty() && self.transport_types == [TransportType::HTTPS] {
+            Err(OptionsError::MissingHttpsUrl)
+        }
+        else {
+            Ok(())
+        }
+    }
 }
 
 
@@ -446,6 +456,7 @@ pub enum OptionsError {
     InvalidTxid(String),
     InvalidTweak(String),
     QueryTypeOPT,
+    MissingHttpsUrl,
 }
 
 impl fmt::Display for OptionsError {
@@ -459,6 +470,7 @@ impl fmt::Display for OptionsError {
             Self::InvalidTxid(txid)      => write!(f, "Invalid transaction ID {:?}", txid),
             Self::InvalidTweak(tweak)    => write!(f, "Invalid protocol tweak {:?}", tweak),
             Self::QueryTypeOPT           => write!(f, "OPT request is sent by default (see -Z flag)"),
+            Self::MissingHttpsUrl        => write!(f, "You must pass a URL as a nameserver when using --https"),
         }
     }
 }
@@ -784,6 +796,12 @@ mod test {
                    OptionsResult::InvalidOptions(OptionsError::QueryTypeOPT));
     }
 
+    #[test]
+    fn missing_https_url() {
+        assert_eq!(Options::getopts(&[ "--https", "lookup.dog" ]),
+                   OptionsResult::InvalidOptions(OptionsError::MissingHttpsUrl));
+    }
+
     // txid tests
 
     #[test]

+ 7 - 0
xtests/errors.toml

@@ -25,3 +25,10 @@ shell = "dog 1234567890123456789012345678901234567890123456789012345678901234567
 stdout = { empty = true }
 stderr = { string = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" }
 status = 3
+
+[[cmd]]
+name = "Running dog with ‘--https’ and no nameserver warns that one is missing"
+shell = "dog --https dns.google"
+stdout = { empty = true }
+stderr = { string = "You must pass a URL as a nameserver when using --https" }
+status = 3