Skip to content
This repository was archived by the owner on Dec 6, 2022. It is now read-only.

Commit 15a41fd

Browse files
committed
Allow overriding /attachments path with RUBYWARDEN_ATTACHMENTS_URL
Also stop storing the attachment URL in the database, it's not used by anything and may change later depending on this variable
1 parent 0cbd5e0 commit 15a41fd

File tree

5 files changed

+30
-22
lines changed

5 files changed

+30
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Identity, and Icon URLs.
8282
If you are not deploying Rubywarden on its own hostname or want to alter the
8383
paths for any reason, you can override them with environment variables:
8484

85+
- `RUBYWARDEN_ATTACHMENTS_URL` for the attachments URL - defaults to `/attachments`
8586
- `RUBYWARDEN_BASE_URL` for the API base - defaults to `/api`
8687
- `RUBYWARDEN_IDENTITY_BASE_URL` for the identity API base - defaults to
8788
`/identity`
@@ -91,7 +92,7 @@ For example, if you had a website `example.com` and wanted to host Rubywarden
9192
on a subdirectory called `/notbitwarden`, you would set the environment
9293
variables in your startup script:
9394

94-
sudo -u _rubywarden env RUBYWARDEN_ENV=production RUBYWARDEN_BASE_URL=/notbitwarden/api RUBYWARDEN_IDENTITY_BASE_URL=/notbitwarden/identity RUBYWARDEN_ICONS_URL=/notbitwarden/icons bundle exec rackup -p 4567 config.ru
95+
sudo -u _rubywarden env RUBYWARDEN_ENV=production RUBYWARDEN_BASE_URL=/notbitwarden/api RUBYWARDEN_IDENTITY_BASE_URL=/notbitwarden/identity RUBYWARDEN_ICONS_URL=/notbitwarden/icons RUBYWARDEN_ATTACHMENTS_URL=/notbitwarden/attachments bundle exec rackup -p 4567 config.ru
9596

9697
Then you can configure the Bitwarden clients with a single server URL of
9798
`https://example.com/notbitwarden`.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DropAttachmentsUrl < ActiveRecord::Migration[5.1]
2+
def change
3+
remove_column "attachments", "url"
4+
end
5+
end

lib/attachment.rb

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,16 @@
1616

1717
class Attachment < DBModel
1818
self.table_name = "attachments"
19-
attr_accessor :context
2019

2120
before_create :generate_uuid_primary_key
22-
before_create :generate_url
2321

24-
belongs_to :cipher, foreign_key: :cipher_uuid, inverse_of: :attachments
22+
belongs_to :cipher,
23+
foreign_key: :cipher_uuid,
24+
inverse_of: :attachments
2525

26-
def self.build_from_params(params, context)
27-
attachment = new filename: params[:filename],
28-
size: params[:size],
29-
file: params[:file]
30-
attachment.context = context
31-
attachment
26+
def self.build_from_params(params)
27+
Attachment.new(filename: params[:filename], size: params[:size],
28+
file: params[:file])
3229
end
3330

3431
def to_hash
@@ -42,12 +39,11 @@ def to_hash
4239
}
4340
end
4441

45-
private
46-
47-
def generate_url
48-
self.url = context.url("/attachments/#{self.cipher_uuid}/#{self.id}")
42+
def url
43+
"#{::ATTACHMENTS_URL}/#{self.cipher_uuid}/#{self.id}"
4944
end
5045

46+
private
5147
def human_file_size
5248
ActiveSupport::NumberHelper.number_to_human_size(self.size)
5349
end

lib/routes/attachments.rb

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def self.registered(app)
3838
attachment_params = { filename: filename,
3939
size: file.size,
4040
file: file.read }
41-
attachment = cipher.attachments.build_from_params(attachment_params, self)
41+
attachment = cipher.attachments.build_from_params(attachment_params)
4242

4343
Attachment.transaction do
4444
if !attachment.save
@@ -50,19 +50,24 @@ def self.registered(app)
5050
end
5151

5252
delete "/ciphers/:uuid/attachment/:attachment_id" do
53-
delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id]
53+
delete_attachment uuid: params[:uuid],
54+
attachment_uuid: params[:attachment_id]
5455
end
5556

5657
post "/ciphers/:uuid/attachment/:attachment_id/delete" do
57-
delete_attachment uuid: params[:uuid], attachment_uuid: params[:attachment_id]
58+
delete_attachment uuid: params[:uuid],
59+
attachment_uuid: params[:attachment_id]
5860
end
5961
end # BASE_URL
6062

61-
app.get "/attachments/:uuid/:attachment_id" do
62-
a = Attachment.find_by_uuid_and_cipher_uuid(params[:attachment_id], params[:uuid])
63-
attachment(a.filename)
64-
response.write(a.file)
65-
end
63+
app.namespace ATTACHMENTS_URL do
64+
get "/:uuid/:attachment_id" do
65+
a = Attachment.find_by_uuid_and_cipher_uuid(params[:attachment_id],
66+
params[:uuid])
67+
attachment(a.filename)
68+
response.write(a.file)
69+
end
70+
end # ATTACHMENTS_URL
6671
end # registered app
6772
end
6873
end

lib/rubywarden.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
BASE_URL = (ENV["RUBYWARDEN_BASE_URL"] || "/api")
4242
IDENTITY_BASE_URL = (ENV["RUBYWARDEN_IDENTITY_BASE_URL"] || "/identity")
4343
ICONS_URL = (ENV["RUBYWARDEN_ICONS_URL"] || "/icons")
44+
ATTACHMENTS_URL = (ENV["RUBYWARDEN_ATTACHMENTS_URL"] || "/attachments")
4445

4546
# whether to allow new users
4647
if !defined?(ALLOW_SIGNUPS)

0 commit comments

Comments
 (0)