Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/core/jsonschema/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ auto elevate_embedded_resources(
SchemaFrame::IdentifierMode::Additional,
{EMPTY_WEAK_POINTER},
"",
{},
remaining};
charge(remaining, entry_frame);
const auto &identifier{entry_frame.root()};
Expand Down Expand Up @@ -235,6 +236,7 @@ auto elevate_embedded_resources(
SchemaFrame::IdentifierMode::Additional,
{EMPTY_WEAK_POINTER},
"",
{},
remaining};
charge(remaining, stored_frame);
const auto &stored_id{stored_frame.root()};
Expand Down Expand Up @@ -307,14 +309,21 @@ auto embed_references(
// between different schemas that have references at the same pointer paths
static const SchemaFrame::Paths NESTED_PATHS{EMPTY_WEAK_POINTER};
const SchemaFrame frame{
SchemaFrame::Mode::References, subschema, walker, resolver,
default_dialect, default_id, SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Mode::References,
subschema,
walker,
resolver,
default_dialect,
default_id,
SchemaFrame::IdentifierMode::Additional,
// We only want to frame in "wrapper" mode for the top
// level object, which is also the only one that the
// base the caller retrieved it from applies to, as
// every remote carries the identity it was resolved by
depth == 0 ? paths : NESTED_PATHS,
depth == 0 ? default_base : std::string_view{}, remaining};
depth == 0 ? default_base : std::string_view{},
{},
remaining};
charge(remaining, frame);

