@@ -374,6 +374,8 @@ inline void THROW_ERR_SQLITE_ERROR(Isolate* isolate, Database* db) { |
374 | 374 | } |
375 | 375 | } |
376 | 376 |
|
| 377 | + if (db->ThrowIfPermissionDenied()) return; |
| 378 | + |
377 | 379 | Local<Object> e; |
378 | 380 | if (CreateSQLiteError(isolate, db->Connection()).ToLocal(&e)) { |
379 | 381 | isolate->ThrowException(e); |
@@ -625,6 +627,7 @@ class BackupJob : public ThreadPoolWork { |
625 | 627 | std::string source_db, |
626 | 628 | std::string destination_name, |
627 | 629 | std::string dest_db, |
| 630 | + std::shared_ptr<SQLitePermissionVFS> permission_vfs, |
628 | 631 | int pages, |
629 | 632 | Local<Function> progressFunc) |
630 | 633 | : ThreadPoolWork(env, ThreadPoolWorkType::kSQLiteBackup), |
@@ -633,7 +636,8 @@ class BackupJob : public ThreadPoolWork { |
633 | 636 | pages_(pages), |
634 | 637 | source_db_(std::move(source_db)), |
635 | 638 | destination_name_(std::move(destination_name)), |
636 | | - dest_db_(std::move(dest_db)) { |
| 639 | + dest_db_(std::move(dest_db)), |
| 640 | + permission_vfs_(std::move(permission_vfs)) { |
637 | 641 | resolver_.Reset(env->isolate(), resolver); |
638 | 642 | progressFunc_.Reset(env->isolate(), progressFunc); |
639 | 643 | } |
@@ -645,13 +649,16 @@ class BackupJob : public ThreadPoolWork { |
645 | 649 | backup_status_ = sqlite3_open_v2( |
646 | 650 | destination_name_.c_str(), |
647 | 651 | &dest_raw, |
648 | | - SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, |
649 | | - nullptr); |
| 652 | + SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | |
| 653 | + (permission_vfs_ == nullptr ? SQLITE_OPEN_URI : 0), |
| 654 | + permission_vfs_ == nullptr ? nullptr : permission_vfs_->name().c_str()); |
650 | 655 | dest_.reset(dest_raw); |
651 | 656 | Local<Promise::Resolver> resolver = |
652 | 657 | Local<Promise::Resolver>::New(env()->isolate(), resolver_); |
653 | 658 | if (backup_status_ != SQLITE_OK) { |
654 | | - Settle(resolver, CreateSQLiteError(isolate, dest_.get()), {}); |
| 659 | + Settle(resolver, |
| 660 | + PermissionErrorOr(CreateSQLiteError(isolate, dest_.get())), |
| 661 | + {}); |
655 | 662 | return; |
656 | 663 | } |
657 | 664 |
|
@@ -682,7 +689,9 @@ class BackupJob : public ThreadPoolWork { |
682 | 689 |
|
683 | 690 | if (!(backup_status_ == SQLITE_OK || backup_status_ == SQLITE_DONE || |
684 | 691 | backup_status_ == SQLITE_BUSY || backup_status_ == SQLITE_LOCKED)) { |
685 | | - Settle(resolver, CreateSQLiteError(isolate, backup_status_), {}); |
| 692 | + Settle(resolver, |
| 693 | + PermissionErrorOr(CreateSQLiteError(isolate, backup_status_)), |
| 694 | + {}); |
686 | 695 | return; |
687 | 696 | } |
688 | 697 |
|
@@ -749,9 +758,21 @@ class BackupJob : public ThreadPoolWork { |
749 | 758 | backup_status_ = sqlite3_errcode(dest_.get()); |
750 | 759 | dest_.reset(); |
751 | 760 | } |
| 761 | + |
| 762 | + permission_vfs_.reset(); |
752 | 763 | } |
753 | 764 |
|
754 | 765 | private: |
| 766 | + MaybeLocal<Value> PermissionErrorOr(MaybeLocal<Object> sqlite_error) { |
| 767 | + if (permission_vfs_ != nullptr) { |
| 768 | + if (auto denied = permission_vfs_->TakeDenied(backup_status_)) { |
| 769 | + return permission::CreateAccessDeniedError( |
| 770 | + env(), denied->scope, denied->path); |
| 771 | + } |
| 772 | + } |
| 773 | + return sqlite_error; |
| 774 | + } |
| 775 | + |
755 | 776 | // The single exit point of a backup. Rejects with `error` if it is set, |
756 | 777 | // otherwise resolves with `value` if it is set. Neither being set means an |
757 | 778 | // exception is already pending. Deletes the job. |
@@ -781,6 +802,7 @@ class BackupJob : public ThreadPoolWork { |
781 | 802 | std::string source_db_; |
782 | 803 | std::string destination_name_; |
783 | 804 | std::string dest_db_; |
| 805 | + std::shared_ptr<SQLitePermissionVFS> permission_vfs_; |
784 | 806 | }; |
785 | 807 |
|
786 | 808 | UserDefinedFunction::UserDefinedFunction(Environment* env, |
@@ -1620,12 +1642,47 @@ void Database::MemoryInfo(MemoryTracker* tracker) const { |
1620 | 1642 | "open_config", sizeof(open_config_), "DatabaseOpenConfiguration"); |
1621 | 1643 | } |
1622 | 1644 |
|
| 1645 | +bool Database::ThrowIfPermissionDenied() { |
| 1646 | + if (permission_vfs_ == nullptr) return false; |
| 1647 | + auto denied = permission_vfs_->TakeDenied(sqlite3_errcode(connection_.get())); |
| 1648 | + if (!denied) return false; |
| 1649 | + permission::Permission::ThrowAccessDenied(env(), denied->scope, denied->path); |
| 1650 | + return true; |
| 1651 | +} |
| 1652 | + |
1623 | 1653 | bool Database::Open() { |
1624 | 1654 | if (IsOpen()) { |
1625 | 1655 | THROW_ERR_INVALID_STATE(env(), "database is already open"); |
1626 | 1656 | return false; |
1627 | 1657 | } |
1628 | 1658 |
|
| 1659 | + // An anonymous temporary database has no path for the VFS to check. |
| 1660 | + if (open_config_.location().empty()) { |
| 1661 | + THROW_IF_INSUFFICIENT_PERMISSIONS( |
| 1662 | + env(), permission::PermissionScope::kFileSystemRead, "", false); |
| 1663 | + if (!open_config_.get_read_only()) { |
| 1664 | + THROW_IF_INSUFFICIENT_PERMISSIONS( |
| 1665 | + env(), permission::PermissionScope::kFileSystemWrite, "", false); |
| 1666 | + } |
| 1667 | + } |
| 1668 | + |
| 1669 | + if (env()->permission()->enabled()) { |
| 1670 | + // Connections of the same Environment share one permission VFS. |
| 1671 | + BindingData* binding = |
| 1672 | + env()->principal_realm()->GetBindingData<BindingData>(); |
| 1673 | + if (binding != nullptr) permission_vfs_ = binding->permission_vfs.lock(); |
| 1674 | + if (permission_vfs_ == nullptr) { |
| 1675 | + permission_vfs_ = std::make_shared<SQLitePermissionVFS>(env()); |
| 1676 | + if (!permission_vfs_->Register()) { |
| 1677 | + THROW_ERR_SQLITE_ERROR(env()->isolate(), |
| 1678 | + "Unable to register the SQLite permission VFS"); |
| 1679 | + permission_vfs_.reset(); |
| 1680 | + return false; |
| 1681 | + } |
| 1682 | + if (binding != nullptr) binding->permission_vfs = permission_vfs_; |
| 1683 | + } |
| 1684 | + } |
| 1685 | + |
1629 | 1686 | // sqlite3_open_v2() assigns a database handle even when it fails. Such a |
1630 | 1687 | // handle is in a "sick" state and may only be used to retrieve the error |
1631 | 1688 | // and must then be released with sqlite3_close(). Close and reset the |
@@ -1635,19 +1692,23 @@ bool Database::Open() { |
1635 | 1692 | auto reset_connection_on_failure = OnScopeLeave([&]() { |
1636 | 1693 | if (!opened) { |
1637 | 1694 | connection_.reset(); |
| 1695 | + permission_vfs_.reset(); |
1638 | 1696 | } |
1639 | 1697 | }); |
1640 | 1698 |
|
1641 | 1699 | // TODO(cjihrig): Support additional flags. |
1642 | | - int default_flags = SQLITE_OPEN_URI; |
| 1700 | + // TODO(araujogui): Support URI filenames with the permission model. A URI |
| 1701 | + // could select another VFS, bypassing the permission VFS. |
| 1702 | + int default_flags = permission_vfs_ == nullptr ? SQLITE_OPEN_URI : 0; |
1643 | 1703 | int flags = open_config_.get_read_only() |
1644 | 1704 | ? SQLITE_OPEN_READONLY |
1645 | 1705 | : SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE; |
1646 | 1706 | sqlite3* raw_conn = nullptr; |
1647 | | - int r = sqlite3_open_v2(open_config_.location().c_str(), |
1648 | | - &raw_conn, |
1649 | | - flags | default_flags, |
1650 | | - nullptr); |
| 1707 | + int r = sqlite3_open_v2( |
| 1708 | + open_config_.location().c_str(), |
| 1709 | + &raw_conn, |
| 1710 | + flags | default_flags, |
| 1711 | + permission_vfs_ == nullptr ? nullptr : permission_vfs_->name().c_str()); |
1651 | 1712 | connection_.reset(raw_conn); |
1652 | 1713 | CHECK_ERROR_OR_THROW(env()->isolate(), this, r, SQLITE_OK, false); |
1653 | 1714 |
|
@@ -1881,10 +1942,22 @@ std::optional<std::string> ValidateDatabasePath(Environment* env, |
1881 | 1942 | constexpr auto has_null_bytes = [](std::string_view str) { |
1882 | 1943 | return str.find('\0') != std::string_view::npos; |
1883 | 1944 | }; |
| 1945 | + // Without SQLITE_OPEN_URI, SQLite would open a file named "file:...". |
| 1946 | + const auto reject_uri = [&](std::string value) -> std::optional<std::string> { |
| 1947 | + if (env->permission()->enabled() && value.starts_with("file:")) { |
| 1948 | + THROW_ERR_INVALID_ARG_VALUE( |
| 1949 | + env->isolate(), |
| 1950 | + "The \"%s\" argument must not be a URL or SQLite URI filename when " |
| 1951 | + "the permission model is enabled.", |
| 1952 | + field_name); |
| 1953 | + return std::nullopt; |
| 1954 | + } |
| 1955 | + return value; |
| 1956 | + }; |
1884 | 1957 | if (path->IsString()) { |
1885 | 1958 | Utf8Value location(env->isolate(), path.As<String>()); |
1886 | 1959 | if (!has_null_bytes(location.ToStringView())) { |
1887 | | - return location.ToString(); |
| 1960 | + return reject_uri(location.ToString()); |
1888 | 1961 | } |
1889 | 1962 | } else if (path->IsUint8Array()) { |
1890 | 1963 | Local<Uint8Array> buffer = path.As<Uint8Array>(); |
@@ -1893,7 +1966,8 @@ std::optional<std::string> ValidateDatabasePath(Environment* env, |
1893 | 1966 | auto data = |
1894 | 1967 | static_cast<const uint8_t*>(buffer->Buffer()->Data()) + byteOffset; |
1895 | 1968 | if (std::find(data, data + byteLength, 0) == data + byteLength) { |
1896 | | - return std::string(reinterpret_cast<const char*>(data), byteLength); |
| 1969 | + return reject_uri( |
| 1970 | + std::string(reinterpret_cast<const char*>(data), byteLength)); |
1897 | 1971 | } |
1898 | 1972 | } else if (path->IsObject()) { // When is URL |
1899 | 1973 | auto url = path.As<Object>(); |
@@ -1913,7 +1987,7 @@ std::optional<std::string> ValidateDatabasePath(Environment* env, |
1913 | 1987 | return std::nullopt; |
1914 | 1988 | } |
1915 | 1989 |
|
1916 | | - return location_value.ToString(); |
| 1990 | + return reject_uri(location_value.ToString()); |
1917 | 1991 | } |
1918 | 1992 | } |
1919 | 1993 | } |
@@ -2120,6 +2194,7 @@ void Database::Close(const FunctionCallbackInfo<Value>& args) { |
2120 | 2194 | int r = sqlite3_close_v2(db->connection_.get()); |
2121 | 2195 | CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void()); |
2122 | 2196 | db->connection_.release(); |
| 2197 | + db->permission_vfs_.reset(); |
2123 | 2198 | // Backups can defer SQLite destruction until after the connection is closed. |
2124 | 2199 | db->user_defined_functions_.clear(); |
2125 | 2200 | } |
@@ -2963,6 +3038,7 @@ void Backup(const FunctionCallbackInfo<Value>& args) { |
2963 | 3038 | std::move(source_db), |
2964 | 3039 | dest_path.value(), |
2965 | 3040 | std::move(dest_db), |
| 3041 | + db->PermissionVFS(), |
2966 | 3042 | rate, |
2967 | 3043 | progressFunc); |
2968 | 3044 | db->AddBackup(job); |
@@ -3249,9 +3325,8 @@ void Database::SetAuthorizer(const FunctionCallbackInfo<Value>& args) { |
3249 | 3325 | Isolate* isolate = env->isolate(); |
3250 | 3326 |
|
3251 | 3327 | if (args[0]->IsNull()) { |
3252 | | - // Clear the authorizer |
3253 | | - sqlite3_set_authorizer(db->connection_.get(), nullptr, nullptr); |
3254 | 3328 | db->object()->SetInternalField(kAuthorizerCallback, Null(isolate)); |
| 3329 | + sqlite3_set_authorizer(db->connection_.get(), nullptr, nullptr); |
3255 | 3330 | return; |
3256 | 3331 | } |
3257 | 3332 |
|
|