11#include < Storages/StorageBuffer.h>
22
3+ #include < Access/Common/AccessFlags.h>
34#include < Analyzer/TableNode.h>
45#include < Analyzer/Utils.h>
56#include < Interpreters/Context.h>
@@ -290,6 +291,7 @@ void StorageBuffer::read(
290291
291292 if (auto destination = getDestinationTable ())
292293 {
294+ local_context->checkAccess (AccessType::SELECT, destination->getStorageID (), column_names);
293295 auto destination_lock
294296 = destination->lockForShare (local_context->getCurrentQueryId (), local_context->getSettingsRef ()[Setting::lock_acquire_timeout]);
295297
@@ -642,13 +644,14 @@ static void appendBlock(LoggerPtr log, const Block & from, Block & to)
642644}
643645
644646
645- class BufferSink : public SinkToStorage
647+ class BufferSink : public SinkToStorage , WithContext
646648{
647649public:
648650 explicit BufferSink (
649651 StorageBuffer & storage_,
650- const StorageMetadataPtr & metadata_snapshot_)
651- : SinkToStorage(std::make_shared<const Block>(metadata_snapshot_->getSampleBlock ()))
652+ const StorageMetadataPtr & metadata_snapshot_,
653+ const ContextPtr & context_)
654+ : SinkToStorage(std::make_shared<const Block>(metadata_snapshot_->getSampleBlock ())), WithContext(context_)
652655 , storage(storage_)
653656 , metadata_snapshot(metadata_snapshot_)
654657 {
@@ -669,6 +672,7 @@ class BufferSink : public SinkToStorage
669672 StoragePtr destination = storage.getDestinationTable ();
670673 if (destination)
671674 {
675+ getContext ()->checkAccess (AccessType::INSERT, destination->getStorageID ());
672676 destination = DatabaseCatalog::instance ().tryGetTable (storage.destination_id , storage.getContext ());
673677 if (destination.get () == &storage)
674678 throw Exception (ErrorCodes::INFINITE_LOOP, " Destination table is myself. Write will cause infinite loop." );
@@ -766,9 +770,9 @@ class BufferSink : public SinkToStorage
766770};
767771
768772
769- SinkToStoragePtr StorageBuffer::write (const ASTPtr & /* query*/ , const StorageMetadataPtr & metadata_snapshot, ContextPtr /* context */ , bool /* async_insert*/ )
773+ SinkToStoragePtr StorageBuffer::write (const ASTPtr & /* query*/ , const StorageMetadataPtr & metadata_snapshot, ContextPtr local_context , bool /* async_insert*/ )
770774{
771- return std::make_shared<BufferSink>(*this , metadata_snapshot);
775+ return std::make_shared<BufferSink>(*this , metadata_snapshot, local_context );
772776}
773777
774778
0 commit comments