feat(embed): add raw HTML embed fence with bleach sanitization
- Add embed markdown fence: user pastes raw embed HTML code
(from Canva/YouTube/Google Docs Share→Embed) into lesson markdown
and slide content. Backend sanitizes via bleach (whitelist
tags/attrs/styles) + checks iframe src against domain blacklist
(SSRF prevention). Frontend renders iframe directly — no lazy
action needed.
- Backend: _process_embed_embeds + _sanitize_embed_html in
lesson_service.py, applied to lesson_content, exercise,
lesson_info, and slide loop. Graceful fallback if tinycss2
missing (CSS unsanitized but tags/attrs still stripped).
- Tests: 9 pytest cases (Canva/YouTube HTML, script stripping,
onclick stripping, blocked domain, non-https iframe, empty,
unchanged, dangerous style).
- Frontend: remove renderEmbedEmbeds.ts + wire-up + .generic-embed
CSS (URL-only approach from earlier iteration, superseded).
Keep .embed-error CSS for error messages.
- Example: update test_slides.md with raw HTML Canva embed (slide)
+ YouTube embed (body).
- Deps: bleach>=6.0.0, tinycss2>=1.2.0 in requirements.txt.
- Docs: consolidate 4 plan files into docs/06-embed-content.md.
2026-07-19 15:39:38 +07:00
|
|
|
import pytest
|
feat(teacher): manajemen akun guru canonical + first-run otomatis; cabut backend CSV (PG only)
- feat: ./elemes.sh teacher — upsert satu akun guru canonical (prompt nama default TEACHER_NAME, token tersembunyi via stdin, rotasi token saat guru sudah ada, idempotent); TEACHER_NAME/TEACHER_TOKEN di .env
- feat: first-run otomatis — db_init jalankan alembic upgrade head + bootstrap guru bila TEACHER_TOKEN terisi (run/runbuild/runclearbuild)
- refactor: backend CSV dicabut penuh — hapus mount tokens_siswa.csv, TOKENS_FILE, STORAGE_BACKEND=csv, csv_backend.py, csv_importer.py, generate_tokens.py, script migrate/verify/export CSV, command generatetoken/dbimport/dbverify/dbexport/synclessons; storage fail-loud postgresql-only
- fix: dbrestore gagal diam-diam saat restore ke DB berisi data (dump tanpa --clean) — dbbackup kini --clean --if-exists + dbrestore reset schema public; roundtrip terverifikasi
- test: suite kontrak PG-native + route auth/progress integrasi PG; 183 passed container, 96 passed host, frontend 91 passed
- docs: README, documentation.md, docs/01/02/11, load-test/README disinkronkan ke realita PostgreSQL-only; proposal.md dipertahankan (historis)
- chore: load-test token sintetis di-seed ke PG (content_parser), config.py bersih TOKENS_FILE, .dockerignore/.gitignore dibersihkan
2026-08-09 11:21:08 +07:00
|
|
|
from services.lesson_service import _process_embed_embeds
|
feat(embed): add raw HTML embed fence with bleach sanitization
- Add embed markdown fence: user pastes raw embed HTML code
(from Canva/YouTube/Google Docs Share→Embed) into lesson markdown
and slide content. Backend sanitizes via bleach (whitelist
tags/attrs/styles) + checks iframe src against domain blacklist
(SSRF prevention). Frontend renders iframe directly — no lazy
action needed.
- Backend: _process_embed_embeds + _sanitize_embed_html in
lesson_service.py, applied to lesson_content, exercise,
lesson_info, and slide loop. Graceful fallback if tinycss2
missing (CSS unsanitized but tags/attrs still stripped).
- Tests: 9 pytest cases (Canva/YouTube HTML, script stripping,
onclick stripping, blocked domain, non-https iframe, empty,
unchanged, dangerous style).
- Frontend: remove renderEmbedEmbeds.ts + wire-up + .generic-embed
CSS (URL-only approach from earlier iteration, superseded).
Keep .embed-error CSS for error messages.
- Example: update test_slides.md with raw HTML Canva embed (slide)
+ YouTube embed (body).
- Deps: bleach>=6.0.0, tinycss2>=1.2.0 in requirements.txt.
- Docs: consolidate 4 plan files into docs/06-embed-content.md.
2026-07-19 15:39:38 +07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_canva_html():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<div style="position: relative; width: 100%; padding-top: 56.25%;">
|
|
|
|
|
<iframe loading="lazy" src="https://www.canva.com/design/ABC/view?embed" allowfullscreen></iframe>
|
|
|
|
|
</div>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'canva.com' in out
|
|
|
|
|
assert '<iframe' in out
|
|
|
|
|
assert 'allowfullscreen' in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_strips_script():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<div><iframe src="https://youtube.com/embed/x"></iframe><script>alert(1)</script></div>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
# Script tags are stripped by bleach; inner text remains but is harmless
|
|
|
|
|
assert '<script' not in out.lower()
|
|
|
|
|
assert '</script>' not in out.lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_strips_onclick():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<div onclick="alert(1)"><iframe src="https://youtube.com/embed/x"></iframe></div>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'onclick' not in out
|
|
|
|
|
assert 'alert' not in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_blocked_domain():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<iframe src="https://169.254.169.254/meta"></iframe>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'embed-error' in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_non_https_iframe():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<iframe src="http://youtube.com/embed/x"></iframe>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'embed-error' in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_empty_rejected():
|
|
|
|
|
md = """```embed
|
|
|
|
|
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'embed-error' in out
|
|
|
|
|
assert 'kosong' in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_no_embed_unchanged():
|
|
|
|
|
md = "# Heading\n\nparagraf biasa"
|
|
|
|
|
assert _process_embed_embeds(md) == md
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_youtube_html():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<iframe width="560" height="315" src="https://www.youtube.com/embed/dQw4w9WgXcQ" allowfullscreen></iframe>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'youtube.com' in out
|
|
|
|
|
assert '<iframe' in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_embed_strips_dangerous_style():
|
|
|
|
|
md = """```embed
|
|
|
|
|
<div style="background: url('javascript:alert(1)')"><iframe src="https://youtube.com/embed/x"></iframe></div>
|
|
|
|
|
```"""
|
|
|
|
|
out = _process_embed_embeds(md)
|
|
|
|
|
assert 'javascript' not in out.lower()
|