Browse Source

Stop treating URI responses as UTF-8

Benjamin Sago 3 years ago
parent
commit
d963fd8d90

+ 6 - 8
dns/src/record/uri.rs

@@ -25,7 +25,7 @@ pub struct URI {
 
     /// The URI contained in the record. Since all we are doing is displaying
     /// it to the user, we do not need to parse it for accuracy.
-    pub target: String,
+    pub target: Vec<u8>,
 }
 
 impl Wire for URI {
@@ -47,11 +47,9 @@ impl Wire for URI {
         }
 
         let remaining_length = stated_length - 4;
-        let mut target_buffer = vec![0_u8; usize::from(remaining_length)];
-        c.read_exact(&mut target_buffer)?;
-
-        let target = String::from_utf8_lossy(&target_buffer).to_string();
-        trace!("Parsed target -> {:?}", target);
+        let mut target = vec![0_u8; usize::from(remaining_length)];
+        c.read_exact(&mut target)?;
+        trace!("Parsed target -> {:?}", String::from_utf8_lossy(&target));
 
         Ok(Self { priority, weight, target })
     }
@@ -76,7 +74,7 @@ mod test {
                    URI {
                        priority: 10,
                        weight: 16,
-                       target: String::from("https://rfcs.io/"),
+                       target: b"https://rfcs.io/".to_vec(),
                    });
     }
 
@@ -92,7 +90,7 @@ mod test {
                    URI {
                        priority: 10,
                        weight: 16,
-                       target: String::from("/"),
+                       target: b"/".to_vec(),
                    });
     }
 

+ 2 - 2
src/output.rs

@@ -280,7 +280,7 @@ impl TextFormat {
                 messages.join(", ")
             }
             Record::URI(uri) => {
-                format!("{} {} {:?}", uri.priority, uri.weight, uri.target)
+                format!("{} {} {}", uri.priority, uri.weight, Ascii(&uri.target))
             }
             Record::Other { bytes, .. } => {
                 format!("{:?}", bytes)
@@ -575,7 +575,7 @@ fn json_record_data(record: Record) -> JsonValue {
             object! {
                 "priority": uri.priority,
                 "weight": uri.weight,
-                "target": uri.target,
+                "target": String::from_utf8_lossy(&uri.target).to_string(),
             }
         }
         Record::Other { bytes, .. } => {

+ 1 - 0
xtests/madns/outputs/bad-utf8.uri.example.ansitxt

@@ -0,0 +1 @@
+URI bad-utf8.uri.example. 10m00s   10 16 "\208\208\160\255"

+ 28 - 0
xtests/madns/outputs/bad-utf8.uri.example.json

@@ -0,0 +1,28 @@
+{
+  "responses": [
+    {
+      "queries": [
+        {
+          "name": "bad-utf8.uri.example.",
+          "class": "IN",
+          "type": "URI"
+        }
+      ],
+      "answers": [
+        {
+          "name": "bad-utf8.uri.example.",
+          "class": "IN",
+          "ttl": 600,
+          "type": "URI",
+          "data": {
+            "priority": 10,
+            "weight": 16,
+            "target": "�Р�"
+          }
+        }
+      ],
+      "authorities": [],
+      "additionals": []
+    }
+  ]
+}

+ 1 - 0
xtests/madns/outputs/utf8.uri.example.ansitxt

@@ -0,0 +1 @@
+URI utf8.uri.example. 10m00s   10 16 "https://\240\159\146\169.la/"

+ 28 - 0
xtests/madns/outputs/utf8.uri.example.json

@@ -0,0 +1,28 @@
+{
+  "responses": [
+    {
+      "queries": [
+        {
+          "name": "utf8.uri.example.",
+          "class": "IN",
+          "type": "URI"
+        }
+      ],
+      "answers": [
+        {
+          "name": "utf8.uri.example.",
+          "class": "IN",
+          "ttl": 600,
+          "type": "URI",
+          "data": {
+            "priority": 10,
+            "weight": 16,
+            "target": "https://💩.la/"
+          }
+        }
+      ],
+      "authorities": [],
+      "additionals": []
+    }
+  ]
+}

+ 32 - 0
xtests/madns/uri-records.toml

@@ -16,6 +16,22 @@ stderr = { empty = true }
 status = 0
 tags = [ "uri", "madns" ]
 
+[[cmd]]
+name = "Running with ‘utf8.uri.example’ escapes characters in the URI"
+shell = "dog --colour=always ${MADNS_ARGS:[email protected]:5301 --tcp} URI utf8.uri.example"
+stdout = { file = "outputs/utf8.uri.example.ansitxt" }
+stderr = { empty = true }
+status = 0
+tags = [ "uri", "madns", "chars" ]
+
+[[cmd]]
+name = "Running with ‘bad-utf8.uri.example’ escapes characters in the URI and does not crash"
+shell = "dog --colour=always ${MADNS_ARGS:[email protected]:5301 --tcp} URI bad-utf8.uri.example"
+stdout = { file = "outputs/bad-utf8.uri.example.ansitxt" }
+stderr = { empty = true }
+status = 0
+tags = [ "uri", "madns", "chars" ]
+
 
 # URI record successes (JSON)
 
@@ -27,6 +43,22 @@ stderr = { empty = true }
 status = 0
 tags = [ "uri", "madns", "json" ]
 
+[[cmd]]
+name = "Running with ‘utf8.uri.example --json’ interprets the response as UTF-8"
+shell = "dog --colour=always ${MADNS_ARGS:[email protected]:5301 --tcp} URI utf8.uri.example --json | jq"
+stdout = { file = "outputs/utf8.uri.example.json" }
+stderr = { empty = true }
+status = 0
+tags = [ "uri", "madns", "chars", "json" ]
+
+[[cmd]]
+name = "Running with ‘bad-utf8.uri.example --json’ uses UTF-8 replacement characters"
+shell = "dog --colour=always ${MADNS_ARGS:[email protected]:5301 --tcp} URI bad-utf8.uri.example --json | jq"
+stdout = { file = "outputs/bad-utf8.uri.example.json" }
+stderr = { empty = true }
+status = 0
+tags = [ "uri", "madns", "chars", "json" ]
+
 
 # URI record invalid packets