[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[vidalia-svn] r2018: Validate that a bridge the user enters is acceptably formatt (in trunk: . src/gui/config)



Author: edmanm
Date: 2007-10-12 22:58:59 -0400 (Fri, 12 Oct 2007)
New Revision: 2018

Modified:
   trunk/
   trunk/src/gui/config/networkpage.cpp
   trunk/src/gui/config/networkpage.h
Log:
 r2097@lysithea:  edmanm | 2007-10-12 22:58:33 -0400
 Validate that a bridge the user enters is acceptably formatted.



Property changes on: trunk
___________________________________________________________________
 svk:merge ticket from /local/vidalia/trunk [r2097] on dc66be73-d13e-47ba-a267-8dc7cda68c65

Modified: trunk/src/gui/config/networkpage.cpp
===================================================================
--- trunk/src/gui/config/networkpage.cpp	2007-10-13 02:58:50 UTC (rev 2017)
+++ trunk/src/gui/config/networkpage.cpp	2007-10-13 02:58:59 UTC (rev 2018)
@@ -28,8 +28,11 @@
 #include <QMenu>
 #include <QIntValidator>
 #include <QClipboard>
+#include <QHostAddress>
 #include <networksettings.h>
+#include <vmessagebox.h>
 #include <vidalia.h>
+#include <util/string.h>
 
 #include "networkpage.h"
 #include "domainvalidator.h"
@@ -86,18 +89,71 @@
   NetworkSettings(Vidalia::torControl()).revert();
 }
 
+/** Verifies that <b>bridge</b> is a valid bridge identifier and places a 
+ * normalized identifier in <b>out</b>. The normalized identifier will have
+ * all spaces removed from the fingerprint portion (if any) and all
+ * hexadecimal characters converted to uppercase. Returns true if
+ * <b>bridge</b> is a valid bridge indentifier, false otherwise. */
+bool
+NetworkPage::validateBridge(const QString &bridge, QString *out)
+{
+  QString temp = bridge;
+  if (temp.startsWith("bridge ", Qt::CaseInsensitive))
+    temp = temp.remove(0, 7); /* remove "bridge " */
+
+  QStringList parts = temp.split(" ", QString::SkipEmptyParts);
+  if (parts.isEmpty())
+    return false;
+
+  QString s = parts.at(0);
+  if (s.contains(":")) {
+    if (s.endsWith(":"))
+      return false;
+
+    int index = s.indexOf(":");
+    QString host = s.mid(0, index);
+    QString port = s.mid(index + 1);
+    if (QHostAddress(host).isNull()
+          || QHostAddress(host).protocol() != QAbstractSocket::IPv4Protocol
+          || port.toUInt() < 1 
+          || port.toUInt() > 65535)
+      return false;
+    temp = s;
+    if (parts.size() > 1) {
+      QString fp = static_cast<QStringList>(parts.mid(1)).join("");
+      if (fp.length() != 40 || !string_is_hex(fp))
+        return false;
+      temp += " " + fp.toUpper();
+    }
+  } else {
+    QString fp = parts.join("");
+    if (fp.length() != 40 || !string_is_hex(fp))
+      return false;
+    temp = fp.toUpper();
+  }
+  *out = temp;
+  return true;
+}
+
 /** Adds a bridge to the bridge list box. */
 void
 NetworkPage::addBridge()
 {
-  QString bridge = ui.lineBridge->text().trimmed();
-  if (bridge.isEmpty())
+  QString bridge;
+  QString input = ui.lineBridge->text().trimmed();
+
+  if (input.isEmpty())
     return;
+  if (!validateBridge(input, &bridge)) {
+    VMessageBox::warning(this,
+                  tr("Invalid Bridge"),
+                  tr("The specified bridge identifier is not valid."),
+                  VMessageBox::Ok|VMessageBox::Default);
+    return;
+  }
   if (!ui.listBridges->findItems(bridge, Qt::MatchFixedString).isEmpty())
     return; /* duplicate bridge */
 
-  /* XXX: We should do some verification that the gibberish the user typed in
-   * is actually valid. */
   ui.listBridges->addItem(bridge);
   ui.lineBridge->clear();
 }

Modified: trunk/src/gui/config/networkpage.h
===================================================================
--- trunk/src/gui/config/networkpage.h	2007-10-13 02:58:50 UTC (rev 2017)
+++ trunk/src/gui/config/networkpage.h	2007-10-13 02:58:59 UTC (rev 2018)
@@ -73,6 +73,13 @@
   void bridgeSelectionChanged();
 
 private:
+  /** Verifies that <b>bridge</b> is a valid bridge identifier and places a 
+   * normalized identifier in <b>out</b>. The normalized identifier will have
+   * all spaces removed from the fingerprint portion (if any) and all
+   * hexadecimal characters converted to uppercase. Returns true if
+   * <b>bridge</b> is a valid bridge indentifier, false otherwise. */
+  bool validateBridge(const QString &bridge, QString *out);
+
   /** Qt Designer generated object */
   Ui::NetworkPage ui;
 };