curtilage 0.3.0 → 0.4.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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +75 -0
  3. data/README.md +76 -22
  4. data/SECURITY.md +18 -3
  5. data/docs/reference.md +221 -27
  6. data/docs/schema/site-v1.json +362 -0
  7. data/docs/security.md +69 -0
  8. data/exe/curtilage +375 -82
  9. data/lib/curtilage/audit.rb +5 -5
  10. data/lib/curtilage/auto_data.rb +61 -11
  11. data/lib/curtilage/batch.rb +116 -0
  12. data/lib/curtilage/build.rb +117 -63
  13. data/lib/curtilage/candidates.rb +67 -0
  14. data/lib/curtilage/catalogue.rb +44 -16
  15. data/lib/curtilage/confidence.rb +55 -0
  16. data/lib/curtilage/config.rb +24 -2
  17. data/lib/curtilage/corrections.rb +57 -0
  18. data/lib/curtilage/diff.rb +90 -0
  19. data/lib/curtilage/doctor.rb +92 -0
  20. data/lib/curtilage/download.rb +42 -16
  21. data/lib/curtilage/duckdb.rb +170 -6
  22. data/lib/curtilage/error.rb +3 -0
  23. data/lib/curtilage/features.rb +54 -5
  24. data/lib/curtilage/formats.rb +271 -36
  25. data/lib/curtilage/generator.rb +145 -166
  26. data/lib/curtilage/generator_details.rb +22 -14
  27. data/lib/curtilage/generator_lot.rb +56 -32
  28. data/lib/curtilage/generator_pin.rb +74 -0
  29. data/lib/curtilage/generator_roads.rb +109 -0
  30. data/lib/curtilage/generator_steps.rb +54 -0
  31. data/lib/curtilage/generator_store.rb +18 -4
  32. data/lib/curtilage/generator_zones.rb +93 -0
  33. data/lib/curtilage/geometry.rb +48 -52
  34. data/lib/curtilage/lookup.rb +41 -21
  35. data/lib/curtilage/overture.rb +137 -17
  36. data/lib/curtilage/preview.rb +55 -16
  37. data/lib/curtilage/profiles.rb +62 -0
  38. data/lib/curtilage/regions.rb +4 -8
  39. data/lib/curtilage/result.rb +72 -7
  40. data/lib/curtilage/validation.rb +73 -9
  41. data/lib/curtilage/version.rb +1 -1
  42. data/lib/curtilage/zip_range.rb +81 -16
  43. data/lib/curtilage.rb +155 -17
  44. data/sql/build_from_shapefiles.sql +1 -1
  45. metadata +15 -2
