Adding the Proxy to Selenium Scripts
To use this method of capturing traffic from the web browser, you must add Fortify code that applies the proxy to your Selenium initialization directly in your code or, if applicable, passed as an argument in the command line interface (CLI).
Advantages
This approach provides flexibility, as it can run from any browser that Selenium supports. Additionally, this approach should provide some upgrade protection. The Fortify code resides in your scripts, so you should be able to continue using it in future versions of Selenium with only minor code changes.
Disadvantages
This approach involves a one-time manual task of adding Fortify code to your scripts for initializing the browser correctly.
Sample Code
You must get the value from the environmental variable named Fortify_WI_Proxy, and then store it as an HTTP and HTTPS proxy for the web browser and trust certificate. How you do this depends on your programming language. The following sections provide sample code for several languages.
Note: These code samples are based on Selenium WebDriver version 3.14. Code for your specific version might be different.
C#
In your C# code, you must find where the browser driver is initialized and add browser options to it. The following is an example for the Chrome browser.
ChromeOptions chromeOptions = new ChromeOptions();
string proxy = Environment.GetEnvironmentVariable("Fortify_WI_Proxy");
if (!String.IsNullOrEmpty(proxy))
{
chromeOptions.AcceptInsecureCertificates = true;
chromeOptions.Proxy = new Proxy();
chromeOptions.Proxy.HttpProxy = proxy;
chromeOptions.Proxy.SslProxy = proxy;
}
…. new ChromeDriver(chromeOptions) // options should go into this class
The following is an example for the Firefox browser.
FirefoxOptions config = new FirefoxOptions();
string proxy = Environment.GetEnvironmentVariable("Fortify_WI_Proxy");
if (!String.IsNullOrEmpty(proxy))
{
config.AcceptInsecureCertificates = true;
config.Proxy = new Proxy();
config.Proxy.HttpProxy = proxy;
config.Proxy.SslProxy = proxy;
}
… new FirefoxDriver(config))
Java
In your Java code, you must find where the browser driver is initialized and add browser options to it. The following is an example for the Chrome browser.
ChromeOptions options = new ChromeOptions();
String wi_proxy = System.getenv("Fortify_WI_Proxy");
if (wi_proxy != null) {
Proxy proxy = new Proxy();
proxy.setHttpProxy(wi_proxy);
proxy.setSslProxy(wi_proxy);
options.setProxy(proxy);
options.setAcceptInsecureCerts(true);
}
ChromeDriver driver=new ChromeDriver(options);
The following is an example for the Firefox browser.
FirefoxOptions options = new FirefoxOptions();
String wi_proxy = System.getenv("Fortify_WI_Proxy");
if (wi_proxy != null) {
Proxy proxy = new Proxy();
proxy.setHttpProxy(wi_proxy);
proxy.setSslProxy(wi_proxy);
options.setProxy(proxy);
options.setAcceptInsecureCerts(true);
}
FirefoxDriver driver=new FirefoxDriver(options);
JavaScript
In your JavaScript code, you must find where the browser driver is initialized and add browser options to it. The following is an example for the Chrome browser.
const selProxy = require('selenium-webdriver/proxy');
……
(async function example() {
let env = process.env.Fortify_WI_Proxy;
if (env) {
let caps = { acceptInsecureCerts: true }; //allow to accept all
certificates
let proxy = { http: env, https: env }; // apply env variable as
proxy
driver = await new Builder().withCapabilities(caps).setProxy
(selProxy.manual(proxy)).forBrowser('chrome').build(); // set proxy and
acceptInsecureCerts
}else
driver = await new Builder().forBrowser('chrome').build();
The following is an example for the Firefox browser.
const selProxy = require('selenium-webdriver/proxy');
……
let env = process.env.Fortify_WI_Proxy;
if (env) {
let caps = { acceptInsecureCerts: true }; //allow to accept all
certificates
let proxy = { http: env, https: env }; // apply env variable as
proxy
driver = await new Builder().withCapabilities(caps).setProxy
(selProxy.manual(proxy)).forBrowser('firefox').build(); // set proxy and
acceptInsecureCerts
}else
driver = await new Builder().forBrowser('firefox').build();
Python
In your Python code, you must find where the browser driver is initialized and add browser options to it. The following is an example for the Chrome browser.
capabilities1 = DesiredCapabilities.CHROME.copy()
Fortify = os.environ.get('Fortify_WI_Proxy')
if Fortify is not None:
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = Fortify
prox.ssl_proxy = Fortify
prox.add_to_capabilities(capabilities1)
cls.driver = webdriver.Chrome(executable_path='C:/chromedriver.exe',
desired_capabilities=capabilities1)
The following is an example for the Firefox browser.
import os
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
……
capabilities1 = DesiredCapabilities.FIREFOX.copy()
Fortify = os.environ.get('Fortify_WI_Proxy')
if Fortify is not None:
capabilities1['acceptInsecureCerts'] = True
prox = Proxy()
prox.proxy_type = ProxyType.MANUAL
prox.http_proxy = Fortify
prox.ssl_proxy = Fortify
prox.add_to_capabilities(capabilities1)
cls.driver = webdriver.Firefox(executable_path='C:/geckodriver.exe', capabilities=capabilities1)
Ruby
In your Ruby code, you must find where the browser driver is initialized and add browser options to it. The following is an example for the Chrome browser.
http_proxy = ENV['Fortify_WI_Proxy']
if http_proxy
proxy = Selenium::WebDriver::Proxy.new(http: http_proxy,
ssl: http_proxy)
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome
(accept_insecure_certs: true)
capabilities.proxy = proxy;
else
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome()
end
driver = Selenium::WebDriver.for :chrome, desired_capabilities:
capabilities
The following is an example for the Firefox browser.
http_proxy = ENV['Fortify_WI_Proxy']
if http_proxy
proxy = Selenium::WebDriver::Proxy.new(http: http_proxy,
ssl: http_proxy)
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox
(accept_insecure_certs: true)
capabilities.proxy = proxy;
else
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox()
end
driver = Selenium::WebDriver.for :firefox, desired_capabilities:
capabilities
Using the CLI
If your scripts accept an argument that configures the proxy, then you can use this method to add the Fortify WebInspect proxy to your scripts. For example, if you have an argument named -proxy "<host:port>", then you can use the placeholder {Fortify_WI_Proxy} in the command at run time as shown here:
-proxy "{Fortify_WI_Proxy}"
If you must specify the host and port separately, then you can use a placeholder for each as shown here:
-proxy "{Fortify_WI_Proxy_Host}:{Fortify_WI_Proxy_Port}"
These arguments will replace the placeholder in your scripts with the Fortify WebInspect proxy at run time.