Skip to content
This repository has been archived by the owner on Nov 6, 2021. It is now read-only.

Commit

Permalink
add an implementation of HTTPS Everywhere
Browse files Browse the repository at this point in the history
A ruby script converts all of the XML rule files into hashes and
dumps them to plist files, which are parsed by the app at startup.

When intercepting URLs, check for matching rules by hostname (using
the same wildcarding as the Javascript in the Firefox extension) and
then rewrite them.  Supports exclusions as well.

Seems to be pretty fast, and has a cache size of (currently) 20
rules to avoid having to recompile all those regexes and stuff on
every request.
  • Loading branch information
jcs committed Dec 8, 2014
1 parent 9e560dc commit 7456109
Show file tree
Hide file tree
Showing 23 changed files with 484,533 additions and 60 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "https-everywhere"]
path = https-everywhere
url = https://github.com/EFForg/https-everywhere
4 changes: 4 additions & 0 deletions Podfile
Expand Up @@ -2,3 +2,7 @@ platform :ios, "7.0"

pod "NJKWebViewProgress"
pod "InAppSettingsKit"

target "endless Tests", :exclusive => true do
pod "OCMock"
end
3 changes: 3 additions & 0 deletions Podfile.lock
Expand Up @@ -5,13 +5,16 @@ PODS:
- NJKWebViewProgress/ProgressView (= 0.2.3)
- NJKWebViewProgress/Core (0.2.3)
- NJKWebViewProgress/ProgressView (0.2.3)
- OCMock (3.1.1)

DEPENDENCIES:
- InAppSettingsKit
- NJKWebViewProgress
- OCMock

SPEC CHECKSUMS:
InAppSettingsKit: c4eb758abf9d71a3b7847a80ceebf02ce4299113
NJKWebViewProgress: 721b57080c840c76f70ff6c2dda4f836ee1b27c1
OCMock: f6cb8c162ab9d5620dddf411282c7b2c0ee78854

COCOAPODS: 0.35.0
70 changes: 70 additions & 0 deletions endless Tests/HTTPSEverywhere_Tests.m
@@ -0,0 +1,70 @@
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>

#import "HTTPSEverywhere.h"

@interface HTTPSEverywhere_Tests : XCTestCase
@end

@implementation HTTPSEverywhere_Tests

id HEMocked;

- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.

HEMocked = OCMClassMock([HTTPSEverywhere class]);

NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"mock_rules" ofType:@"plist"];
if (![fm fileExistsAtPath:path])
abort();

OCMStub([HEMocked rules]).andReturn([NSDictionary dictionaryWithContentsOfFile:path]);

NSString *tpath = [[NSBundle bundleForClass:[self class]] pathForResource:@"mock_targets" ofType:@"plist"];
if (![fm fileExistsAtPath:tpath])
abort();

OCMStub([HEMocked targets]).andReturn([NSDictionary dictionaryWithContentsOfFile:tpath]);
}

- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testMock {
XCTAssertNotNil([[HTTPSEverywhere rules] objectForKey:@"Reddit"]);
XCTAssertNotNil([[HTTPSEverywhere rules] objectForKey:@"Better Business Bureau (partial)"]);
// make sure the big list didn't get loaded
XCTAssertNil([[HTTPSEverywhere rules] objectForKey:@"EFF"]);
}

- (void)testApplicableRules {
NSArray *results = [HTTPSEverywhere potentiallyApplicableRulesFor:@"www.reddit.com"];
XCTAssertEqual([results count], 1U);
XCTAssert([[results objectAtIndex:0] isKindOfClass:[HTTPSEverywhereRule class]]);
XCTAssert([[(HTTPSEverywhereRule *)[results objectAtIndex:0] name] isEqualToString:@"Reddit"]);
}

- (void)testRewrittenURI {
NSURL *rewritten = [HTTPSEverywhere rewrittenURI:[NSURL URLWithString:@"http://www.reddit.com/test"]];
XCTAssert([[rewritten absoluteString] isEqualToString:@"https://www.reddit.com/test"]);

/* a more complex rewrite */
rewritten = [HTTPSEverywhere rewrittenURI:[NSURL URLWithString:@"http://bbbonline.org/cks.asp?id=1234"]];
XCTAssert([[rewritten absoluteString] isEqualToString:@"https://www.bbb.org/us/bbb-online-business/?id=1234"]);
}

- (void)testRewrittenURIWithExclusion {
NSString *input = @"http://www.dc.bbb.org/";
NSURL *rewritten = [HTTPSEverywhere rewrittenURI:[NSURL URLWithString:input]];
XCTAssert([[rewritten absoluteString] isEqualToString:input]);

}


