-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add query property to AnalyticEntry with privacy masking #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7c926e7
feat: Add query property to AnalyticEntry with privacy masking
c3fc058
refactor: Reduce cyclomatic complexity of query masking algorithm
79a1294
docs: Update README with GraphQL query masking documentation
f3661e7
fix: Address PR review feedback for query masking security
28e3731
refactor: Improve QueryLiteralMasker with init-based design
2010e56
Update README.md
ssestak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
Sources/FTNetworkTracer/Analytics/QueryLiteralMasker.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import Foundation | ||
|
|
||
| /// Masks literal values in GraphQL queries while preserving structure | ||
| struct QueryLiteralMasker { | ||
| /// The masked query with literals replaced by "***" | ||
| let maskedQuery: String | ||
|
|
||
| /// Initializes a masker and processes the query | ||
| /// - Parameter query: The GraphQL query string to mask | ||
| init(query: String) { | ||
| var processor = Processor() | ||
| self.maskedQuery = processor.process(query) | ||
| } | ||
|
|
||
| // MARK: - Internal Processor | ||
|
|
||
| private struct Processor { | ||
| private var result = "" | ||
| private var insideString = false | ||
| private var insideParentheses = false | ||
| private var currentToken = "" | ||
| private var escapeNext = false | ||
|
|
||
| mutating func process(_ query: String) -> String { | ||
| for char in query { | ||
| if escapeNext { | ||
| handleEscapedCharacter(char) | ||
| continue | ||
| } | ||
|
|
||
| if char == "\\" { | ||
| handleBackslash(char) | ||
| continue | ||
| } | ||
|
|
||
| processCharacter(char) | ||
| } | ||
|
|
||
| finalize() | ||
| return result | ||
| } | ||
|
|
||
| // MARK: - Character Handlers | ||
|
|
||
| private mutating func handleEscapedCharacter(_ char: Character) { | ||
| if insideString { | ||
| currentToken.append(char) | ||
| } else { | ||
| result.append(char) | ||
| } | ||
| escapeNext = false | ||
| } | ||
|
|
||
| private mutating func handleBackslash(_ char: Character) { | ||
| escapeNext = true | ||
| if insideString { | ||
| currentToken.append(char) | ||
| } else { | ||
| result.append(char) | ||
| } | ||
| } | ||
|
|
||
| private mutating func processCharacter(_ char: Character) { | ||
| switch char { | ||
| case "\"": | ||
| handleQuote() | ||
| case "(": | ||
| handleOpenParenthesis(char) | ||
| case ")": | ||
| handleCloseParenthesis(char) | ||
| case " ", "\n", "\t", ",", ":": | ||
| handleDelimiter(char) | ||
| default: | ||
| handleDefaultCharacter(char) | ||
| } | ||
| } | ||
|
|
||
| private mutating func handleQuote() { | ||
| if insideParentheses && !insideString { | ||
| insideString = true | ||
| currentToken = "\"" | ||
| } else if insideString { | ||
| insideString = false | ||
| result.append("\"***\"") | ||
| currentToken = "" | ||
| } else { | ||
| result.append("\"") | ||
| } | ||
| } | ||
|
|
||
| private mutating func handleOpenParenthesis(_ char: Character) { | ||
| if insideString { | ||
| // Inside string literal - treat as regular character | ||
| currentToken.append(char) | ||
| } else { | ||
| result.append(currentToken) | ||
| result.append(char) | ||
| currentToken = "" | ||
| insideParentheses = true | ||
| } | ||
| } | ||
|
|
||
| private mutating func handleCloseParenthesis(_ char: Character) { | ||
| if insideString { | ||
| // Inside string literal - treat as regular character | ||
| currentToken.append(char) | ||
| } else { | ||
| flushToken() | ||
| result.append(char) | ||
| currentToken = "" | ||
| insideParentheses = false | ||
| } | ||
| } | ||
|
|
||
| private mutating func handleDelimiter(_ char: Character) { | ||
| if insideString { | ||
| currentToken.append(char) | ||
| } else { | ||
| flushToken() | ||
| result.append(char) | ||
| } | ||
| } | ||
|
|
||
| private mutating func handleDefaultCharacter(_ char: Character) { | ||
| if insideString || insideParentheses { | ||
| currentToken.append(char) | ||
| } else { | ||
| result.append(char) | ||
| } | ||
| } | ||
|
|
||
| private mutating func flushToken() { | ||
| guard insideParentheses && !currentToken.isEmpty else { | ||
| return | ||
| } | ||
|
|
||
| if isNumericLiteral(currentToken) { | ||
| result.append("***") | ||
| } else { | ||
| result.append(currentToken) | ||
| } | ||
| currentToken = "" | ||
| } | ||
|
|
||
| private mutating func finalize() { | ||
| // Handle any remaining token | ||
| if insideString { | ||
| // Unclosed string - mask it for safety | ||
| result.append("\"***\"") | ||
| } else if !currentToken.isEmpty { | ||
| result.append(currentToken) | ||
| } | ||
| } | ||
|
|
||
| private func isNumericLiteral(_ token: String) -> Bool { | ||
| let trimmed = token.trimmingCharacters(in: .whitespaces) | ||
| // Check if it's a number (int or float) but not a variable reference | ||
| guard !trimmed.isEmpty && !trimmed.hasPrefix("$") else { | ||
| return false | ||
| } | ||
| return Double(trimmed) != nil | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.