Skip to content

Commit d23b611

Browse files
committed
Remove Session serialize/deserialize methods to fix RCE vulnerability
The Session.deserialize method used Oj.load without safe mode, which allows instantiation of arbitrary Ruby objects. If an attacker could control session storage (e.g., compromise a Redis instance or database), they could inject malicious serialized data to achieve remote code execution. These methods were vestigial code from when the library handled session storage (deprecated in v12.3.0). After that deprecation, apps became responsible for their own session persistence, rendering serialize/deserialize unnecessary for their original purpose. Investigation confirmed no external usage - the shopify_app gem stores individual session attributes in database columns and reconstructs sessions using Session.new(). The only internal usage was copy_attributes_from, which called serialize just to enumerate attribute names via JSON.parse(other.serialize).keys before copying instance variables. This has been refactored to directly copy each attribute, eliminating the dependency on serialize. Breaking change: Session#serialize and Session.deserialize removed. Migration: Apps should use Session.new() to reconstruct sessions from stored attributes (the pattern already used by shopify_app). Complete removal eliminates the RCE vector entirely while maintaining all functionality.
1 parent 8522bbd commit d23b611

File tree

3 files changed

+73
-16
lines changed

3 files changed

+73
-16
lines changed

BREAKING_CHANGES_FOR_V16.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Breaking change notice for version 16.0.0
2+
3+
## Removal of `Session#serialize` and `Session.deserialize` methods
4+
5+
The `Session#serialize` and `Session.deserialize` methods have been removed due to a security vulnerability. The `deserialize` method used `Oj.load` without safe mode, which allows instantiation of arbitrary Ruby objects.
6+
7+
These methods were originally created for session persistence when the library handled session storage. After session storage was deprecated in v12.3.0, applications became responsible for their own session persistence, making these methods unnecessary for their original purpose.
8+
9+
### Why this change?
10+
11+
**No impact on most applications:** The `shopify_app gem` stores individual session attributes in database columns and reconstructs sessions using `Session.new()`, which is the recommended pattern.
12+
13+
## Migration Guide
14+
15+
If your application was using `Session#serialize` and `Session.deserialize` for session persistence, you'll need to refactor to store individual session attributes and reconstruct sessions using `Session.new()`.
16+
17+
### Previous implementation (removed in v16.0.0)
18+
19+
```ruby
20+
# Storing a session
21+
session = ShopifyAPI::Auth::Session.new(
22+
shop: "example.myshopify.com",
23+
access_token: "shpat_xxxxx",
24+
scope: "read_products,write_orders"
25+
)
26+
27+
serialized_data = session.serialize
28+
# Store serialized_data in Redis, database, etc.
29+
redis.set("session:#{session.id}", serialized_data)
30+
31+
# Retrieving a session
32+
serialized_data = redis.get("session:#{session_id}")
33+
session = ShopifyAPI::Auth::Session.deserialize(serialized_data)
34+
```
35+
36+
### New implementation (required in v16.0.0)
37+
38+
Store individual session attributes and reconstruct using `Session.new()`:
39+
40+
## Reference: shopify_app gem implementation
41+
42+
The [shopify_app gem](https://github.com/Shopify/shopify_app) provides a reference implementation of session storage that follows these best practices:
43+
44+
**Shop Session Storage** ([source](https://github.com/Shopify/shopify_app/blob/main/lib/shopify_app/session/shop_session_storage.rb)):
45+
```ruby
46+
# Stores attributes in database columns
47+
def store(auth_session)
48+
shop = find_or_initialize_by(shopify_domain: auth_session.shop)
49+
shop.shopify_token = auth_session.access_token
50+
shop.save!
51+
end
52+
53+
# Reconstructs using Session.new()
54+
def retrieve(id)
55+
shop = find_by(id: id)
56+
return unless shop
57+
58+
ShopifyAPI::Auth::Session.new(
59+
shop: shop.shopify_domain,
60+
access_token: shop.shopify_token
61+
)
62+
end
63+
```

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Note: For changes to the API, see https://shopify.dev/changelog?filter=api
44
## Unreleased
5+
- ⚠️ [Breaking] Removed `Session#serialize` and `Session.deserialize` methods due to security concerns (RCE vulnerability via `Oj.load`). These methods were not used internally by the library. If your application relies on session serialization, use `Session.new()` to reconstruct sessions from stored attributes instead.
56

67
### 15.0.0
78

lib/shopify_api/auth/session.rb

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -117,29 +117,22 @@ def from(shop:, access_token_response:)
117117
shopify_session_id: access_token_response.session,
118118
)
119119
end
120-
121-
sig { params(str: String).returns(Session) }
122-
def deserialize(str)
123-
Oj.load(str)
124-
end
125120
end
126121

127122
sig { params(other: Session).returns(Session) }
128123
def copy_attributes_from(other)
129-
JSON.parse(other.serialize).keys.each do |key|
130-
next if key.include?("^")
131-
132-
variable_name = "@#{key}"
133-
instance_variable_set(variable_name, other.instance_variable_get(variable_name))
134-
end
124+
@shop = other.shop
125+
@state = other.state
126+
@access_token = other.access_token
127+
@scope = other.scope
128+
@associated_user_scope = other.associated_user_scope
129+
@expires = other.expires
130+
@associated_user = other.associated_user
131+
@is_online = other.online?
132+
@shopify_session_id = other.shopify_session_id
135133
self
136134
end
137135

138-
sig { returns(String) }
139-
def serialize
140-
Oj.dump(self)
141-
end
142-
143136
alias_method :eql?, :==
144137
sig { params(other: T.nilable(Session)).returns(T::Boolean) }
145138
def ==(other)

0 commit comments

Comments
 (0)