@@ -0,0 +1,362 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://github.com/mohanenm/curtilage/blob/main/docs/schema/site-v1.json",
4
+ "title": "curtilage site, version 1",
5
+ "description": "One line of `curtilage generate --format ndjson`: a GeoJSON Feature holding one site's premises. Version 1 only gains optional properties; a change that removes or retypes one gets a new file.",
6
+ "type": "object",
7
+ "required": [
8
+ "type",
9
+ "geometry",
10
+ "properties"
11
+ ],
12
+ "properties": {
13
+ "type": {
14
+ "const": "Feature"
15
+ },
16
+ "geometry": {
17
+ "$ref": "#/$defs/polygon_or_null",
18
+ "description": "The premises (closed, convex ring of [lng, lat]); null when none could be made."
19
+ },
20
+ "properties": {
21
+ "type": "object",
22
+ "required": [
23
+ "id",
24
+ "role",
25
+ "ok",
26
+ "confidence",
27
+ "confidence_factors",
28
+ "method",
29
+ "flags",
30
+ "building",
31
+ "details",
32
+ "errors",
33
+ "warnings",
34
+ "metrics",
35
+ "explanation",
36
+ "steps",
37
+ "attribution",
38
+ "provenance"
39
+ ],
40
+ "properties": {
41
+ "id": {
42
+ "type": "string",
43
+ "description": "The site's id from the input, or its position from 1."
44
+ },
45
+ "role": {
46
+ "const": "premises"
47
+ },
48
+ "ok": {
49
+ "type": "boolean",
50
+ "description": "The premises passed validation. Never use one that isn't."
51
+ },
52
+ "pin_quality": {
53
+ "type": [
54
+ "object",
55
+ "null"
56
+ ],
57
+ "description": "Where the pin was given and how far from the nearest building; moved_from when snapped.",
58
+ "properties": {
59
+ "quality": {
60
+ "enum": [
61
+ "on_building",
62
+ "on_road",
63
+ "near_building",
64
+ "far"
65
+ ]
66
+ },
67
+ "building_m": {
68
+ "type": "number"
69
+ },
70
+ "moved_from": {
71
+ "type": "array",
72
+ "minItems": 2,
73
+ "maxItems": 2,
74
+ "items": {
75
+ "type": "number"
76
+ }
77
+ }
78
+ }
79
+ },
80
+ "confidence": {
81
+ "type": "number",
82
+ "minimum": 0,
83
+ "maximum": 1,
84
+ "description": "How far to trust the result (rule-based; see the reference). 0 when not ok."
85
+ },
86
+ "confidence_factors": {
87
+ "type": "array",
88
+ "description": "Every factor behind the confidence: when one isn't met, its weight comes off 1.",
89
+ "items": {
90
+ "type": "object",
91
+ "required": [
92
+ "name",
93
+ "met",
94
+ "weight"
95
+ ],
96
+ "properties": {
97
+ "name": {
98
+ "type": "string"
99
+ },
100
+ "met": {
101
+ "type": "boolean"
102
+ },
103
+ "weight": {
104
+ "type": "number"
105
+ }
106
+ }
107
+ }
108
+ },
109
+ "method": {
110
+ "type": [
111
+ "string",
112
+ "null"
113
+ ],
114
+ "enum": [
115
+ "grow_trim",
116
+ "building",
117
+ null
118
+ ]
119
+ },
120
+ "flags": {
121
+ "type": "array",
122
+ "items": {
123
+ "type": "string"
124
+ },
125
+ "description": "Review flags (see the reference)."
126
+ },
127
+ "building": {
128
+ "description": "The id of the building used, or null."
129
+ },
130
+ "details": {
131
+ "type": [
132
+ "object",
133
+ "null"
134
+ ],
135
+ "description": "Facts about the site (building, parking_area_m2, parking_spaces_estimate, frontage_m, access_points); null when no premises were made."
136
+ },
137
+ "errors": {
138
+ "type": "array",
139
+ "items": {
140
+ "type": "string"
141
+ }
142
+ },
143
+ "warnings": {
144
+ "type": "array",
145
+ "items": {
146
+ "type": "string"
147
+ }
148
+ },
149
+ "metrics": {
150
+ "type": "object"
151
+ },
152
+ "explanation": {
153
+ "type": "array",
154
+ "items": {
155
+ "type": "string"
156
+ },
157
+ "description": "Each step in plain words."
158
+ },
159
+ "steps": {
160
+ "type": "array",
161
+ "description": "The explanation as records, one per line in the same order (see the reference for step names and rules).",
162
+ "items": {
163
+ "type": "object",
164
+ "required": [
165
+ "step",
166
+ "text"
167
+ ],
168
+ "properties": {
169
+ "step": {
170
+ "type": "string"
171
+ },
172
+ "text": {
173
+ "type": "string"
174
+ },
175
+ "rule": {
176
+ "type": "string"
177
+ },
178
+ "features": {
179
+ "type": "array"
180
+ },
181
+ "distance_m": {
182
+ "type": "number"
183
+ },
184
+ "area_before_m2": {
185
+ "type": "number"
186
+ },
187
+ "area_after_m2": {
188
+ "type": "number"
189
+ },
190
+ "flag": {
191
+ "type": "string"
192
+ }
193
+ }
194
+ }
195
+ },
196
+ "attribution": {
197
+ "type": "array",
198
+ "items": {
199
+ "type": "string"
200
+ },
201
+ "minItems": 1,
202
+ "description": "Credit these wherever the result is shown."
203
+ },
204
+ "provenance": {
205
+ "type": "object",
206
+ "required": [
207
+ "gem_version"
208
+ ],
209
+ "properties": {
210
+ "gem_version": {
211
+ "type": "string"
212
+ },
213
+ "data": {
214
+ "type": [
215
+ "object",
216
+ "null"
217
+ ],
218
+ "properties": {
219
+ "source": {
220
+ "enum": [
221
+ "overture",
222
+ "geofabrik",
223
+ "parquet"
224
+ ]
225
+ },
226
+ "release": {
227
+ "type": "string",
228
+ "description": "Overture release, e.g. 2026-11-18.0."
229
+ },
230
+ "regions": {
231
+ "type": "array",
232
+ "items": {
233
+ "type": "object"
234
+ }
235
+ }
236
+ }
237
+ },
238
+ "features_sha256": {
239
+ "type": "string",
240
+ "pattern": "^[0-9a-f]{64}$"
241
+ },
242
+ "options": {
243
+ "type": "object",
244
+ "description": "Every generation option after defaults."
245
+ }
246
+ }
247
+ },
248
+ "store": {
249
+ "$ref": "#/$defs/polygon_or_null",
250
+ "description": "The store zone (the building's outline, inside the premises); absent with --no-store."
251
+ },
252
+ "approach": {
253
+ "$ref": "#/$defs/polygon_or_null",
254
+ "description": "The approach zone (the premises grown by approach_m, simplified); only with --approach."
255
+ },
256
+ "pickup": {
257
+ "$ref": "#/$defs/polygon_or_null",
258
+ "description": "The pickup zone, always estimated (mapped parking within pickup_m of the building, inside the premises); only with --pickup, and absent when none was found."
259
+ },
260
+ "staging": {
261
+ "type": "object",
262
+ "required": [
263
+ "type",
264
+ "coordinates"
265
+ ],
266
+ "properties": {
267
+ "type": {
268
+ "const": "Point"
269
+ },
270
+ "coordinates": {
271
+ "type": "array",
272
+ "minItems": 2,
273
+ "maxItems": 2,
274
+ "items": {
275
+ "type": "number"
276
+ }
277
+ }
278
+ },
279
+ "description": "The staging point, always estimated (the pickup zone's point nearest the building); only with a pickup zone."
280
+ },
281
+ "candidates": {
282
+ "type": "array",
283
+ "description": "Other premises the site could have, most confident first; only with --candidates.",
284
+ "items": {
285
+ "type": "object",
286
+ "required": [
287
+ "rank",
288
+ "geometry",
289
+ "confidence",
290
+ "flags"
291
+ ],
292
+ "properties": {
293
+ "rank": {
294
+ "type": "integer"
295
+ },
296
+ "geometry": {
297
+ "$ref": "#/$defs/polygon_or_null"
298
+ },
299
+ "confidence": {
300
+ "type": "number"
301
+ },
302
+ "flags": {
303
+ "type": "array",
304
+ "items": {
305
+ "type": "string"
306
+ }
307
+ },
308
+ "building": {},
309
+ "method": {
310
+ "type": "string"
311
+ },
312
+ "explanation": {
313
+ "type": "array",
314
+ "items": {
315
+ "type": "string"
316
+ }
317
+ }
318
+ }
319
+ }
320
+ }
321
+ }
322
+ }
323
+ },
324
+ "$defs": {
325
+ "polygon_or_null": {
326
+ "oneOf": [
327
+ {
328
+ "type": "null"
329
+ },
330
+ {
331
+ "type": "object",
332
+ "required": [
333
+ "type",
334
+ "coordinates"
335
+ ],
336
+ "properties": {
337
+ "type": {
338
+ "const": "Polygon"
339
+ },
340
+ "coordinates": {
341
+ "type": "array",
342
+ "minItems": 1,
343
+ "maxItems": 1,
344
+ "items": {
345
+ "type": "array",
346
+ "minItems": 4,
347
+ "items": {
348
+ "type": "array",
349
+ "minItems": 2,
350
+ "maxItems": 2,
351
+ "items": {
352
+ "type": "number"
353
+ }
354
+ }
355
+ }
356
+ }
357
+ }
358
+ }
359
+ ]
360
+ }
361
+ }
362
+ }
data/docs/security.md ADDED
@@ -0,0 +1,69 @@
1
+ # Security model
2
+
3
+ What curtilage trusts, what it doesn't, who might attack it, and how each attack is met. Report problems privately, as [SECURITY.md](../SECURITY.md) explains.
4
+
5
+ ## What it does that matters
6
+
7
+ - **Downloads** public map data over HTTPS: Geofabrik's region index and shapefiles, Overture's catalogue and file index, and, when opted in, Microsoft's footprint tiles.
8
+ - **Runs the DuckDB CLI**, an external program, with SQL it builds. DuckDB reads Parquet files (local, in a bucket, or Overture's) and installs its `spatial` and `httpfs` extensions.
9
+ - **Parses untrusted input**: your pin files, and all map data.
10
+ - **Writes** GeoJSON, CSV, KML and an HTML preview, files under `--out`, and a cache folder of built regions.
11
+
12
+ ## Trust boundaries
13
+
14
+ | Input | Trusted? | How it's handled |
15
+ |---|---|---|
16
+ | Your Ruby calls | The caller is trusted, the values are checked | Unknown options raise; numeric options are range-checked; pins are checked before use in SQL. |
17
+ | Pin files (CSV, GeoJSON) | No | Capped at 50 MB and 100,000 pins; fields and ids at 10,000 characters, rows at 1,000 columns, JSON nesting at 20, all enforced while reading; must be UTF-8 without NUL bytes; CSV parsed by a strict quoted-field reader; every pin checked as finite plain decimals in range. |
18
+ | Map data (Geofabrik, Overture, Microsoft, your own catalogue files) | No | At most 20,000 features per pin and 5,000 vertices per ring; empty, self-intersecting or invalid geometry dropped; names and ids escaped in every output; DuckDB output capped at 200 MB; values from map data never become SQL. |
19
+ | The DuckDB binary (`CURTILAGE_DUCKDB`, or `duckdb` on the `PATH`) | **Yes**: trusted configuration | It runs with your user's rights. Point it only at a DuckDB you installed. `curtilage doctor` shows which one runs. |
20
+ | DuckDB's extensions | Through DuckDB's signature checks | Installed from DuckDB's repository on first use, or with `CURTILAGE_OFFLINE=1` only loaded from a pre-populated folder (the Docker image ships one). The gem never turns signature checks off or allows unsigned extensions (a test checks every SQL it builds). `doctor` and each build's metadata record the versions loaded. |
21
+ | `setup_sql:` (lookup option) | **Yes** | Passed to DuckDB as written, for statements such as `CREATE SECRET`. Never fill it from untrusted input. |
22
+ | Environment variables (`CURTILAGE_*`) and `Curtilage.configure` | **Yes**: configuration | Values that reach SQL are quoted (`DuckDB.quote`) or converted to numbers first. |
23
+ | Catalogue folders and bucket URLs | The location is configuration; the files are map data | Paths and URLs are quoted in SQL; what they contain is treated like any map data. |
24
+ | The cache folder | Only if nobody else can write to it | DuckDB reads its Parquet files and the gem trusts its metadata. A new cache folder is created `0700`; one another user owns, or everyone can write to, is refused. Each built file's size and SHA256 are recorded; a missing or wrong-sized file is rebuilt, and `curtilage doctor --verify DIR` checks the hashes. |
25
+ | Upstream hosts | No | HTTPS with certificate checks, to a fixed list of hosts; no redirects followed; sizes capped while streaming. |
26
+
27
+ ## Who might attack, and the defences
28
+
29
+ **A poisoned map-data file** (vandalised OSM data, a compromised mirror or bucket). Oversized and invalid geometry is dropped and counts are capped, so a file can't exhaust memory through the gem's own parsing; text from map data is escaped wherever it's written; nothing from map data is placed into SQL (Overture file links are accepted only from Overture's own bucket, and quoted). What's left: bugs in DuckDB or its extensions when reading a crafted Parquet file or shapefile, and wrong but plausible geometry, which validation, flags and review are there to catch.
30
+
31
+ **A malicious input file** (a pin list from someone else). Size, pin-count and nesting caps; a strict CSV reader; pins must be numbers in range. Outputs are made safe for where they're opened: CSV cells a spreadsheet would run as a formula get a leading `'`, the preview escapes all text, KML escapes all text, and GeoJSON is written by a JSON library.
32
+
33
+ **A shared cache on a multi-user machine.** Another user who can write to the cache folder could replace a built region with a crafted one. The gem creates its cache folder `0700` and refuses one that another user owns or everyone can write to; a group-writable folder is your call. Recorded sizes catch a truncated or swapped file on use, and `doctor --verify` checks every SHA256.
34
+
35
+ **A compromised upstream release.** Downloads go only to the listed hosts over HTTPS. Geofabrik publishes no checksums, so a build checks each file's zip CRC, which catches corruption but not deliberate tampering, and records every downloaded file's size and SHA256 in the region's metadata: a later build of the same dated extract warns if anything changed. The DuckDB binary and its extensions come from DuckDB: install DuckDB from a source you trust.
36
+
37
+ **Hostile configuration** (environment variables, `setup_sql:`, the DuckDB path). Configuration is trusted by design: whoever sets it already controls what runs. It's documented here so nobody fills it from untrusted input.
38
+
39
+ **The HTTP server** (`curtilage serve`, planned). Not built yet. When it is: it will bind to `127.0.0.1` by default, support an optional bearer token, cap body size, pins, time and concurrency, and never log pins unless asked.
40
+
41
+ ## Defences in the code
42
+
43
+ - **Pins**: every entry point goes through `Validation.pin`: two finite plain decimal numbers, in range.
44
+ - **SQL**: text values go through `DuckDB.quote`, which also refuses line breaks and NUL bytes (the DuckDB CLI treats a line starting with `.` as its own command); numbers through `Float()`/`Integer()`; pins through `Validation.pin`; `memory_limit` must match a size pattern and `threads` must be 1 to 64. `test/sql_safety_test.rb` pushes crafted values through each path into SQL.
45
+ - **DuckDB runs** (`DuckDB.capture`): each in its own process group, stopped with everything it started if the caller is interrupted (Ctrl-C) or after a timeout (600 s for lookups, 3600 s for builds); output read as it arrives and DuckDB stopped once it passes the cap; lookups and Overture reads capped at 1 GB of memory; error messages stripped of the query excerpt DuckDB quotes, which can hold the box around a pin.
46
+ - **Commands**: every external program (DuckDB, `curl` when installed, `git` in the benchmark) runs through `Open3`, `system` or `IO.popen` with an argument list, never a shell string.
47
+ - **Downloads** (`Download`): only `ALLOWED_HOSTS`; HTTPS only (`curl --proto =https`); no redirects followed; size capped while streaming; written to a `.part` file, renamed only when complete and removed if the download fails or is interrupted; transient failures retried three times.
48
+ - **Temporary folders**: created with `Dir.mktmpdir` (unique, private) and removed afterwards, including `curtilage build`'s work folder when `--work` isn't given; never a fixed name in a shared `/tmp`.
49
+ - **Zip extraction** (`ZipRange`): only plain file names (no folders, so nothing can be written outside the target, and no symlinks are created); at most 1,000 entries; compressed size capped; the layers taken may inflate to at most 20 GB in all and no member by more than 200 times; inflated size must match the header; CRC-32 checked. A whole zip downloaded as a fallback is read the same way, from disk; no external `unzip` is used.
50
+ - **Atomic writes**: the cached region index, each region's metadata and the command line's `--out` files are written to a temporary name and renamed; an unreadable cached index is fetched again. `--out` refuses a symbolic link at the target.
51
+ - **Errors**: everything raised on purpose is a `Curtilage::Error`, or an `ArgumentError` for bad input.
52
+ - **Batches**: pins and options are checked before any data is fetched; at most 100,000 pins per call and 8 parallel builds; one site that raises during generation gets an `error` result instead of aborting the batch.
53
+ - **Output**: the preview escapes all text, drops Unicode direction controls from it, and carries a Content-Security-Policy that allows only its own inline style and script (by hash) and no loads at all, so a missed escape can't run script. CSV cells a spreadsheet would run as a formula (a leading `=`, `+`, `-`, `@`, `|` or `%`, full-width forms, after any whitespace) get a leading `'`, in every column; KML escapes all text; GeoJSON is written by a JSON library.
54
+ - **Fuzz tests** (`test/fuzz_test.rb`, plain Ruby, fixed seed): the CSV and GeoJSON readers, map-data parsing, geometry on degenerate input (repeated, lined-up and coincident points), validation, and generation near the poles and the antimeridian. `FUZZ_ITERATIONS=5000` runs a longer pass.
55
+ - **Docker**: the base image is pinned by digest; the DuckDB download is checked against a pinned SHA256; a multi-stage build leaves curl and unzip out of the image that runs; it runs as a non-root user, works offline and with a read-only root filesystem. CI builds it on every change and runs `doctor` inside it with no network.
56
+ - **CI and dependencies**: GitHub Actions pinned to commit SHAs with least-privilege permissions; CodeQL for Ruby; `bundler-audit` on the development gems; Dependabot for gems, actions and the base image. The gem itself has no runtime gem dependencies.
57
+
58
+ ## Extension versions
59
+
60
+ DuckDB's extensions come from its own repository, signed, and their versions follow the DuckDB version. The gem doesn't pin the repository or an extension version: that would mean running an unsigned or custom repository, or holding back security fixes. To control exactly which extension binaries run, populate an extension folder once (`curtilage doctor`, or the Docker image) and run with `CURTILAGE_OFFLINE=1`: nothing is installed or downloaded after that, and the versions are recorded.
61
+
62
+ ## Known gaps
63
+
64
+ None open from this review. Planned: release signing and provenance (see the release plan), and the HTTP server's limits when it exists.
65
+
66
+
67
+ ## Out of scope
68
+
69
+ The accuracy of OpenStreetMap or Overture data; whether a generated polygon matches the real property line (results are proposals for review); and denial of service by giving the gem a large but legitimate workload on your own machine.