Skip to content

Commit 8d18f52

Browse files
author
Lenardt Gerhardts
committed
Added whitelist option
1 parent 18769eb commit 8d18f52

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Here are the available configuration options and their default values:
2626
| `allow_exec` | false | Allow usage of the `sqlpage.exec` function. Do this only if all users with write access to sqlpage query files and to the optional `sqlpage_files` table on the database are trusted. |
2727
| `max_uploaded_file_size` | 5242880 | Maximum size of forms and uploaded files in bytes. Defaults to 5 MiB. |
2828
| `oidc_protected_paths` | `["/"]` | A list of URL prefixes that should be protected by OIDC authentication. By default, all paths are protected (`["/"]`). If you want to make some pages public, you can restrict authentication to a sub-path, for instance `["/admin", "/users/settings"]`. |
29+
| `oidc_public_paths` | `[]` | A list of URL prefixes that should be publicly available. By default, no paths are publicly accessible (`[]`). If you want to make some pages public, you can bypass authentication for a sub-path, for instance `["/public/", "/assets/"]`. Keep in mind that without the closing backslashes, that any directory or file starting with `public` or `assets` will be publicly available. This will also overwrite any protected path restriction. If you have a private path `/private` and you define the public path `/private/public/` everything in `/private/public/` will be publicly accessible, while everything else in private will still need authentication. You will not be able to define a private path inside a public path. |
2930
| `oidc_issuer_url` | | The base URL of the [OpenID Connect provider](#openid-connect-oidc-authentication). Required for enabling Single Sign-On. |
3031
| `oidc_client_id` | sqlpage | The ID that identifies your SQLPage application to the OIDC provider. You get this when registering your app with the provider. |
3132
| `oidc_client_secret` | | The secret key for your SQLPage application. Keep this confidential as it allows your app to authenticate with the OIDC provider. |

src/app_config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,21 @@ pub struct AppConfig {
206206
/// If you specify a list of prefixes, only requests whose path starts with one of the prefixes will require authentication.
207207
/// For example, if you set this to `["/private"]`, then requests to `/private/some_page.sql` will require authentication,
208208
/// but requests to `/index.sql` will not.
209+
/// NOTE: `OIDC_PUBLIC_PATHS` takes precedence over `OIDC_PROTECTED_PATHS`.
210+
/// For example, if you have `["/private"]` on the `protected_paths` like before, but also `["/private/public"]` on the `public_paths`, then `/private` requires authentication, but `/private/public` requires not authentication.
211+
/// You cannot make a path inside a public path private again. So expanding the previous example, if you now add `/private/public/private_again`, then this path will still be accessible.
209212
#[serde(default = "default_oidc_protected_paths")]
210213
pub oidc_protected_paths: Vec<String>,
211214

215+
/// Defines a list of path prefixes that should be ignored by OIDC authentication
216+
/// By default, now paths will be ignored.
217+
/// If you specify a list of prefixes, requests whose path starts with one of the prefixes will be not require authentication.
218+
/// For example, if set this to `["/public"]`, then requests to `/public/some_page.sql` will not require authentication,
219+
/// but requests to `/index.sql` will.
220+
/// If you still want to make `/index.sql` public, but leave the rest of the folder protected, then append `["/index.sql"]`. But keep in mind that if you have a directory that starts with `index.sql` that it will also be public.
221+
#[serde(default = "default_oidc_public_paths")]
222+
pub oidc_public_paths: Vec<String>,
223+
212224
/// A domain name to use for the HTTPS server. If this is set, the server will perform all the necessary
213225
/// steps to set up an HTTPS server automatically. All you need to do is point your domain name to the
214226
/// server's IP address.
@@ -558,6 +570,10 @@ fn default_oidc_protected_paths() -> Vec<String> {
558570
vec!["/".to_string()]
559571
}
560572

573+
fn default_oidc_public_paths() -> Vec<String> {
574+
vec![]
575+
}
576+
561577
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone, Copy, Eq, Default)]
562578
#[serde(rename_all = "lowercase")]
563579
pub enum DevOrProd {

src/webserver/oidc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ pub struct OidcConfig {
4949
pub client_id: String,
5050
pub client_secret: String,
5151
pub protected_paths: Vec<String>,
52+
pub public_paths: Vec<String>,
5253
pub app_host: String,
5354
pub scopes: Vec<Scope>,
5455
}
@@ -62,6 +63,7 @@ impl TryFrom<&AppConfig> for OidcConfig {
6263
"The \"oidc_client_secret\" setting is required to authenticate with the OIDC provider",
6364
))?;
6465
let protected_paths: Vec<String> = config.oidc_protected_paths.clone();
66+
let public_paths: Vec<String> = config.oidc_public_paths.clone();
6567

6668
let app_host = get_app_host(config);
6769

@@ -70,6 +72,7 @@ impl TryFrom<&AppConfig> for OidcConfig {
7072
client_id: config.oidc_client_id.clone(),
7173
client_secret: client_secret.clone(),
7274
protected_paths,
75+
public_paths,
7376
scopes: config
7477
.oidc_scopes
7578
.split_whitespace()
@@ -203,6 +206,20 @@ where
203206
return self.handle_oidc_callback(request);
204207
}
205208

209+
if self
210+
.oidc_state
211+
.config
212+
.public_paths
213+
.iter()
214+
.any(|path| request.path().starts_with(path))
215+
{
216+
log::debug!(
217+
"The request path {} is in a public path, skipping OIDC authentication",
218+
request.path()
219+
);
220+
return Box::pin(self.service.call(request));
221+
}
222+
206223
if !self
207224
.oidc_state
208225
.config

0 commit comments

Comments
 (0)