india_pii_guard 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +136 -0
- data/lib/india_pii_guard/configuration.rb +42 -0
- data/lib/india_pii_guard/detectors.rb +125 -0
- data/lib/india_pii_guard/digits.rb +24 -0
- data/lib/india_pii_guard/gstin_check.rb +31 -0
- data/lib/india_pii_guard/guard.rb +62 -0
- data/lib/india_pii_guard/vault.rb +41 -0
- data/lib/india_pii_guard/verhoeff.rb +56 -0
- data/lib/india_pii_guard/version.rb +7 -0
- data/lib/india_pii_guard.rb +29 -0
- metadata +57 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8a3b8f9182fb87a03237b9a09e93ce363789212491cb70afc40598c392f1e042
|
|
4
|
+
data.tar.gz: 000ea374f0086dee0149ce53b38b04552cfd8453fbcef07b6ecf5d9fd12a4e4c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 589232d193bdf9aa6d01e3d30b03ab36a274d1a1a614a50c77af2e8af4d78af4fc18ffb1c872828380d607373b7be53b3afca8cd3017f6429d78c87854fecd54
|
|
7
|
+
data.tar.gz: fcf30c2f09e007de26ef5ff184d454d0551a5768742d47d5fbf5e0eb846c91a726bf63267b57dbef5092ee7b059823f740bcbcae34b37cee1d60af414dbb59fe
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bhushan Naidu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
<!-- Purpose: First stop for users: what the library does, how to install it and how to use it. -->
|
|
2
|
+
|
|
3
|
+
# india-pii-guard
|
|
4
|
+
|
|
5
|
+
[](https://github.com/bhushan-naidu/india-pii-guard/actions/workflows/ci.yml)
|
|
6
|
+
|
|
7
|
+
**Mask Indian PII before text reaches an LLM, then put the real values back in the reply.**
|
|
8
|
+
|
|
9
|
+
For Ruby and TypeScript/JavaScript. No dependencies, no network calls: the real values never leave your process.
|
|
10
|
+
|
|
11
|
+
```text
|
|
12
|
+
You: "Customer Ravi, PAN ABCPE1234F, Aadhaar 9876 5432 1012"
|
|
13
|
+
LLM sees: "Customer Ravi, PAN [PAN_1], Aadhaar [AADHAAR_1]"
|
|
14
|
+
LLM says: "Verified [PAN_1] against [AADHAAR_1]."
|
|
15
|
+
You get: "Verified ABCPE1234F against 9876 5432 1012."
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Why
|
|
19
|
+
|
|
20
|
+
Teams in lending, insurance, payments and healthcare want to use LLMs on customer messages, documents, support tickets and logs. Those are full of PAN and Aadhaar numbers. Sending them to a model provider is often against policy or regulation, and generic PII tools are built for US data (SSNs, US phone numbers) and miss Indian identifiers.
|
|
21
|
+
|
|
22
|
+
## What it detects
|
|
23
|
+
|
|
24
|
+
| Type | Example | How false positives are avoided | Status |
|
|
25
|
+
|---|---|---|---|
|
|
26
|
+
| PAN | `ABCPE1234F` | 4th letter must be a valid holder type; must not be part of a longer word (digits next to it are fine) | ✅ v0.1 |
|
|
27
|
+
| Aadhaar | `2345 6789 0124` | First digit 2–9, Verhoeff checksum must pass, consistent separators (spaces of any kind, hyphens or dashes) | ✅ v0.1 |
|
|
28
|
+
| Indian mobile | `+91 98765 43210` | 10 digits starting 6–9, not part of a longer number; +91, 0091 or 0 prefix. No checksum exists, so any such number is masked | ✅ v0.2 |
|
|
29
|
+
| IFSC | `HDFC0001234` | 4 letters, a 0, then 6 letters or digits; not part of a longer word | ✅ v0.2 |
|
|
30
|
+
| GSTIN | `27ABCPE1234F1ZB` | Check character must be valid; if it isn't, the PAN inside is still masked | ✅ v0.2 |
|
|
31
|
+
| Email | `name@example.com` | Domain must have a dot and a 2+ letter ending | ✅ v0.2 |
|
|
32
|
+
| Bank account | `123456789012` | | 🗓️ planned |
|
|
33
|
+
| Your own patterns | policy, proposal or loan numbers | | 🗓️ planned |
|
|
34
|
+
|
|
35
|
+
Numbers written in Indian-script digits (Devanagari, Bengali, Gurmukhi, Gujarati, Odia, Tamil, Telugu, Kannada, Malayalam) are detected too, and restored exactly as written.
|
|
36
|
+
|
|
37
|
+
All numbers in this repo are synthetic.
|
|
38
|
+
|
|
39
|
+
## Install
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
gem install india_pii_guard # Ruby >= 3.2
|
|
43
|
+
npm install india-pii-guard # Node >= 20
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or in a Gemfile: `gem "india_pii_guard"`.
|
|
47
|
+
|
|
48
|
+
## Try it
|
|
49
|
+
|
|
50
|
+
- **[Playground](https://bhushan-naidu.github.io/india-pii-guard/):** paste text in your browser and see exactly what an LLM would receive. Nothing leaves the page.
|
|
51
|
+
- **[Examples](examples/):** a Ruby support-ticket summariser, a Node chat, and a check that runs realistic samples through a real LLM. They work with any LLM you configure (Claude, Groq or xAI Grok) or offline.
|
|
52
|
+
|
|
53
|
+
## Usage
|
|
54
|
+
|
|
55
|
+
### Ruby
|
|
56
|
+
|
|
57
|
+
```ruby
|
|
58
|
+
require "india_pii_guard"
|
|
59
|
+
|
|
60
|
+
result = IndiaPiiGuard.mask("PAN ABCPE1234F, Aadhaar 2345 6789 0124")
|
|
61
|
+
result.text # => "PAN [PAN_1], Aadhaar [AADHAAR_1]"
|
|
62
|
+
result.counts # => {"PAN"=>1, "AADHAAR"=>1}
|
|
63
|
+
|
|
64
|
+
reply = llm.chat(result.text) # the LLM only sees placeholders
|
|
65
|
+
IndiaPiiGuard.unmask(reply, result.vault) # real values back
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### TypeScript / JavaScript
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { mask, unmask } from "india-pii-guard";
|
|
72
|
+
|
|
73
|
+
const result = mask("PAN ABCPE1234F, Aadhaar 2345 6789 0124");
|
|
74
|
+
result.text; // "PAN [PAN_1], Aadhaar [AADHAAR_1]"
|
|
75
|
+
|
|
76
|
+
const reply = await llm.chat(result.text);
|
|
77
|
+
unmask(reply, result.vault);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Conversations
|
|
81
|
+
|
|
82
|
+
Reuse one vault for every message, so the same value keeps the same placeholder and the model can reason about it across turns:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
vault = IndiaPiiGuard::Vault.new
|
|
86
|
+
IndiaPiiGuard.mask("PAN ABCPE1234F", vault: vault).text # "PAN [PAN_1]"
|
|
87
|
+
IndiaPiiGuard.mask("Is ABCPE1234F linked?", vault: vault).text # "Is [PAN_1] linked?"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
const vault = new Vault();
|
|
92
|
+
mask("PAN ABCPE1234F", { vault });
|
|
93
|
+
mask("Is ABCPE1234F linked?", { vault }); // "Is [PAN_1] linked?"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Choose what to mask
|
|
97
|
+
|
|
98
|
+
Every detector runs by default. Pick by name with `only` or `except`:
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
IndiaPiiGuard.mask(text, only: [:pan, :aadhaar]) # just these
|
|
102
|
+
IndiaPiiGuard.mask(text, except: [:gstin]) # everything but these
|
|
103
|
+
IndiaPiiGuard::Detectors.names # => [:aadhaar, :gstin, :pan, :phone, :email, :ifsc]
|
|
104
|
+
|
|
105
|
+
# App-wide default, for example in config/initializers/india_pii_guard.rb
|
|
106
|
+
IndiaPiiGuard.configure { |c| c.except = [:gstin] }
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { mask, createGuard, DETECTOR_NAMES } from "india-pii-guard";
|
|
111
|
+
|
|
112
|
+
mask(text, { only: ["pan", "aadhaar"] });
|
|
113
|
+
const guard = createGuard({ except: ["gstin"] }); // checked once, reuse everywhere
|
|
114
|
+
guard.mask(text);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A misspelt name raises an error instead of being skipped, so a typo can never silently turn masking off. Turning GSTIN off while PAN is on still masks the PAN inside a GSTIN.
|
|
118
|
+
|
|
119
|
+
## Design
|
|
120
|
+
|
|
121
|
+
- **Precision through validation.** A regex alone flags order IDs and amounts. Each detector also checks structure (PAN holder type, Aadhaar checksum) before masking.
|
|
122
|
+
- **Same value, same placeholder.** `abcpe1234f` and `ABCPE1234F` both become `[PAN_1]`.
|
|
123
|
+
- **One behaviour, two languages.** Both packages run the same test cases from [`fixtures/cases.json`](fixtures/cases.json). A change that passes in one language and fails in the other cannot be merged.
|
|
124
|
+
- **Nothing leaves your process.** No network, no telemetry, no dependencies.
|
|
125
|
+
|
|
126
|
+
## Limits
|
|
127
|
+
|
|
128
|
+
This reduces exposure; it is not a compliance guarantee. It finds identifiers by pattern, so it will not catch PII written in words ("my PAN starts with ABC…") and it does not detect names or addresses yet. Review your own data and obligations.
|
|
129
|
+
|
|
130
|
+
## Contributing
|
|
131
|
+
|
|
132
|
+
New detectors, test cases and bug reports are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md); issues labelled `good first issue` are a good place to start. Found real data slipping through? Please report it privately (see [SECURITY.md](SECURITY.md)).
|
|
133
|
+
|
|
134
|
+
## License
|
|
135
|
+
|
|
136
|
+
[MIT](LICENSE)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: App-wide defaults for which detectors run (IndiaPiiGuard.configure).
|
|
4
|
+
|
|
5
|
+
module IndiaPiiGuard
|
|
6
|
+
# App-wide defaults for which detectors run, for example in a Rails initializer:
|
|
7
|
+
#
|
|
8
|
+
# IndiaPiiGuard.configure { |c| c.except = [:gstin] }
|
|
9
|
+
#
|
|
10
|
+
# only: / except: passed to a single mask call replace these defaults for that call.
|
|
11
|
+
class Configuration
|
|
12
|
+
attr_reader :only, :except
|
|
13
|
+
|
|
14
|
+
# Names are checked when they are set, so a typo fails at boot, not silently at runtime.
|
|
15
|
+
def only=(names)
|
|
16
|
+
Detectors.choose(only: names, except: except)
|
|
17
|
+
@only = names
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def except=(names)
|
|
21
|
+
Detectors.choose(only: only, except: names)
|
|
22
|
+
@except = names
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def detectors
|
|
26
|
+
Detectors.choose(only: only, except: except)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.configuration
|
|
31
|
+
@configuration ||= Configuration.new
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def self.configure
|
|
35
|
+
yield configuration
|
|
36
|
+
configuration
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def self.reset_configuration!
|
|
40
|
+
@configuration = Configuration.new
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: Detector definitions (PAN, Aadhaar, GSTIN): the pattern for each identifier and the
|
|
4
|
+
# checks that reject look-alikes.
|
|
5
|
+
|
|
6
|
+
module IndiaPiiGuard
|
|
7
|
+
# One piece of PII found in the text. start/finish are character offsets.
|
|
8
|
+
Match = Struct.new(:type, :value, :key, :start, :finish, keyword_init: true)
|
|
9
|
+
|
|
10
|
+
# A detector = a regex that finds candidates + an optional validator that rejects false positives.
|
|
11
|
+
# normalize turns different spellings of the same value into one key,
|
|
12
|
+
# so "abcpe1234f" and "ABCPE1234F" get the same placeholder.
|
|
13
|
+
Detector = Struct.new(:type, :pattern, :validate, :normalize, keyword_init: true) do
|
|
14
|
+
def scan(text)
|
|
15
|
+
found = []
|
|
16
|
+
text.scan(pattern) do
|
|
17
|
+
m = Regexp.last_match
|
|
18
|
+
value = m[0]
|
|
19
|
+
next if validate && !validate.call(value)
|
|
20
|
+
|
|
21
|
+
key = normalize ? normalize.call(value) : value
|
|
22
|
+
found << Match.new(type: type, value: value, key: key, start: m.begin(0), finish: m.end(0))
|
|
23
|
+
end
|
|
24
|
+
found
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
module Detectors
|
|
29
|
+
# PAN: AAAPA9999A. The 4th letter is the holder type
|
|
30
|
+
# (P person, C company, H HUF, F firm, A AOP, T trust, B BOI, L local authority, J juridical, G government).
|
|
31
|
+
# Only letters next to it disqualify a match. Digits do not: a PAN inside a GSTIN
|
|
32
|
+
# ("27ABCPE1234F1Z5") or glued to a phone number in a bad export must still be masked.
|
|
33
|
+
PAN = Detector.new(
|
|
34
|
+
type: "PAN",
|
|
35
|
+
pattern: /(?<![A-Za-z])[A-Z]{3}[PCHFATBLJG][A-Z]\d{4}[A-Z](?![A-Za-z])/i,
|
|
36
|
+
normalize: ->(v) { v.upcase }
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# Aadhaar: 12 digits, first digit 2-9, last digit is a Verhoeff checksum.
|
|
40
|
+
# Accepts "234567890123", "2345 6789 0123" and "2345-6789-0123".
|
|
41
|
+
# Separators: one or two spaces of any kind that text copied from PDFs, Word and web pages
|
|
42
|
+
# produces (normal, non-breaking U+00A0, thin U+2009, narrow no-break U+202F), or one hyphen
|
|
43
|
+
# or dash (U+2010 to U+2013, which Word's autocorrect creates). Both gaps must use the same separator.
|
|
44
|
+
AADHAAR_SEPARATOR = "((?:[ \u00A0\u2009\u202F]{1,2}|[-\u2010-\u2013])?)"
|
|
45
|
+
AADHAAR = Detector.new(
|
|
46
|
+
type: "AADHAAR",
|
|
47
|
+
pattern: /(?<!\d)[2-9]\d{3}#{AADHAAR_SEPARATOR}\d{4}\1\d{4}(?!\d)/,
|
|
48
|
+
validate: ->(v) { Verhoeff.valid?(v.delete("^0-9")) },
|
|
49
|
+
normalize: ->(v) { v.delete("^0-9") }
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# GSTIN: 2-digit state code + the holder's PAN + entity number + "Z" + check character,
|
|
53
|
+
# for example 27ABCPE1234F1ZB. Masked as a whole when the check character is valid.
|
|
54
|
+
# If it is not, the PAN detector still masks the PAN inside it.
|
|
55
|
+
GSTIN = Detector.new(
|
|
56
|
+
type: "GSTIN",
|
|
57
|
+
pattern: /(?<![A-Za-z0-9])\d{2}[A-Z]{5}\d{4}[A-Z][0-9A-Z]Z[0-9A-Z](?![A-Za-z0-9])/i,
|
|
58
|
+
validate: ->(v) { GstinCheck.valid?(v.upcase) },
|
|
59
|
+
normalize: ->(v) { v.upcase }
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# Indian mobile: 10 digits starting 6-9, written as 9876543210, 98765 43210 or 987 654 3210,
|
|
63
|
+
# optionally after +91, 0091, "91 " / "91-" or a leading 0. There is no checksum, so any
|
|
64
|
+
# such 10-digit number is masked; leave it out with except: [:phone] if that is too broad.
|
|
65
|
+
# A bare "91" with no separator is not accepted, because "91" + 10 digits is also 12 digits
|
|
66
|
+
# and could be an Aadhaar number.
|
|
67
|
+
PHONE = Detector.new(
|
|
68
|
+
type: "PHONE",
|
|
69
|
+
pattern: /(?<![\d+])(?:(?:\+91|0091|91)[ -]?(?=[6-9])|0(?=[6-9]))?(?<!\d91)[6-9](?:\d{9}|\d{4}[ -]\d{5}|\d{2}[ -]\d{3}[ -]\d{4})(?!\d)/,
|
|
70
|
+
normalize: ->(v) { v.delete("^0-9")[-10..] }
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# Email: local part, @, a domain with at least one dot and a 2+ letter ending.
|
|
74
|
+
EMAIL = Detector.new(
|
|
75
|
+
type: "EMAIL",
|
|
76
|
+
pattern: /(?<![A-Za-z0-9._%+-])[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,}(?![A-Za-z0-9-])/,
|
|
77
|
+
normalize: ->(v) { v.downcase }
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# IFSC: 4-letter bank code, a 0, then a 6-character branch code, for example HDFC0001234.
|
|
81
|
+
# Not personal on its own, but next to an account number it points to one bank branch.
|
|
82
|
+
IFSC = Detector.new(
|
|
83
|
+
type: "IFSC",
|
|
84
|
+
pattern: /(?<![A-Za-z0-9])[A-Z]{4}0[A-Z0-9]{6}(?![A-Za-z0-9])/i,
|
|
85
|
+
normalize: ->(v) { v.upcase }
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
ALL = [AADHAAR, GSTIN, PAN, PHONE, EMAIL, IFSC].freeze
|
|
89
|
+
BY_NAME = ALL.to_h { |d| [d.type.downcase.to_sym, d] }.freeze
|
|
90
|
+
|
|
91
|
+
# The names users pass to only: / except:, for example [:aadhaar, :gstin, :pan, :phone, :email, :ifsc].
|
|
92
|
+
def self.names
|
|
93
|
+
BY_NAME.keys
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Pick detectors by name. Names can be symbols or strings in any case (:pan, "PAN").
|
|
97
|
+
# Every mistake raises, because a silently skipped detector means unmasked PII.
|
|
98
|
+
def self.choose(only: nil, except: nil)
|
|
99
|
+
raise ArgumentError, "pass only: or except:, not both" if only && except
|
|
100
|
+
|
|
101
|
+
if only
|
|
102
|
+
wanted = resolve(only)
|
|
103
|
+
raise ArgumentError, "only: must name at least one detector (available: #{names.join(", ")})" if wanted.empty?
|
|
104
|
+
|
|
105
|
+
ALL.select { |d| wanted.include?(d) }
|
|
106
|
+
elsif except
|
|
107
|
+
left = ALL - resolve(except)
|
|
108
|
+
raise ArgumentError, "except: leaves no detector, so nothing would be masked" if left.empty?
|
|
109
|
+
|
|
110
|
+
left
|
|
111
|
+
else
|
|
112
|
+
ALL
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def self.resolve(list)
|
|
117
|
+
Array(list).map do |name|
|
|
118
|
+
BY_NAME.fetch(name.to_s.strip.downcase.to_sym) do
|
|
119
|
+
raise ArgumentError, "unknown detector #{name.inspect} (available: #{names.join(", ")})"
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
private_class_method :resolve
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: Reads the digits of nine Indian scripts as 0-9, so detectors find numbers written in
|
|
4
|
+
# them.
|
|
5
|
+
|
|
6
|
+
module IndiaPiiGuard
|
|
7
|
+
# Decimal digits of Indian scripts, read as 0-9 so detectors find numbers written in them.
|
|
8
|
+
# Every script below has its digits 0-9 in one contiguous block starting at the code point listed.
|
|
9
|
+
module Digits
|
|
10
|
+
ZEROS = {
|
|
11
|
+
devanagari: 0x0966, bengali: 0x09E6, gurmukhi: 0x0A66, gujarati: 0x0AE6, odia: 0x0B66,
|
|
12
|
+
tamil: 0x0BE6, telugu: 0x0C66, kannada: 0x0CE6, malayalam: 0x0D66
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
FROM = ZEROS.values.map { |z| "#{z.chr(Encoding::UTF_8)}-#{(z + 9).chr(Encoding::UTF_8)}" }.join.freeze
|
|
16
|
+
TO = ("0-9" * ZEROS.size).freeze
|
|
17
|
+
|
|
18
|
+
# One character in, one character out, so match positions in the result
|
|
19
|
+
# are valid positions in the original text.
|
|
20
|
+
def self.to_ascii(text)
|
|
21
|
+
text.tr(FROM, TO)
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: GSTIN check character (15th character): calculation and validation.
|
|
4
|
+
|
|
5
|
+
module IndiaPiiGuard
|
|
6
|
+
# The GSTIN check character (15th character): a Luhn-style checksum in base 36.
|
|
7
|
+
module GstinCheck
|
|
8
|
+
CHARS = [*"0".."9", *"A".."Z"].join.freeze
|
|
9
|
+
FORMAT = /\A[0-9A-Z]{15}\z/
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
def valid?(gstin)
|
|
14
|
+
return false unless gstin.is_a?(String) && FORMAT.match?(gstin)
|
|
15
|
+
|
|
16
|
+
check_char(gstin[0, 14]) == gstin[14]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def check_char(first14)
|
|
20
|
+
unless first14.is_a?(String) && /\A[0-9A-Z]{14}\z/.match?(first14)
|
|
21
|
+
raise ArgumentError, "expected 14 characters 0-9 or A-Z, got #{first14.inspect}"
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
sum = first14.each_char.with_index.sum do |ch, i|
|
|
25
|
+
product = CHARS.index(ch) * (i.even? ? 1 : 2)
|
|
26
|
+
(product / 36) + (product % 36)
|
|
27
|
+
end
|
|
28
|
+
CHARS[(36 - (sum % 36)) % 36]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: The masking pipeline: scan, validate, resolve overlaps, assign placeholders and build the
|
|
4
|
+
# masked text.
|
|
5
|
+
|
|
6
|
+
module IndiaPiiGuard
|
|
7
|
+
Result = Struct.new(:text, :vault, :counts, keyword_init: true)
|
|
8
|
+
|
|
9
|
+
class Guard
|
|
10
|
+
# only: / except: choose detectors by name, for example only: [:pan, :aadhaar].
|
|
11
|
+
# detectors: takes Detector objects, for custom detectors.
|
|
12
|
+
# With none of them, the IndiaPiiGuard.configure defaults apply (every detector, unless configured).
|
|
13
|
+
def initialize(detectors: nil, only: nil, except: nil)
|
|
14
|
+
raise ArgumentError, "pass detectors: or only:/except:, not both" if detectors && (only || except)
|
|
15
|
+
|
|
16
|
+
@detectors =
|
|
17
|
+
if detectors
|
|
18
|
+
detectors
|
|
19
|
+
elsif only || except
|
|
20
|
+
Detectors.choose(only: only, except: except)
|
|
21
|
+
else
|
|
22
|
+
IndiaPiiGuard.configuration.detectors
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
attr_reader :detectors
|
|
27
|
+
|
|
28
|
+
# Replace PII with placeholders like [PAN_1].
|
|
29
|
+
# Pass the same vault for every message in a conversation.
|
|
30
|
+
def mask(text, vault: Vault.new)
|
|
31
|
+
# Detectors read a copy with Indian-script digits as 0-9 (same length, same positions).
|
|
32
|
+
# The original text is what gets replaced and what the vault stores.
|
|
33
|
+
scan_text = Digits.to_ascii(text)
|
|
34
|
+
matches = pick_non_overlapping(@detectors.flat_map { |d| d.scan(scan_text) })
|
|
35
|
+
matches.each { |m| m.value = text[m.start...m.finish] }
|
|
36
|
+
|
|
37
|
+
tokens = matches.map { |m| vault.token_for(m) } # assigned in reading order
|
|
38
|
+
masked = text.dup
|
|
39
|
+
matches.zip(tokens).reverse_each { |m, token| masked[m.start...m.finish] = token }
|
|
40
|
+
|
|
41
|
+
Result.new(text: masked, vault: vault, counts: matches.map(&:type).tally)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def unmask(text, vault)
|
|
45
|
+
vault.restore(text)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
# When two detectors claim overlapping text, the earlier and then longer match wins.
|
|
51
|
+
# A full tie goes to the detector listed first, the same as in TypeScript, whose sort is stable.
|
|
52
|
+
def pick_non_overlapping(matches)
|
|
53
|
+
matches
|
|
54
|
+
.each_with_index
|
|
55
|
+
.sort_by { |m, i| [m.start, -(m.finish - m.start), i] }
|
|
56
|
+
.map(&:first)
|
|
57
|
+
.each_with_object([]) do |m, kept|
|
|
58
|
+
kept << m if kept.empty? || m.start >= kept.last.finish
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: Remembers which placeholder stands for which real value, and puts real values back on
|
|
4
|
+
# unmask.
|
|
5
|
+
|
|
6
|
+
module IndiaPiiGuard
|
|
7
|
+
# Remembers which placeholder stands for which real value.
|
|
8
|
+
# It never leaves your process: only the masked text goes to the LLM.
|
|
9
|
+
# Reuse one vault across a conversation so the same value keeps the same placeholder.
|
|
10
|
+
class Vault
|
|
11
|
+
TOKEN = /\[[A-Z]+_\d+\]/
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@token_by_key = {}
|
|
15
|
+
@value_by_token = {}
|
|
16
|
+
@counters = Hash.new(0)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def token_for(match)
|
|
20
|
+
@token_by_key[[match.type, match.key]] ||= begin
|
|
21
|
+
@counters[match.type] += 1
|
|
22
|
+
token = "[#{match.type}_#{@counters[match.type]}]"
|
|
23
|
+
@value_by_token[token] = match.value
|
|
24
|
+
token
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Put real values back. Unknown placeholders are left as they are.
|
|
29
|
+
def restore(text)
|
|
30
|
+
text.gsub(TOKEN) { |token| @value_by_token.fetch(token, token) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def size
|
|
34
|
+
@value_by_token.size
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def to_h
|
|
38
|
+
@value_by_token.dup
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: Verhoeff checksum, used to validate the last digit of an Aadhaar number.
|
|
4
|
+
|
|
5
|
+
module IndiaPiiGuard
|
|
6
|
+
# Verhoeff checksum, used by Aadhaar for its last digit.
|
|
7
|
+
# Checking it stops random 12-digit numbers (order IDs, amounts) from being masked.
|
|
8
|
+
module Verhoeff
|
|
9
|
+
D = [
|
|
10
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
11
|
+
[1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
|
|
12
|
+
[2, 3, 4, 0, 1, 7, 8, 9, 5, 6],
|
|
13
|
+
[3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
|
|
14
|
+
[4, 0, 1, 2, 3, 9, 5, 6, 7, 8],
|
|
15
|
+
[5, 9, 8, 7, 6, 0, 4, 3, 2, 1],
|
|
16
|
+
[6, 5, 9, 8, 7, 1, 0, 4, 3, 2],
|
|
17
|
+
[7, 6, 5, 9, 8, 2, 1, 0, 4, 3],
|
|
18
|
+
[8, 7, 6, 5, 9, 3, 2, 1, 0, 4],
|
|
19
|
+
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
|
|
20
|
+
].freeze
|
|
21
|
+
|
|
22
|
+
P = [
|
|
23
|
+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
24
|
+
[1, 5, 7, 6, 2, 8, 3, 0, 9, 4],
|
|
25
|
+
[5, 8, 0, 3, 7, 9, 6, 1, 4, 2],
|
|
26
|
+
[8, 9, 1, 6, 0, 4, 3, 5, 2, 7],
|
|
27
|
+
[9, 4, 5, 3, 1, 2, 6, 8, 7, 0],
|
|
28
|
+
[4, 2, 8, 6, 5, 7, 3, 9, 0, 1],
|
|
29
|
+
[2, 7, 9, 3, 8, 0, 6, 4, 1, 5],
|
|
30
|
+
[7, 0, 4, 6, 9, 1, 3, 2, 5, 8]
|
|
31
|
+
].freeze
|
|
32
|
+
|
|
33
|
+
INV = [0, 4, 3, 2, 1, 5, 6, 7, 8, 9].freeze
|
|
34
|
+
|
|
35
|
+
# ASCII digits only. "x".to_i is 0 in Ruby, so without this a letter would count as a zero.
|
|
36
|
+
DIGITS = /\A[0-9]+\z/
|
|
37
|
+
|
|
38
|
+
module_function
|
|
39
|
+
|
|
40
|
+
def valid?(digits)
|
|
41
|
+
return false unless digits.is_a?(String) && DIGITS.match?(digits)
|
|
42
|
+
|
|
43
|
+
c = 0
|
|
44
|
+
digits.reverse.each_char.with_index { |ch, i| c = D[c][P[i % 8][ch.to_i]] }
|
|
45
|
+
c.zero?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def check_digit(digits)
|
|
49
|
+
raise ArgumentError, "expected ASCII digits only, got #{digits.inspect}" unless digits.is_a?(String) && DIGITS.match?(digits)
|
|
50
|
+
|
|
51
|
+
c = 0
|
|
52
|
+
digits.reverse.each_char.with_index { |ch, i| c = D[c][P[(i + 1) % 8][ch.to_i]] }
|
|
53
|
+
INV[c]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Purpose: Public entry point of the gem: loads every part and defines IndiaPiiGuard.mask and
|
|
4
|
+
# IndiaPiiGuard.unmask.
|
|
5
|
+
|
|
6
|
+
require_relative "india_pii_guard/version"
|
|
7
|
+
require_relative "india_pii_guard/verhoeff"
|
|
8
|
+
require_relative "india_pii_guard/gstin_check"
|
|
9
|
+
require_relative "india_pii_guard/digits"
|
|
10
|
+
require_relative "india_pii_guard/detectors"
|
|
11
|
+
require_relative "india_pii_guard/configuration"
|
|
12
|
+
require_relative "india_pii_guard/vault"
|
|
13
|
+
require_relative "india_pii_guard/guard"
|
|
14
|
+
|
|
15
|
+
# Mask Indian PII before text reaches an LLM, then restore it in the response.
|
|
16
|
+
#
|
|
17
|
+
# result = IndiaPiiGuard.mask("PAN ABCPE1234F")
|
|
18
|
+
# result.text # => "PAN [PAN_1]"
|
|
19
|
+
# IndiaPiiGuard.unmask(llm_reply, result.vault) # real values back
|
|
20
|
+
module IndiaPiiGuard
|
|
21
|
+
# only: / except: choose detectors by name for this call, for example only: [:pan].
|
|
22
|
+
def self.mask(text, vault: Vault.new, detectors: nil, only: nil, except: nil)
|
|
23
|
+
Guard.new(detectors: detectors, only: only, except: except).mask(text, vault: vault)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def self.unmask(text, vault)
|
|
27
|
+
vault.restore(text)
|
|
28
|
+
end
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: india_pii_guard
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Bhushan Naidu
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/india_pii_guard.rb
|
|
22
|
+
- lib/india_pii_guard/configuration.rb
|
|
23
|
+
- lib/india_pii_guard/detectors.rb
|
|
24
|
+
- lib/india_pii_guard/digits.rb
|
|
25
|
+
- lib/india_pii_guard/gstin_check.rb
|
|
26
|
+
- lib/india_pii_guard/guard.rb
|
|
27
|
+
- lib/india_pii_guard/vault.rb
|
|
28
|
+
- lib/india_pii_guard/verhoeff.rb
|
|
29
|
+
- lib/india_pii_guard/version.rb
|
|
30
|
+
homepage: https://github.com/bhushan-naidu/india-pii-guard
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
source_code_uri: https://github.com/bhushan-naidu/india-pii-guard
|
|
35
|
+
changelog_uri: https://github.com/bhushan-naidu/india-pii-guard/blob/main/CHANGELOG.md
|
|
36
|
+
rubygems_mfa_required: 'true'
|
|
37
|
+
post_install_message:
|
|
38
|
+
rdoc_options: []
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '3.2'
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
51
|
+
requirements: []
|
|
52
|
+
rubygems_version: 3.5.1
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Mask Indian PII (PAN, Aadhaar, ...) before text reaches an LLM, and restore
|
|
56
|
+
it after.
|
|
57
|
+
test_files: []
|