Skip to content

Commit 712b8df

Browse files
hongj-srcmeta-codesync[bot]
authored andcommitted
Python: provide breakdown to differentiate net new and modified new appendix
Summary: Add breakdown to differentiate net new (0->1) event and modified event. - Add new appendix type constants: APPENDIX_GENERAL_NEW (0x01), APPENDIX_NET_NEW (0x02), APPENDIX_MODIFIED_NEW (0x03), APPENDIX_NO_CHANGE (0x00) - Change _get_appendix() API from boolean to appendix type constant - Update all usages to use appropriate appendix type: - APPENDIX_NET_NEW: Brand new cookies/PII (0->1) - APPENDIX_MODIFIED_NEW: Updated existing cookies with new payload - APPENDIX_NO_CHANGE: Existing values without modification TODO: cleanup the APPENDIX_GENERAL_NEW flag, since no longer in use Differential Revision: D91706612 fbshipit-source-id: 3b78f1a87c0a838a732398f6305b9de390253cca
1 parent 3d54c3e commit 712b8df

2 files changed

Lines changed: 131 additions & 48 deletions

File tree

python/capi_param_builder/capi_param_builder/param_builder.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
APPENDIX_LENGTH_V1: Final[int] = 2
3232
APPENDIX_LENGTH_V2: Final[int] = 8
3333

34+
# Appendix type constants
35+
APPENDIX_NO_CHANGE: Final[int] = 0x00
36+
APPENDIX_GENERAL_NEW: Final[int] = 0x01
37+
APPENDIX_NET_NEW: Final[int] = 0x02
38+
APPENDIX_MODIFIED_NEW: Final[int] = 0x03
39+
3440

3541
class ParamBuilder:
3642
"""
@@ -54,8 +60,9 @@ def __init__(self, input: Union[EtldPlusOneResolver, List, None] = None) -> None
5460
self.domain_list: Optional[List] = None
5561
self.etld_plus_one_resolver: Optional[EtldPlusOneResolver] = None
5662
## Appendix with version number
57-
self.appendix_new: str = self._get_appendix(True)
58-
self.appendix_normal: str = self._get_appendix(False)
63+
self.appendix_net_new: str = self._get_appendix(APPENDIX_NET_NEW)
64+
self.appendix_modified_new: str = self._get_appendix(APPENDIX_MODIFIED_NEW)
65+
self.appendix_no_change: str = self._get_appendix(APPENDIX_NO_CHANGE)
5966

6067
if isinstance(input, List):
6168
self.domain_list = []
@@ -77,20 +84,29 @@ def _get_version(self) -> str:
7784
# Fallback version on any error
7885
return "1.0.0"
7986

80-
def _get_appendix(self, is_new: bool) -> str:
87+
def _get_appendix(self, appendix_type: int) -> str:
8188
try:
8289
version = self._get_version()
8390
version_parts = version.split(".")
8491
major = int(version_parts[0])
8592
minor = int(version_parts[1])
8693
patch = int(version_parts[2])
8794

88-
is_new_byte = 0x01 if is_new else 0x00
95+
# Validate appendix type
96+
valid_types = [
97+
APPENDIX_NET_NEW,
98+
APPENDIX_GENERAL_NEW,
99+
APPENDIX_MODIFIED_NEW,
100+
]
101+
if appendix_type in valid_types:
102+
type_byte = appendix_type
103+
else:
104+
type_byte = APPENDIX_NO_CHANGE
89105

90106
bytes_array = [
91107
DEFAULT_FORMAT,
92108
LANGUAGE_TOKEN_INDEX,
93-
is_new_byte,
109+
type_byte,
94110
major,
95111
minor,
96112
patch,
@@ -134,7 +150,7 @@ def _pre_process_cookies(
134150
return None
135151

136152
if len(cookie_split) == MIN_PAYLOAD_SPLIT_LENGTH:
137-
updated_cookie = cookie_value + "." + self.appendix_normal
153+
updated_cookie = cookie_value + "." + self.appendix_no_change
138154
self.cookies_to_set_dict[cookie_name] = CookieSettings(
139155
cookie_name, updated_cookie, self.etld_plus_one, DEFAULT_1PC_AGE
140156
)
@@ -251,17 +267,24 @@ def _get_updated_fbc_cookie(
251267

252268
# cookie update
253269
cookie_update = False
270+
is_net_new = False
254271
if existing_fbc is None:
255272
cookie_update = True
273+
is_net_new = True
256274
else:
257275
parts = existing_fbc.split(".")
258-
cookie_update = new_fbc_payload != parts[3]
276+
if len(parts) < MIN_PAYLOAD_SPLIT_LENGTH:
277+
cookie_update = True # corrupt fbc, overwrite
278+
is_net_new = True
279+
else:
280+
cookie_update = new_fbc_payload != parts[3]
259281

260282
if cookie_update is False:
261283
return None
262284

263285
# Get ms
264286
now_ts = int(time.time() * 1000)
287+
appendix = self.appendix_net_new if is_net_new else self.appendix_modified_new
265288
new_fbc = (
266289
"fb."
267290
+ str(self.sub_domain_index)
@@ -270,7 +293,7 @@ def _get_updated_fbc_cookie(
270293
+ "."
271294
+ new_fbc_payload
272295
+ "."
273-
+ self.appendix_new
296+
+ appendix
274297
)
275298
# TODO: update etld+1 to get proper etld+1.
276299
udpated_cookie_setting = CookieSettings(
@@ -297,7 +320,7 @@ def _get_updated_fbp_cookie(
297320
+ "."
298321
+ new_fbp_payload
299322
+ "."
300-
+ self.appendix_new
323+
+ self.appendix_net_new
301324
)
302325
udpated_cookie_setting = CookieSettings(
303326
FBP_COOKIE_NAME, new_fbp, self.etld_plus_one, DEFAULT_1PC_AGE

0 commit comments

Comments
 (0)