-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restrict access to uploads #2088
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
DOMAIN="example.com" | ||
STAGING_API_URL="bops-staging.services" | ||
STAGING_API_BEARER="fjisdfjsdiofjdsoi" | ||
OS_VECTOR_TILES_API_KEY="testtest" | ||
NOTIFY_LETTER_API_KEY='testtest' | ||
UPLOADS_HOSTNAME=uploads.example.com | ||
UPLOADS_BASE_URL=http://uploads.example.com | ||
USE_SIGNED_URLS=false |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,22 @@ class Document < ApplicationRecord | |
class Routing | ||
include Rails.application.routes.url_helpers | ||
include Rails.application.routes.mounted_helpers | ||
|
||
def initialize(subdomain) | ||
@subdomain = subdomain | ||
end | ||
|
||
def default_url_options | ||
{host: "#{subdomain}.#{domain}"} | ||
end | ||
|
||
private | ||
|
||
attr_reader :subdomain | ||
|
||
def domain | ||
Rails.configuration.domain | ||
end | ||
end | ||
|
||
class NotArchiveableError < StandardError; end | ||
|
@@ -29,6 +45,7 @@ class NotArchiveableError < StandardError; end | |
inverse_of: false | ||
|
||
delegate :audits, to: :planning_application | ||
delegate :local_authority, to: :planning_application | ||
delegate :blob, :representable?, to: :file | ||
|
||
include Auditable | ||
|
@@ -209,12 +226,11 @@ class NotArchiveableError < StandardError; end | |
validate :numbered | ||
validate :created_date_is_in_the_past | ||
|
||
default_scope -> { no_owner.or(not_excluded_owners) } | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This needed to be removed so the uploads engine could find all documents for checking the blob key |
||
scope :no_owner, -> { where(owner_type: nil) } | ||
scope :not_excluded_owners, -> { where.not(owner_type: EXCLUDED_OWNERS) } | ||
scope :default, -> { no_owner.or(not_excluded_owners) } | ||
scope :by_created_at, -> { order(created_at: :asc) } | ||
scope :active, -> { where(archived_at: nil) } | ||
scope :active, -> { default.where(archived_at: nil) } | ||
scope :invalidated, -> { where(validated: false) } | ||
scope :validated, -> { where(validated: true) } | ||
scope :redacted, -> { where(redacted: true) } | ||
|
@@ -225,8 +241,8 @@ class NotArchiveableError < StandardError; end | |
) | ||
scope :publishable, -> { where(publishable: true) } | ||
|
||
scope :for_publication, -> { active.publishable } | ||
scope :for_display, -> { active.referenced_in_decision_notice } | ||
scope :for_publication, -> { publishable } | ||
scope :for_display, -> { referenced_in_decision_notice } | ||
benjamineskola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
scope :with_tag, ->(tag) { where(arel_table[:tags].contains(Array.wrap(tag))) } | ||
scope :with_siteplan_tags, -> { where(arel_table[:tags].overlaps(%w[sitePlan.existing sitePlan.proposed])) } | ||
|
@@ -247,7 +263,33 @@ class NotArchiveableError < StandardError; end | |
self.user ||= Current.user | ||
end | ||
|
||
has_many :file_variant_records, through: :file_blob, source: :variant_records | ||
has_many :file_variant_attachments, through: :file_variant_records, source: :image_attachment | ||
has_many :file_variant_blobs, through: :file_variant_attachments, source: :blob | ||
|
||
has_one :file_preview_image_attachment, through: :file_blob, source: :preview_image_attachment | ||
has_one :file_preview_blob, through: :file_preview_image_attachment, source: :blob | ||
has_many :file_preview_variant_records, through: :file_preview_blob, source: :variant_records | ||
has_many :file_preview_variant_attachments, through: :file_preview_variant_records, source: :image_attachment | ||
has_many :file_preview_variant_blobs, through: :file_preview_variant_attachments, source: :blob | ||
|
||
class << self | ||
def find_by_blob!(key:) | ||
blob_associations = %i[ | ||
file_blob | ||
file_preview_blob | ||
file_variant_blobs | ||
file_preview_variant_blobs | ||
] | ||
|
||
left_joins(*blob_associations) | ||
.where(file_blob: {key:}) | ||
.or(where(file_variant_blobs: {key:})) | ||
.or(where(file_preview_blob: {key:})) | ||
.or(where(file_preview_variant_blobs: {key:})) | ||
.distinct.sole | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This huge block is so we can find a document through a local authority and prevent someone using access in one local authority to access a document in another authority. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing an |
||
def tags(key) | ||
case key.to_s | ||
when "plans" | ||
|
@@ -359,7 +401,7 @@ def representation_url(transformations = {resize_to_limit: [1000, 1000]}) | |
private | ||
|
||
def routes | ||
@_routes ||= Routing.new | ||
@_routes ||= Routing.new(local_authority.subdomain) | ||
end | ||
|
||
def no_open_replacement_request | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsCore | ||
module Routing | ||
extend ActiveSupport::Concern | ||
|
||
class LocalAuthoritySubdomain | ||
class << self | ||
def matches?(request) | ||
LocalAuthority.pluck(:subdomain).include?(request.subdomain) | ||
end | ||
end | ||
end | ||
|
||
class ConfigSubdomain | ||
class << self | ||
def matches?(request) | ||
request.subdomain == "config" | ||
end | ||
end | ||
end | ||
|
||
class DeviseSubdomain | ||
class << self | ||
def matches?(request) | ||
ConfigSubdomain.matches?(request) || LocalAuthoritySubdomain.matches?(request) | ||
end | ||
end | ||
end | ||
|
||
class UploadsSubdomain | ||
class << self | ||
def matches?(request) | ||
request.subdomain == "uploads" | ||
end | ||
end | ||
end | ||
|
||
def local_authority_subdomain(&) | ||
constraints(LocalAuthoritySubdomain, &) | ||
end | ||
|
||
def config_subdomain(&) | ||
constraints(ConfigSubdomain, &) | ||
end | ||
|
||
def devise_subdomain(&) | ||
constraints(DeviseSubdomain, &) | ||
end | ||
|
||
def uploads_subdomain(&) | ||
constraints(UploadsSubdomain, &) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module BopsUploads | ||
class BlobsController < ApplicationController | ||
def show | ||
serve_file(blob_path, content_type:, disposition:) | ||
rescue Errno::ENOENT | ||
head :not_found | ||
end | ||
|
||
private | ||
|
||
def blob_path | ||
@service.path_for(@blob.key) | ||
end | ||
|
||
def forcibly_serve_as_binary? | ||
ActiveStorage.content_types_to_serve_as_binary.include?(@blob.content_type) | ||
end | ||
|
||
def allowed_inline? | ||
ActiveStorage.content_types_allowed_inline.include?(@blob.content_type) | ||
end | ||
|
||
def content_type | ||
forcibly_serve_as_binary? ? ActiveStorage.binary_content_type : @blob.content_type | ||
end | ||
|
||
def disposition | ||
if forcibly_serve_as_binary? || !allowed_inline? | ||
:attachment | ||
else | ||
:inline | ||
end | ||
end | ||
|
||
def serve_file(path, content_type:, disposition:) | ||
::Rack::Files.new(nil).serving(request, path).tap do |(status, headers, body)| | ||
self.status = status | ||
self.response_body = body | ||
|
||
headers.each do |name, value| | ||
response.headers[name] = value | ||
end | ||
|
||
response.headers.except!("X-Cascade", "x-cascade") if status == 416 | ||
response.headers["Content-Type"] = content_type || DEFAULT_SEND_FILE_TYPE | ||
response.headers["Content-Disposition"] = disposition || DEFAULT_SEND_FILE_DISPOSITION | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to deploy this to staging so we can test it but don't want to break the app so this controls whether it uses the current urls or the new urls.