Skip to content

Commit 57df078

Browse files
authored
Merge pull request #6 from waratuman/waratuman/security-performance-review
Fix geometry type: perf, nil-limit crash, adapter pollution, and DDL hardening
2 parents 5796ece + 0c9a71f commit 57df078

6 files changed

Lines changed: 75 additions & 9 deletions

File tree

.github/workflows/ci.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
services:
13+
postgres:
14+
image: postgis/postgis:16-3.4
15+
env:
16+
POSTGRES_USER: postgres
17+
POSTGRES_PASSWORD: postgres
18+
POSTGRES_DB: postgis_adapter_test
19+
ports:
20+
- 5432:5432
21+
options: >-
22+
--health-cmd pg_isready
23+
--health-interval 10s
24+
--health-timeout 5s
25+
--health-retries 5
26+
27+
env:
28+
PGHOST: localhost
29+
PGPORT: 5432
30+
PGUSER: postgres
31+
PGPASSWORD: postgres
32+
33+
strategy:
34+
fail-fast: false
35+
matrix:
36+
ruby: ['3.2', '3.3', '3.4']
37+
38+
steps:
39+
- uses: actions/checkout@v5
40+
41+
# rgeo's CAPI extension links against GEOS at build time, so libgeos-dev
42+
# must be present before `bundle install` compiles the native extension.
43+
- name: Install GEOS
44+
run: sudo apt-get update && sudo apt-get install -y libgeos-dev
45+
46+
- uses: ruby/setup-ruby@v1
47+
with:
48+
ruby-version: ${{ matrix.ruby }}
49+
bundler-cache: true
50+
51+
- name: Run tests
52+
run: bundle exec rake test

lib/active_record/connection_adapters/postgis/oid/geometry.rb

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ module OID
88
class Geometry < Type::Value
99
include ActiveModel::Type::Helpers::Mutable
1010

11+
# Build the RGeo factory, parsers and generator once, up front. The
12+
# SRID is fixed for the column, so these are invariant for the life of
13+
# the type instance. Doing it here (rather than lazily on each cast)
14+
# avoids rebuilding a GEOS factory on every value read, and is
15+
# required because Type instances are frozen after initialization.
16+
def initialize(...)
17+
super
18+
@rgeo_factory = RGeo::Geos.factory(srid: srid)
19+
@wkb_parser = RGeo::WKRep::WKBParser.new(@rgeo_factory, support_ewkb: true, default_srid: srid)
20+
@wkt_parser = RGeo::WKRep::WKTParser.new(@rgeo_factory, support_ewkt: true, default_srid: srid)
21+
@wkb_generator = RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true)
22+
end
23+
1124
def type
1225
:geometry
1326
end
@@ -20,9 +33,9 @@ def cast(value)
2033
value
2134
when ::String # HEXEWKB
2235
if value[0,1] == "\x00" || value[0,1] == "\x01" || value[0,4] =~ /[0-9a-fA-F]{4}/
23-
RGeo::WKRep::WKBParser.new(rgeo_factory_generator, support_ewkb: true, default_srid: limit[:srid]).parse(value)
36+
@wkb_parser.parse(value)
2437
else
25-
RGeo::WKRep::WKTParser.new(rgeo_factory_generator, support_ewkt: true, default_srid: limit[:srid]).parse(value)
38+
@wkt_parser.parse(value)
2639
end
2740
else
2841
raise ArgumentError, "Unsupported argument type (#{value.class})"
@@ -32,7 +45,7 @@ def cast(value)
3245
def serialize(value)
3346
case value
3447
when RGeo::Feature::Instance
35-
::RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)
48+
@wkb_generator.generate(value)
3649
else
3750
value
3851
end
@@ -44,8 +57,8 @@ def changed_in_place?(raw_old_value, new_value)
4457

4558
private
4659

47-
def rgeo_factory_generator
48-
RGeo::Geos.factory(srid: limit[:srid].to_i)
60+
def srid
61+
limit ? limit[:srid].to_i : 0
4962
end
5063

5164
end

lib/active_record/connection_adapters/postgis/schema_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def type_to_sql(sql_type, limit: nil, precision: nil, scale: nil, array: nil, ty
1414
type ||= 'Geometry'
1515
srid ||= 4326
1616
end
17-
"geometry(#{type},#{srid})"
17+
"geometry(#{type},#{srid.to_i})"
1818
else
1919
super
2020
end

lib/active_record/connection_adapters/postgis_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module ConnectionAdapters
1212
class PostGISAdapter < PostgreSQLAdapter
1313
ADAPTER_NAME = 'PostGIS'.freeze
1414

15-
NATIVE_DATABASE_TYPES = PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge!({
15+
NATIVE_DATABASE_TYPES = PostgreSQLAdapter::NATIVE_DATABASE_TYPES.merge({
1616
geometry: { name: "geometry" },
1717
})
1818

test/cases/adapters/postgis/geometry_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_polygon_with_hole
2727
assert_equal "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))", geo.polygon_with_hole.as_text
2828
end
2929

30-
def test_polygon_with_hole
30+
def test_collection
3131
geo = Geo.create(collection: 'GEOMETRYCOLLECTION(POINT(2 0),POLYGON((0 0, 1 0, 1 1, 0 1, 0 0)))')
3232
assert geo.collection.is_a?(RGeo::Geos::CAPIGeometryCollectionImpl)
3333
assert_equal "GEOMETRYCOLLECTION (POINT (2 0), POLYGON ((0 0, 1 0, 1 1, 0 1, 0 0)))", geo.collection.as_text

test/cases/helper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
require "postgis_adapter"
44

55
require "active_support"
6-
require "active_support/testing/autorun"
6+
require "active_support/test_case"
7+
require "minitest/autorun"
78

89
ActiveRecord::Base.logger = ActiveSupport::Logger.new("debug.log", 0, 100 * 1024 * 1024)
910
ActiveRecord::Base.configurations = ActiveRecord::DatabaseConfigurations.new({

0 commit comments

Comments
 (0)