| @@ -10,11 +10,13 @@ |
| 10 | 10 | |
| 11 | 11 | use std::cmp::Ordering as Order; |
| 12 | 12 | |
| 13 | | −use louise_rt_prims::{chars, hash_bytes, hash_combine, html_escape, mix, split, url_decode}; |
| 13 | +use louise_rt_prims::{ |
| 14 | + chars, hash_bytes, hash_combine, html_escaped, html_escaped_len, mix, split, |
| 15 | +}; |
| 14 | 16 | |
| 15 | 17 | use crate::abi::{Block, Ctx, TAG_FLOAT, UNIT, Value, int}; |
| 16 | 18 | use crate::console; |
| 17 | | −use crate::heap::tl_alloc; |
| 19 | +use crate::heap::{drop_value, tl_alloc}; |
| 18 | 20 | use crate::int::saturating; |
| 19 | 21 | use crate::values::{bytes_from, cell, error, list, ok, string, string_bytes, text}; |
| 20 | 22 | |
| @@ -450,12 +452,27 @@ pub unsafe extern "C" fn tl_string_join(ctx: *mut Ctx, parts: Value, sep: Value) |
| 450 | 452 | s |
| 451 | 453 | } |
| 452 | 454 | |
| 455 | +/// `a`, escaped for HTML (`html.escape`). Consumes `a` (an `owned` |
| 456 | +/// extern): a string without metacharacters — the common case — is |
| 457 | +/// returned as is; otherwise the escaped form is written straight into |
| 458 | +/// one fresh string, without an intermediate `String`. |
| 459 | +/// |
| 453 | 460 | /// # Safety |
| 454 | | −/// `a` must be a live string. |
| 461 | +/// `a` must be a live string (owned). |
| 455 | 462 | #[unsafe(no_mangle)] |
| 456 | 463 | pub unsafe extern "C" fn tl_html_escape(ctx: *mut Ctx, a: Value) -> Value { |
| 457 | 464 | // SAFETY: caller guarantees a live string. |
| 458 | | − string(ctx, &html_escape(unsafe { text(a) })) |
| 465 | + let s = unsafe { text(a) }; |
| 466 | + let Some(len) = html_escaped_len(s) else { |
| 467 | + return a; |
| 468 | + }; |
| 469 | + let (out, at) = crate::values::string_uninit(ctx, len); |
| 470 | + // SAFETY: `at` has room for the escaped length just computed, over the |
| 471 | + // same chunks. |
| 472 | + unsafe { crate::values::write_joined(at, html_escaped(s), &[]) }; |
| 473 | + // SAFETY: `a` is owned and not used again. |
| 474 | + unsafe { drop_value(ctx, a) }; |
| 475 | + out |
| 459 | 476 | } |
| 460 | 477 | |
| 461 | 478 | /// Print to stdout, through the console (ADR 0022, as `tl_println`). |
| @@ -521,12 +538,44 @@ pub unsafe extern "C" fn tl_os_exit(_ctx: *mut Ctx, code: Value) -> Value { |
| 521 | 538 | std::process::exit(unsafe { crate::int::saturating(code) } as i32) |
| 522 | 539 | } |
| 523 | 540 | |
| 541 | +/// `s`, URL-decoded (`http.url_decode`). Consumes `s` (an `owned` |
| 542 | +/// extern): a string without `%` or `+` is returned as is; otherwise the |
| 543 | +/// decoded form is written straight into one fresh string (it is never |
| 544 | +/// longer than `s`), without the intermediate buffers of the kernel. |
| 545 | +/// |
| 524 | 546 | /// # Safety |
| 525 | | −/// `s` must be a live string. |
| 547 | +/// `s` must be a live string (owned). |
| 526 | 548 | #[unsafe(no_mangle)] |
| 527 | 549 | pub unsafe extern "C" fn tl_url_decode(ctx: *mut Ctx, s: Value) -> Value { |
| 528 | 550 | // SAFETY: caller guarantees a live string. |
| 529 | | − string(ctx, &url_decode(unsafe { text(s) })) |
| 551 | + let text = unsafe { text(s) }; |
| 552 | + if !louise_rt_prims::url_needs_decode(text) { |
| 553 | + return s; |
| 554 | + } |
| 555 | + let (out, at) = crate::values::string_uninit(ctx, text.len()); |
| 556 | + // SAFETY: `at` has room for `text.len()` bytes. |
| 557 | + let n = unsafe { |
| 558 | + louise_rt_prims::url_decode_into(text, std::slice::from_raw_parts_mut(at, text.len())) |
| 559 | + }; |
| 560 | + // SAFETY: the first `n` bytes at `at` were just written. |
| 561 | + let decoded = unsafe { std::slice::from_raw_parts(at, n) }; |
| 562 | + if std::str::from_utf8(decoded).is_ok() { |
| 563 | + // SAFETY: `out` is a fresh unique string allocated for |
| 564 | + // `text.len()` bytes, its first `n` written. |
| 565 | + let out = unsafe { crate::values::string_shrink(out, text.len(), n) }; |
| 566 | + // SAFETY: `s` is owned and not used again. |
| 567 | + unsafe { drop_value(ctx, s) }; |
| 568 | + return out; |
| 569 | + } |
| 570 | + // Decoded bytes that are not valid UTF-8 (rare): the lossy form can |
| 571 | + // be longer than they are, so it is built in a second string. |
| 572 | + let fixed = string(ctx, &String::from_utf8_lossy(decoded)); |
| 573 | + // SAFETY: both are owned and not used again. |
| 574 | + unsafe { |
| 575 | + drop_value(ctx, out); |
| 576 | + drop_value(ctx, s); |
| 577 | + } |
| 578 | + fixed |
| 530 | 579 | } |
| 531 | 580 | |
| 532 | 581 | #[cfg(test)] |
| @@ -603,6 +652,79 @@ mod tests { |
| 603 | 652 | UNIT |
| 604 | 653 | } |
| 605 | 654 | |
| 655 | + /// HTML escaping and URL decoding: a clean input is returned as is |
| 656 | + /// (the same block, no allocation); a dirty one is built in place, |
| 657 | + /// with its NUL, and the input released (the leak oracle checks). |
| 658 | + unsafe extern "C" fn escapes_and_decodes(ctx: *mut Ctx) -> Value { |
| 659 | + // SAFETY: every value is owned by this test. |
| 660 | + unsafe { |
| 661 | + for (input, want) in [ |
| 662 | + // Clean: the five metacharacters absent (unicode incl.). |
| 663 | + ("", ""), |
| 664 | + ("plain", "plain"), |
| 665 | + ("héllo 🦀", "héllo 🦀"), |
| 666 | + // Each metacharacter, alone and combined. |
| 667 | + ("&", "&"), |
| 668 | + ("<>&\"'", "<>&"'"), |
| 669 | + ( |
| 670 | + r#"<a href="x">Tom & 'Jerry'</a>"#, |
| 671 | + "<a href="x">Tom & 'Jerry'</a>", |
| 672 | + ), |
| 673 | + // Escaped text is escaped again. |
| 674 | + ("&", "&amp;"), |
| 675 | + // Multibyte characters around a metacharacter. |
| 676 | + ("日本<語", "日本<語"), |
| 677 | + ] { |
| 678 | + let s = string(ctx, input); |
| 679 | + let out = tl_html_escape(ctx, s); |
| 680 | + assert_eq!(text(out), want, "escape {input:?}"); |
| 681 | + assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 682 | + let clean = html_escaped_len(input).is_none(); |
| 683 | + assert_eq!(out == s, clean, "clean returned as is: {input:?}"); |
| 684 | + drop_value(ctx, out); |
| 685 | + } |
| 686 | + for (input, want) in [ |
| 687 | + // Clean: no `%`, no `+` (a space is not a `+`). |
| 688 | + ("", ""), |
| 689 | + ("plain text", "plain text"), |
| 690 | + ("é", "é"), |
| 691 | + // `+` and escapes, at the edges too. |
| 692 | + ("a+b%20c", "a b c"), |
| 693 | + ("+a+", " a "), |
| 694 | + ("%41%42%43", "ABC"), |
| 695 | + ("%00", "\0"), |
| 696 | + ("%C3%A9", "é"), |
| 697 | + ("%c3%a9", "é"), |
| 698 | + // Invalid escapes stay; invalid UTF-8 becomes U+FFFD. |
| 699 | + ("%", "%"), |
| 700 | + ("%2", "%2"), |
| 701 | + ("%2G", "%2G"), |
| 702 | + ("%zz", "%zz"), |
| 703 | + ("%%41", "%A"), |
| 704 | + ("%ff", "\u{fffd}"), |
| 705 | + ("%C3", "\u{fffd}"), |
| 706 | + ("%A9%C3", "\u{fffd}\u{fffd}"), |
| 707 | + ] { |
| 708 | + let s = string(ctx, input); |
| 709 | + let out = tl_url_decode(ctx, s); |
| 710 | + assert_eq!(text(out), want, "decode {input:?}"); |
| 711 | + assert_eq!(*string_bytes(out).as_ptr().add(want.len()), 0, "NUL"); |
| 712 | + let clean = !louise_rt_prims::url_needs_decode(input); |
| 713 | + assert_eq!(out == s, clean, "clean returned as is: {input:?}"); |
| 714 | + drop_value(ctx, out); |
| 715 | + } |
| 716 | + } |
| 717 | + assert_eq!(crate::heap::stats::tl_stats_live_blocks(), 0); |
| 718 | + UNIT |
| 719 | + } |
| 720 | + |
| 721 | + #[test] |
| 722 | + fn escaping_and_decoding_are_in_place() { |
| 723 | + // SAFETY: the test follows the generated-code convention. |
| 724 | + let code = unsafe { crate::sched::run_test_actor(escapes_and_decodes) }; |
| 725 | + assert_eq!(code, 0); |
| 726 | + } |
| 727 | + |
| 606 | 728 | #[test] |
| 607 | 729 | fn strings_are_built_in_place() { |
| 608 | 730 | // SAFETY: the test follows the generated-code convention. |