mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-24 08:06:28 +01:00
Compare commits
2 Commits
79130a6dd7
...
4c024a24f0
| Author | SHA1 | Date | |
|---|---|---|---|
| 4c024a24f0 | |||
| db3d8f827f |
@ -34,8 +34,23 @@ through the identitytoolkit REST API to anyone holding the Web API key, and
|
||||
that key ships inside the Android APKs. Pinning the uid means a stranger who
|
||||
registers an account still reads and writes nothing.
|
||||
|
||||
To deploy, substitute the real uid (Firebase console → Authentication → Users
|
||||
→ User UID) and paste into **Realtime Database → Rules → Publish**.
|
||||
The uid here is a **placeholder on purpose** — this repo is public, and the
|
||||
uid is the one value that must not appear in it. Publishing this file
|
||||
verbatim yields rules matching no account, which locks every app out of the
|
||||
project until someone notices; the failure looks like a permission error
|
||||
from every device at once.
|
||||
|
||||
So do not hand-substitute. Render it:
|
||||
|
||||
```bash
|
||||
./render_rules.sh # prints deployable rules, real uid filled in
|
||||
./render_rules.sh --check # validates without printing the uid
|
||||
```
|
||||
|
||||
It reads the uid from `~/.config/crdt-sync/firebase.json` and refuses to emit
|
||||
anything still containing the placeholder, missing `auth != null`, missing a
|
||||
uid pin, or not parsing as JSON. Paste its output into **Realtime Database →
|
||||
Rules → Publish**.
|
||||
|
||||
Verify the rules are live — an unauthenticated request must be refused:
|
||||
|
||||
@ -53,7 +68,7 @@ outside version control:
|
||||
|---|---|
|
||||
| `firebase.json` | `apiKey`, `databaseUrl`, `projectId`, `uid`, `email` |
|
||||
| `password` | the sync account's password, no trailing newline |
|
||||
| `database.rules.json` | the deployed rules, with the real uid filled in |
|
||||
| `database.rules.json` | the rendered rules, real uid filled in (see `render_rules.sh`) |
|
||||
|
||||
The Web API key is **not** a secret — it is a public project identifier that
|
||||
ships inside client apps. The password and refresh tokens are the real
|
||||
|
||||
85
firebase/render_rules.sh
Executable file
85
firebase/render_rules.sh
Executable file
@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
|
||||
# ============================================================================
|
||||
# Render deployable Realtime Database rules from the public template.
|
||||
#
|
||||
# The template in this repo carries a placeholder on purpose: the repository
|
||||
# is public and the uid is the one value that must not appear in it. That
|
||||
# leaves a trap -- publishing the template verbatim yields rules that match
|
||||
# no account, which locks every app out of the shared project until someone
|
||||
# notices. This script closes it: it substitutes the real uid from the local
|
||||
# config and refuses to emit anything still containing a placeholder.
|
||||
# ============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_NAME="$(basename "$0")"
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
readonly SCRIPT_NAME
|
||||
readonly SCRIPT_DIR
|
||||
readonly TEMPLATE="$SCRIPT_DIR/database.rules.json"
|
||||
readonly CONFIG_DIR="${CRDT_SYNC_CONFIG_DIR:-$HOME/.config/crdt-sync}"
|
||||
readonly PLACEHOLDER="YOUR_FIREBASE_UID"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $SCRIPT_NAME [--check]"
|
||||
echo " (no args) print deployable rules with the real uid substituted"
|
||||
echo " --check verify the rendered rules are sane; print nothing"
|
||||
exit 0
|
||||
}
|
||||
|
||||
# The uid lives in firebase.json, the single source of truth for this machine.
|
||||
read_uid() {
|
||||
local config="$CONFIG_DIR/firebase.json"
|
||||
if [[ ! -r "$config" ]]; then
|
||||
echo "Error: $config is missing or unreadable." >&2
|
||||
echo "It holds the uid these rules pin; see README.md." >&2
|
||||
exit 1
|
||||
fi
|
||||
python3 -c "import json,sys; print(json.load(open(sys.argv[1]))['uid'])" \
|
||||
"$config"
|
||||
}
|
||||
|
||||
render() {
|
||||
local uid="$1"
|
||||
sed "s/$PLACEHOLDER/$uid/g" "$TEMPLATE"
|
||||
}
|
||||
|
||||
# Fail closed: a rules file that still names the placeholder, or that any
|
||||
# unauthenticated request would satisfy, must never reach the console.
|
||||
validate() {
|
||||
local rendered="$1"
|
||||
if [[ "$rendered" == *"$PLACEHOLDER"* ]]; then
|
||||
echo "Error: rendered rules still contain $PLACEHOLDER." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! grep -q 'auth != null' <<< "$rendered"; then
|
||||
echo "Error: rendered rules do not require authentication." >&2
|
||||
exit 1
|
||||
fi
|
||||
if ! grep -q "auth.uid ===" <<< "$rendered"; then
|
||||
echo "Error: rendered rules do not pin a uid; 'auth != null' alone" >&2
|
||||
echo "lets any stranger who self-registers read and write." >&2
|
||||
exit 1
|
||||
fi
|
||||
python3 -c "import json,sys; json.loads(sys.stdin.read())" <<< "$rendered"
|
||||
}
|
||||
|
||||
main() {
|
||||
local check_only=false
|
||||
[[ "${1:-}" == "--check" ]] && check_only=true
|
||||
[[ "${1:-}" == "-h" || "${1:-}" == "--help" ]] && usage
|
||||
|
||||
local uid rendered
|
||||
uid="$(read_uid)"
|
||||
rendered="$(render "$uid")"
|
||||
validate "$rendered"
|
||||
|
||||
if [[ "$check_only" == true ]]; then
|
||||
echo "Rules render cleanly and pin a uid." >&2
|
||||
else
|
||||
printf '%s\n' "$rendered"
|
||||
fi
|
||||
}
|
||||
|
||||
main "$@"
|
||||
@ -163,6 +163,7 @@ class _SyncSettingsScreenState extends State<SyncSettingsScreen> {
|
||||
connected: _firebaseConnected,
|
||||
busy: _busy,
|
||||
googleAvailable: _googleAvailable,
|
||||
googleUnavailableReason: widget.googleUnavailableReason,
|
||||
emailController: _emailController,
|
||||
passwordController: _passwordController,
|
||||
onConnectPassword: _connectFirebase,
|
||||
|
||||
@ -10,6 +10,7 @@ class _FirebaseSection extends StatelessWidget {
|
||||
required this.connected,
|
||||
required this.busy,
|
||||
required this.googleAvailable,
|
||||
required this.googleUnavailableReason,
|
||||
required this.emailController,
|
||||
required this.passwordController,
|
||||
required this.onConnectPassword,
|
||||
@ -20,6 +21,7 @@ class _FirebaseSection extends StatelessWidget {
|
||||
final bool connected;
|
||||
final bool busy;
|
||||
final bool googleAvailable;
|
||||
final String? googleUnavailableReason;
|
||||
final TextEditingController emailController;
|
||||
final TextEditingController passwordController;
|
||||
final VoidCallback onConnectPassword;
|
||||
@ -54,6 +56,25 @@ class _FirebaseSection extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
] else if (googleUnavailableReason != null) ...[
|
||||
// Disabled, not hidden. A null onPressed cannot be tapped, so this
|
||||
// is not the "control that can never succeed" trap -- that one
|
||||
// looks live and reports a fake cancellation. This one says why,
|
||||
// which beats a screen that silently offers less on one platform.
|
||||
Align(
|
||||
alignment: Alignment.centerLeft,
|
||||
child: FilledButton.icon(
|
||||
onPressed: null,
|
||||
icon: const Icon(Icons.account_circle),
|
||||
label: const Text('Sign in with Google'),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
googleUnavailableReason!,
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
ExpansionTile(
|
||||
initiallyExpanded: !googleAvailable,
|
||||
|
||||
@ -19,6 +19,7 @@ class SyncSettingsScreen extends StatefulWidget {
|
||||
required this.firebaseFactory,
|
||||
this.googleFirebaseFactory,
|
||||
this.googleAvailable,
|
||||
this.googleUnavailableReason,
|
||||
this.backup,
|
||||
super.key,
|
||||
});
|
||||
@ -59,6 +60,17 @@ class SyncSettingsScreen extends StatefulWidget {
|
||||
/// unsupported even though a factory is wired.
|
||||
final bool? googleAvailable;
|
||||
|
||||
/// Why Google sign-in is unavailable here, shown under a *disabled* button.
|
||||
///
|
||||
/// Null hides the button entirely, which stays the default. Supplying a
|
||||
/// reason trades a silently-absent control for one that explains itself --
|
||||
/// worth it where a platform genuinely cannot support the flow, and where
|
||||
/// its absence would otherwise read as a bug.
|
||||
///
|
||||
/// Not the same as a button that can never succeed: this one cannot be
|
||||
/// tapped at all, so it can never report a misleading "cancelled".
|
||||
final String? googleUnavailableReason;
|
||||
|
||||
/// Optional local export/import section. Null omits the "Backup" section
|
||||
/// entirely (diet_guard, wake_alarm have no local backup format).
|
||||
final BackupSlot? backup;
|
||||
|
||||
@ -5,7 +5,7 @@ description: >-
|
||||
injected closures -- no keystore or platform plugin access lives in this
|
||||
package itself.
|
||||
publish_to: 'none'
|
||||
version: 0.2.0
|
||||
version: 0.3.0
|
||||
|
||||
environment:
|
||||
sdk: ^3.12.2
|
||||
|
||||
@ -141,5 +141,55 @@ void main() {
|
||||
expect(find.text('Sign in with Google'), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('a reason renders a disabled button that explains itself', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_wrap(
|
||||
SyncSettingsScreen(
|
||||
accountLoader: () async => null,
|
||||
accountSaver: (_) async {},
|
||||
accountClearer: () async {},
|
||||
sessionProbe: () async => false,
|
||||
firebaseFactory: () async => null,
|
||||
googleFirebaseFactory: () async => _fakeClient(),
|
||||
googleAvailable: false,
|
||||
googleUnavailableReason: 'Not available on this platform.',
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Sign in with Google'), findsOneWidget);
|
||||
expect(find.text('Not available on this platform.'), findsOneWidget);
|
||||
|
||||
// Disabled, so it cannot report a misleading "cancelled".
|
||||
final button = tester.widget<FilledButton>(
|
||||
find.widgetWithText(FilledButton, 'Sign in with Google'),
|
||||
);
|
||||
expect(button.onPressed, isNull);
|
||||
});
|
||||
|
||||
testWidgets('no reason keeps the button hidden, as before', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.pumpWidget(
|
||||
_wrap(
|
||||
SyncSettingsScreen(
|
||||
accountLoader: () async => null,
|
||||
accountSaver: (_) async {},
|
||||
accountClearer: () async {},
|
||||
sessionProbe: () async => false,
|
||||
firebaseFactory: () async => null,
|
||||
googleFirebaseFactory: () async => _fakeClient(),
|
||||
googleAvailable: false,
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('Sign in with Google'), findsNothing);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user