Kaynağa Gözat

FIX: prosody token util handles race on timeout gracefully

master
Aaron van Meerten 5 yıl önce
ebeveyn
işleme
5e35b69fc9
1 değiştirilmiş dosya ile 22 ekleme ve 15 silme
  1. 22
    15
      resources/prosody-plugins/token/util.lib.lua

+ 22
- 15
resources/prosody-plugins/token/util.lib.lua Dosyayı Görüntüle

127
         -- If the key is not found in the cache.
127
         -- If the key is not found in the cache.
128
         module:log("debug", "Cache miss for key: "..keyId);
128
         module:log("debug", "Cache miss for key: "..keyId);
129
         local code;
129
         local code;
130
+        local timeout_occurred;
130
         local wait, done = async.waiter();
131
         local wait, done = async.waiter();
131
         local function cb(content_, code_, response_, request_)
132
         local function cb(content_, code_, response_, request_)
132
-            content, code = content_, code_;
133
-            if code == 200 or code == 204 then
134
-                cache:set(keyId, content);
133
+            if timeout_occurred == nil then
134
+                content, code = content_, code_;
135
+                if code == 200 or code == 204 then
136
+                    cache:set(keyId, content);
137
+                end
138
+                done();
139
+            else
140
+                module:log("warn", "public key reply delivered after timeout from: %s",keyurl);
135
             end
141
             end
136
-            done();
137
         end
142
         end
138
-        local keyurl = path.join(self.asapKeyServer, hex.to(sha256(keyId))..'.pem');
139
-        module:log("debug", "Fetching public key from: "..keyurl);
140
-
141
-        -- We hash the key ID to work around some legacy behavior and make
142
-        -- deployment easier. It also helps prevent directory
143
-        -- traversal attacks (although path cleaning could have done this too).
144
-        local request = http.request(keyurl, {
145
-            headers = http_headers or {},
146
-            method = "GET"
147
-        }, cb);
148
-
149
         -- TODO: Is the done() call racey? Can we cancel this if the request
143
         -- TODO: Is the done() call racey? Can we cancel this if the request
150
         --       succeedes?
144
         --       succeedes?
151
         local function cancel()
145
         local function cancel()
152
             -- TODO: This check is racey. Not likely to be a problem, but we should
146
             -- TODO: This check is racey. Not likely to be a problem, but we should
153
             --       still stick a mutex on content / code at some point.
147
             --       still stick a mutex on content / code at some point.
154
             if code == nil then
148
             if code == nil then
149
+                timeout_occurred = true;
155
                 module:log("warn", "Timeout %s seconds fetching public key from: %s",http_timeout,keyurl);
150
                 module:log("warn", "Timeout %s seconds fetching public key from: %s",http_timeout,keyurl);
156
                 if http.destroy_request then
151
                 if http.destroy_request then
157
                     http.destroy_request(request);
152
                     http.destroy_request(request);
159
                 done();
154
                 done();
160
             end
155
             end
161
         end
156
         end
157
+
158
+        local keyurl = path.join(self.asapKeyServer, hex.to(sha256(keyId))..'.pem');
159
+        module:log("debug", "Fetching public key from: "..keyurl);
160
+
161
+        -- We hash the key ID to work around some legacy behavior and make
162
+        -- deployment easier. It also helps prevent directory
163
+        -- traversal attacks (although path cleaning could have done this too).
164
+        local request = http.request(keyurl, {
165
+            headers = http_headers or {},
166
+            method = "GET"
167
+        }, cb);
168
+
162
         timer.add_task(http_timeout, cancel);
169
         timer.add_task(http_timeout, cancel);
163
         wait();
170
         wait();
164
 
171
 

Loading…
İptal
Kaydet