From db07c5cc347a5bc52cf50f5c516726844d3b69b6 Mon Sep 17 00:00:00 2001 From: Dominik Pich Date: Wed, 1 May 2024 05:42:20 -0700 Subject: [PATCH] open source the cookie monitoring plugin Summary: This diffs adds an ios plugin to monitor the app's NSCookie storage Differential Revision: D56712794 --- desktop/plugins/public/cookies/index.tsx | 38 ++++++++++ desktop/plugins/public/cookies/package.json | 28 ++++++++ .../Exported/FlipperKitCookiesPlugin.h | 18 +++++ .../Exported/FlipperKitCookiesPlugin.mm | 71 +++++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 desktop/plugins/public/cookies/index.tsx create mode 100644 desktop/plugins/public/cookies/package.json create mode 100644 iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.h create mode 100644 iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm diff --git a/desktop/plugins/public/cookies/index.tsx b/desktop/plugins/public/cookies/index.tsx new file mode 100644 index 00000000000..050decfab41 --- /dev/null +++ b/desktop/plugins/public/cookies/index.tsx @@ -0,0 +1,38 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @format + */ + +import {DataTableColumn, createTablePlugin} from 'flipper-plugin'; + +type Row = { + id: number; + Name: string; + Expires: string; + Value: string; +}; + +const columns: DataTableColumn[] = [ + { + key: 'Name', + width: 250, + }, + { + key: 'Expires', + width: 250, + }, + { + key: 'Value', + }, +]; + +module.exports = createTablePlugin({ + columns, + key: 'id', + method: 'addCookie', + resetMethod: 'resetCookies', +}); diff --git a/desktop/plugins/public/cookies/package.json b/desktop/plugins/public/cookies/package.json new file mode 100644 index 00000000000..954b0320471 --- /dev/null +++ b/desktop/plugins/public/cookies/package.json @@ -0,0 +1,28 @@ +{ + "$schema": "https://fbflipper.com/schemas/plugin-package/v2.json", + "name": "flipper-plugin-cookies", + "id": "cookies", + "pluginType": "client", + "version": "0.0.0", + "flipperBundlerEntry": "index.tsx", + "main": "dist/bundle.js", + "license": "MIT", + "title": "Cookies", + "icon": "apps", + "keywords": [ + "flipper-plugin", + "cookies" + ], + "description": "Flipper plugin to monitor NSHTTPCookieStorage", + "peerDependencies": { + "flipper-plugin": "*", + "antd": "*", + "react": "*", + "react-dom": "*", + "@emotion/styled": "*", + "@ant-design/icons": "*", + "@types/react": "*", + "@types/react-dom": "*", + "@types/node": "*" + } +} diff --git a/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.h b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.h new file mode 100644 index 00000000000..46d9634bfdd --- /dev/null +++ b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#ifdef FB_SONARKIT_ENABLED + +#import + +#import + +@interface FlipperKitCookiesPlugin : NSObject + +@end + +#endif diff --git a/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm new file mode 100644 index 00000000000..86da7671cc8 --- /dev/null +++ b/iOS/Plugins/FlipperKitCookiesPlugin/FlipperKitCookiesPlugin/Exported/FlipperKitCookiesPlugin.mm @@ -0,0 +1,71 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#ifdef FB_SONARKIT_ENABLED + +#import "FlipperKitCookiesPlugin.h" +#import +#import +#import +#import "Plugins.h" + +@implementation FlipperKitCookiesPlugin { + id _connection; +} + +- (NSString*)identifier { + return @"cookies"; +} + +- (void)didConnect:(id)connection { + _connection = connection; + [self _sendCookies]; + + [[NSNotificationCenter defaultCenter] + addObserver:self + selector:@selector(onCookieStorageChange:) + name:NSHTTPCookieManagerCookiesChangedNotification + object:nil]; +} + +- (void)didDisconnect { + [[NSNotificationCenter defaultCenter] removeObserver:self]; + _connection = nil; +} + +#pragma mark - cookie storage observer + +- (void)onCookieStorageChange:(NSNotification*)notification { + [self _sendCookies]; +} + +#pragma mark - helper + +- (void)_sendCookies { + [_connection send:@"resetCookies" withParams:@{}]; + + NSInteger i = 1; + NSArray* _Nullable cookies = + [[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] copy]; + for (NSHTTPCookie* cookie in cookies) { + NSMutableDictionary* dict = [NSMutableDictionary dictionary]; + dict[@"id"] = @(i); + dict[@"Name"] = cookie.name; + dict[@"Expires"] = cookie.expiresDate.description; + dict[@"Value"] = cookie.value; + [_connection send:@"addCookie" withParams:dict]; + i++; + } +} + +@end + +void FlipperKitCookiesPluginInit(FlipperClient* client) { + [client addPlugin:[FlipperKitCookiesPlugin new]]; +} + +#endif