@end
24 changes: 24 additions & 0 deletions endless Tests/Info.plist
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>org.jcs.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
211 changes: 211 additions & 0 deletions endless Tests/mock_rules.plist
@@ -0,0 +1,211 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Better Business Bureau (partial)</key>
<dict>
<key>ruleset</key>
<dict>
<key>exclusion</key>
<array>
<dict>
<key>pattern</key>
<string>^http://(?:www\.)?dc\.</string>
</dict>
<dict>
<key>pattern</key>
<string>http://(?:www\.)?seflorida\.</string>
</dict>
</array>
<key>name</key>
<string>Better Business Bureau (partial)</string>
<key>platform</key>
<string>mixedcontent</string>
<key>rule</key>
<array>
<dict>
<key>from</key>
<string>^https?://(?:www\.)?la\.?bbb\.org/</string>
<key>to</key>
<string>https://www.labbb.org/</string>
</dict>
<dict>
<key>from</key>
<string>^http://bbb\.org/</string>
<key>to</key>
<string>https://www.bbb.org/</string>
</dict>
<dict>
<key>from</key>
<string>^http://(?:www\.)?([\w\-]+)\.bbb\.org/</string>
<key>to</key>
<string>https://$1.bbb.org/</string>
</dict>
<dict>
<key>from</key>
<string>^http://hurdman\.app\.bbb\.org/</string>
<key>to</key>
<string>https://hurdman.app.bbb.org/</string>
</dict>
<dict>
<key>from</key>
<string>^https?://(?:www\.)?bbbonline\.org/cks\.asp\?id=(\d+)</string>
<key>to</key>
<string>https://www.bbb.org/us/bbb-online-business/?id=$1</string>
</dict>
<dict>
<key>from</key>
<string>^http://(www\.)?bbbsilicon\.org/</string>
<key>to</key>
<string>https://$1bbbsilicon.org/</string>
</dict>
<dict>
<key>from</key>
<string>^https?://(?:www\.)?sanjose\.bbb\.org/</string>
<key>to</key>
<string>https://bbbsilicon.org/</string>
</dict>
</array>
<key>securecookie</key>
<array>
<dict>
<key>host</key>
<string>^.*\.bbb\.org$</string>
<key>name</key>
<string>.*</string>
</dict>
<dict>
<key>host</key>
<string>^(.*\.)?bbbsilicon\.org$</string>
<key>name</key>
<string>.*</string>
</dict>
<dict>
<key>host</key>
<string>^www\.labbb\.org$</string>
<key>name</key>
<string>.*</string>
</dict>
</array>
<key>target</key>
<array>
<dict>
<key>host</key>
<string>bbb.org</string>
</dict>
<dict>
<key>host</key>
<string>hurdman.app.bbb.org</string>
</dict>
<dict>
<key>host</key>
<string>*.bbb.org</string>
</dict>
<dict>
<key>host</key>
<string>www.*.bbb.org</string>
</dict>
<dict>
<key>host</key>
<string>bbbonline.org</string>
</dict>
<dict>
<key>host</key>
<string>www.bbbonline.org</string>
</dict>
<dict>
<key>host</key>
<string>bbbsilicon.org</string>
</dict>
<dict>
<key>host</key>
<string>www.bbbsilicon.org</string>
</dict>
<dict>
<key>host</key>
<string>labbb.org</string>
</dict>
<dict>
<key>host</key>
<string>www.labbb.org</string>
</dict>
</array>
</dict>
</dict>
<key>Reddit</key>
<dict>
<key>ruleset</key>
<dict>
<key>name</key>
<string>Reddit</string>
<key>rule</key>
<array>
<dict>
<key>from</key>
<string>^http://((?:[a-z]{1,2}|[a-z]{2}-[a-z]{2}|blog|pay|redditama|ssl|static|www)\.)?reddit\.com/</string>
<key>to</key>
<string>https://$1reddit.com/</string>
</dict>
<dict>
<key>from</key>
<string>^http://(?:www\.)?np\.reddit\.com/</string>
<key>to</key>
<string>https://np.reddit.com/</string>
</dict>
<dict>
<key>from</key>
<string>^http://thumbs\.reddit\.com/</string>
<key>to</key>
<string>https://s3.amazonaws.com/thumbs.reddit.com/</string>
</dict>
<dict>
<key>from</key>
<string>^http://(\w+\.)?thumbs\.redditmedia\.com/</string>
<key>to</key>
<string>https://$1thumbs.redditmedia.com/</string>
</dict>
<dict>
<key>from</key>
<string>^http://www\.redditstatic\.com/</string>
<key>to</key>
<string>https://www.redditstatic.com/</string>
</dict>
</array>
<key>securecookie</key>
<array>
<dict>
<key>host</key>
<string>.*\.reddit\.com$</string>
<key>name</key>
<string>.+</string>
</dict>
<dict>
<key>host</key>
<string>^\.redditmedia\.com$</string>
<key>name</key>
<string>^__cfduid$</string>
</dict>
</array>
<key>target</key>
<array>
<dict>
<key>host</key>
<string>reddit.com</string>
</dict>
<dict>
<key>host</key>
<string>*.reddit.com</string>
</dict>
<dict>
<key>host</key>
<string>*.redditmedia.com</string>
</dict>
<dict>
<key>host</key>
<string>www.redditstatic.com</string>
</dict>
</array>
</dict>
</dict>
</dict>
</plist>
34 changes: 34 additions & 0 deletions endless Tests/mock_targets.plist
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>*.bbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>*.reddit.com</key>
<string>Reddit</string>
<key>*.redditmedia.com</key>
<string>Reddit</string>
<key>bbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>bbbonline.org</key>
<string>Better Business Bureau (partial)</string>
<key>bbbsilicon.org</key>
<string>Better Business Bureau (partial)</string>
<key>hurdman.app.bbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>labbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>reddit.com</key>
<string>Reddit</string>
<key>www.*.bbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>www.bbbonline.org</key>
<string>Better Business Bureau (partial)</string>
<key>www.bbbsilicon.org</key>
<string>Better Business Bureau (partial)</string>
<key>www.labbb.org</key>
<string>Better Business Bureau (partial)</string>
<key>www.redditstatic.com</key>
<string>Reddit</string>
</dict>
</plist>

0 comments on commit 7456109

Please sign in to comment.