std::vector<std::tuple<JSON, JSON::String, SchemaBaseDialect>> deferred;
Expand Down Expand Up @@ -381,10 +390,11 @@ auto embed_references(

std::optional<SchemaFrame> remote_root_frame;
try {
remote_root_frame.emplace(
SchemaFrame::Mode::Root, remote, walker, resolver, default_dialect,
"", SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Paths{EMPTY_WEAK_POINTER}, "", remaining);
remote_root_frame.emplace(SchemaFrame::Mode::Root, remote, walker,
resolver, default_dialect, "",
SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Paths{EMPTY_WEAK_POINTER}, "",
SchemaFrame::Bases{}, remaining);
charge(remaining, remote_root_frame.value());
} catch (const SchemaUnknownBaseDialectError &) {
throw SchemaReferenceError(identifier, to_pointer(pointer),
Expand Down Expand Up @@ -420,6 +430,7 @@ auto embed_references(
SchemaFrame::IdentifierMode::Additional,
{EMPTY_WEAK_POINTER},
"",
{},
remaining};
charge(remaining, remote_frame);
exists = remote_frame.traverse(reference.destination).has_value();
Expand Down Expand Up @@ -514,6 +525,7 @@ auto bundle_internal(JSON &schema, const SchemaWalker &walker,
SchemaFrame::IdentifierMode::Additional,
paths,
default_base,
{},
remaining};
charge(remaining, initial_frame);
initial_frame.for_each_resource_uri([&bundled](const auto &uri) -> void {
Expand Down Expand Up @@ -570,6 +582,7 @@ auto bundle_internal(JSON &schema, const SchemaWalker &walker,
SchemaFrame::IdentifierMode::Additional,
{EMPTY_WEAK_POINTER},
"",
{},
remaining};
charge(remaining, declared_frame);
if (declared_frame.root().empty()) {
Expand All @@ -579,10 +592,11 @@ auto bundle_internal(JSON &schema, const SchemaWalker &walker,

std::optional<SchemaFrame> schema_root_frame;
try {
schema_root_frame.emplace(
SchemaFrame::Mode::Root, schema, walker, resolver, default_dialect,
default_id, SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Paths{EMPTY_WEAK_POINTER}, "", remaining);
schema_root_frame.emplace(SchemaFrame::Mode::Root, schema, walker, resolver,
default_dialect, default_id,
SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Paths{EMPTY_WEAK_POINTER}, "",
SchemaFrame::Bases{}, remaining);
charge(remaining, schema_root_frame.value());
} catch (const SchemaUnknownBaseDialectError &) {
throw SchemaError("Could not determine how to perform bundling in this "
Expand All @@ -603,6 +617,7 @@ auto bundle_internal(JSON &schema, const SchemaWalker &walker,
SchemaFrame::IdentifierMode::Additional,
{EMPTY_WEAK_POINTER},
default_base,
{},
remaining};
charge(remaining, frame);
if (frame.standalone()) {
Expand Down
58 changes: 43 additions & 15 deletions src/core/jsonschema/frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ auto canonicalize_identifier(const std::string_view identifier,
.recompose();
}

// RFC 3986 Section 5.1 defines a base URI as one that carries no fragment, and
// Section 5.2.2 never consults one, so a base that states a fragment states
// something that could not mean anything
//
// the base URI ... a fragment component is not part of it
auto canonicalize_base(const std::string_view base)
-> sourcemeta::core::JSON::String {
const sourcemeta::core::URI uri{base};
const auto fragment{uri.fragment()};
if (fragment.has_value() && !fragment.value().empty()) {
throw sourcemeta::core::SchemaFrameError(
base, "The base must not contain a non-empty fragment");
}

return sourcemeta::core::URI::canonicalize(base);
}

[[noreturn]]
auto throw_already_exists(const sourcemeta::core::JSON::String &uri) -> void {
throw sourcemeta::core::SchemaFrameError(uri,
Expand Down Expand Up @@ -851,6 +868,7 @@ SchemaFrame::SchemaFrame(const Mode mode, const sourcemeta::core::JSON &root,
const SchemaFrame::IdentifierMode identifier_mode,
const SchemaFrame::Paths &paths,
const std::string_view default_base,
const SchemaFrame::Bases &additional_bases,
const std::uint64_t max_locations)
: mode_{mode}, cache_{std::make_unique<Cache>()} {
// This mode reports on a single schema. Framing a wrapper that holds more
Expand Down Expand Up @@ -897,20 +915,15 @@ SchemaFrame::SchemaFrame(const Mode mode, const sourcemeta::core::JSON &root,
base_dialects;

if (!default_base.empty()) {
// RFC 3986 Section 5.1 defines a base URI as one that carries no
// fragment, and Section 5.2.2 never consults one, so a base that states a
// fragment states something that could not mean anything
//
// the base URI ... a fragment component is not part of it
const sourcemeta::core::URI base{default_base};
const auto fragment{base.fragment()};
if (fragment.has_value() && !fragment.value().empty()) {
throw SchemaFrameError(default_base,
"The base must not contain a non-empty fragment");
}
this->cache_->default_base = canonicalize_base(default_base);
}

this->cache_->default_base =
sourcemeta::core::URI::canonicalize(default_base);
std::vector<sourcemeta::core::JSON::String> additional_base_uris;
additional_base_uris.reserve(additional_bases.size());
for (const auto &additional_base : additional_bases) {
if (!additional_base.empty()) {
additional_base_uris.push_back(canonicalize_base(additional_base));
}
}

for (const auto &path : paths) {
Expand Down Expand Up @@ -1020,7 +1033,11 @@ SchemaFrame::SchemaFrame(const Mode mode, const sourcemeta::core::JSON &root,
// instead make that path a resource, addressing what sits under it from
// there rather than from the top of the document, which is what an
// identifier does and what this deliberately does not
if (!this->cache_->default_base.empty()) {
//
// Whatever else the caller states the document goes by comes after all of
// those, as a name that the document neither carries nor was retrieved
// from is the last thing a reference within it could be read against
if (!this->cache_->default_base.empty() || !additional_base_uris.empty()) {
std::vector<sourcemeta::core::JSON::String> root_bases;
if (root_id.has_value() && path.empty()) {
root_bases.push_back(root_id.value());
Expand All @@ -1029,7 +1046,18 @@ SchemaFrame::SchemaFrame(const Mode mode, const sourcemeta::core::JSON &root,
}
}

root_bases.push_back(this->cache_->default_base);
if (!this->cache_->default_base.empty()) {
root_bases.push_back(this->cache_->default_base);
}

for (const auto &additional_base : additional_base_uris) {
// A base that is already in force addresses every place of the
// document once, so stating it again would address each of them twice
if (!std::ranges::contains(root_bases, additional_base)) {
root_bases.push_back(additional_base);

@augmentcode augmentcode Bot Oct 3, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check distinct bases that resolve a relative $id to the same URI at src/core/jsonschema/frame.cc:1057, e.g. default_base=https://example.com/a and additional_bases={https://example.com/b} with $id: "pet"? Both resolve to https://example.com/pet, but the resource loop at lines 1266–1276 attempts to store it twice and throws SchemaFrameError for a valid schema.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

}
}

base_uris.insert_or_assign(sourcemeta::core::EMPTY_WEAK_POINTER,
std::move(root_bases));
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/jsonschema/include/sourcemeta/core/jsonschema_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
/// A list of paths to frame within a schema wrapper
using Paths = std::vector<sourcemeta::core::WeakPointer>;

/// A list of bases that the top of a document goes by
using Bases = std::vector<std::string_view>;

/// Export the frame as JSON
[[nodiscard]] auto to_json(
const SchemaResolver &resolver,
Expand All @@ -178,8 +181,8 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
/// it, so the schema must outlive the frame. The same goes for
/// `default_dialect`, as a location that has no dialect of its own reports
/// the default back as a view into what the caller passed. In contrast,
/// `default_id` and `default_base` are copied, so they do not need to
/// outlive this call
/// `default_id`, `default_base` and `additional_bases` are copied, so they
/// do not need to outlive this call
///
/// Pass `default_base` to state the base URI that the document was
/// retrieved from, which anything that declares no identifier of its own
Expand All @@ -189,6 +192,13 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
/// the document rather than from wherever a path begins, and a document
/// that declares no identifier still reports none
///
/// Pass `additional_bases` to state every other base that the top of the
/// document goes by, which is what a document retrieved from more than one
/// place or named by its holder comes to. Each one addresses every place of
/// the document in turn, after whatever the document declares and after
/// `default_base`, and claims an identifier no more than `default_base`
/// does. A base already in force is not registered twice
///
/// Framing a schema that declares nested identifiers registers a location
/// per enclosing base, so what an untrusted schema costs to analyse grows
/// faster than the schema itself does. Pass `max_locations` to bound that,
Expand All @@ -205,7 +215,7 @@ class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
std::string_view default_dialect = "", std::string_view default_id = "",
IdentifierMode identifier_mode = IdentifierMode::Additional,
const Paths &paths = {sourcemeta::core::EMPTY_WEAK_POINTER},
std::string_view default_base = "",
std::string_view default_base = "", const Bases &additional_bases = {},
std::uint64_t max_locations = std::numeric_limits<std::uint64_t>::max());

/// Get a specific reference entry by type and pointer
Expand Down
4 changes: 4 additions & 0 deletions src/core/jsonschema/known_resolver.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ file(READ "${OPENAPI_3_2_DIR}/dialect/2025-09-17" OPENAPI_OAS_3_2_DIALECT_2025_0
set(OPENAPI_3_1_DIR "${PROJECT_SOURCE_DIR}/vendor/openapi/oas/3.1")
file(READ "${OPENAPI_3_1_DIR}/meta/base" OPENAPI_OAS_3_1_META_BASE)
file(READ "${OPENAPI_3_1_DIR}/dialect/base" OPENAPI_OAS_3_1_DIALECT_BASE)
file(READ "${OPENAPI_3_1_DIR}/meta/2024-11-10" OPENAPI_OAS_3_1_META_2024_11_10)
file(READ "${OPENAPI_3_1_DIR}/dialect/2024-11-10" OPENAPI_OAS_3_1_DIALECT_2024_11_10)
file(READ "${OPENAPI_3_1_DIR}/meta/2024-10-25" OPENAPI_OAS_3_1_META_2024_10_25)
file(READ "${OPENAPI_3_1_DIR}/dialect/2024-10-25" OPENAPI_OAS_3_1_DIALECT_2024_10_25)

if(NOT KNOWN_RESOLVER_INPUT)
message(FATAL_ERROR "Missing KNOWN_RESOLVER_INPUT")
Expand Down
50 changes: 50 additions & 0 deletions src/core/jsonschema/known_resolver.in.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ enum class KnownSchema : std::uint8_t {

OAS_3_1_DIALECT_BASE,
OAS_3_1_META_BASE,
OAS_3_1_DIALECT_2024_11_10,
OAS_3_1_META_2024_11_10,
OAS_3_1_DIALECT_2024_10_25,
OAS_3_1_META_2024_10_25,

UNKNOWN
};
Expand Down Expand Up @@ -419,6 +423,28 @@ static auto parse_identifier(const std::string_view identifier) -> KnownSchema {
return KnownSchema::OAS_3_1_META_BASE;
}

// OpenAPI Specification 3.1.1, Section 4.8.24.1 names only the undated URI
// above as the dialect of the revision, and Section 4.8.24.5 leaves room for
// the dated ones published beside it: "Tooling MUST support the OAS dialect
// schema id, and MAY support additional values of `$schema`". Each of them
// declares the same vocabularies as the undated one, so a Schema Object
// written against one is read exactly as it would be against the other
if (identifier == "https://spec.openapis.org/oas/3.1/dialect/2024-11-10") {
return KnownSchema::OAS_3_1_DIALECT_2024_11_10;
}

if (identifier == "https://spec.openapis.org/oas/3.1/meta/2024-11-10") {
return KnownSchema::OAS_3_1_META_2024_11_10;
}

if (identifier == "https://spec.openapis.org/oas/3.1/dialect/2024-10-25") {
return KnownSchema::OAS_3_1_DIALECT_2024_10_25;
}

if (identifier == "https://spec.openapis.org/oas/3.1/meta/2024-10-25") {
return KnownSchema::OAS_3_1_META_2024_10_25;
}

return KnownSchema::UNKNOWN;
}

Expand Down Expand Up @@ -700,6 +726,26 @@ auto sourcemeta::core::schema_resolver(const std::string_view identifier)
sourcemeta::core::parse_json(R"EOF(@OPENAPI_OAS_3_1_META_BASE@)EOF")};
return SCHEMA;
}
case KnownSchema::OAS_3_1_DIALECT_2024_11_10: {
static const auto SCHEMA{sourcemeta::core::parse_json(
R"EOF(@OPENAPI_OAS_3_1_DIALECT_2024_11_10@)EOF")};
return SCHEMA;
}
case KnownSchema::OAS_3_1_META_2024_11_10: {
static const auto SCHEMA{sourcemeta::core::parse_json(
R"EOF(@OPENAPI_OAS_3_1_META_2024_11_10@)EOF")};
return SCHEMA;
}
case KnownSchema::OAS_3_1_DIALECT_2024_10_25: {
static const auto SCHEMA{sourcemeta::core::parse_json(
R"EOF(@OPENAPI_OAS_3_1_DIALECT_2024_10_25@)EOF")};
return SCHEMA;
}
case KnownSchema::OAS_3_1_META_2024_10_25: {
static const auto SCHEMA{sourcemeta::core::parse_json(
R"EOF(@OPENAPI_OAS_3_1_META_2024_10_25@)EOF")};
return SCHEMA;
}
case KnownSchema::UNKNOWN:
return std::nullopt;
}
Expand Down Expand Up @@ -771,6 +817,10 @@ auto sourcemeta::core::schema_is_official(
case KnownSchema::OAS_3_2_META_2025_09_17:
case KnownSchema::OAS_3_1_DIALECT_BASE:
case KnownSchema::OAS_3_1_META_BASE:
case KnownSchema::OAS_3_1_DIALECT_2024_11_10:
case KnownSchema::OAS_3_1_META_2024_11_10:
case KnownSchema::OAS_3_1_DIALECT_2024_10_25:
case KnownSchema::OAS_3_1_META_2024_10_25:
case KnownSchema::UNKNOWN:
return false;
}
Expand Down
9 changes: 6 additions & 3 deletions src/core/openapi/bundle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ auto absolutize_schemas(sourcemeta::core::JSON &value,
sourcemeta::core::SchemaFrame::IdentifierMode::Additional,
paths,
remote.base,
{},
remaining};
charge(remaining, frame.location_count());

Expand Down Expand Up @@ -772,6 +773,7 @@ auto schema_pending(const sourcemeta::core::JSON &document,
sourcemeta::core::SchemaFrame::IdentifierMode::Additional,
paths,
walk.base,
{},
remaining};
charge(remaining, frame.location_count());

Expand Down Expand Up @@ -966,6 +968,7 @@ auto index_schemas(const sourcemeta::core::JSON &remote_document,
sourcemeta::core::SchemaFrame::IdentifierMode::Additional,
paths,
remote.base,
{},
remaining};
charge(remaining, frame.location_count());

Expand Down Expand Up @@ -1458,7 +1461,7 @@ auto adopt_mappings(
JSON::String{dialect}, identifier,
SchemaFrame::IdentifierMode::Additional,
SchemaFrame::Paths{EMPTY_WEAK_POINTER}, identifier,
remaining);
SchemaFrame::Bases{}, remaining);
} catch (const SchemaUnknownBaseDialectError &) {
// What a schema is written against is what says how to read it, and
// Section 4.8.24.5 leaves the dialect to whatever the schema names. One
Expand Down Expand Up @@ -1566,7 +1569,7 @@ auto bundle_internal(JSON &document, const SchemaWalker &walker,
bool named{false};

while (true) {
const auto walk{openapi_analyse(document, retrieval, remaining)};
const auto walk{openapi_analyse(document, retrieval, {}, remaining)};
const auto &base{walk.base};
if (!named) {
names.security_schemes = walk.security_schemes;
Expand Down Expand Up @@ -1722,7 +1725,7 @@ auto bundle_internal(JSON &document, const SchemaWalker &walker,
// takes its place before anything walks it
const auto &held{
documents.emplace(identifier, std::move(candidate)).first->second};
auto analysis{openapi_analyse(held, identifier, remaining, &names)};
auto analysis{openapi_analyse(held, identifier, {}, remaining, &names)};
charge(remaining, analysis.locations.size());
const auto &recorded{
walks.emplace(identifier, std::move(analysis)).first->second};
Expand Down
Loading
Loading