Skip to content

feat: taint checking and security#197

Open
ambrishrawat wants to merge 52 commits intogenerative-computing:mainfrom
ambrishrawat:security_poc
Open

feat: taint checking and security#197
ambrishrawat wants to merge 52 commits intogenerative-computing:mainfrom
ambrishrawat:security_poc

Conversation

@ambrishrawat
Copy link
Copy Markdown

This PR introduces a minimal proof-of-concept for taint and security propagation across CBlock, ModelOutputThunk, and session flows, as discussed in generative-computing/mellea#189
.

@ambrishrawat ambrishrawat marked this pull request as draft October 14, 2025 19:21
@mergify
Copy link
Copy Markdown

mergify bot commented Oct 14, 2025

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@nrfulton nrfulton self-requested a review October 15, 2025 16:54
@ambrishrawat
Copy link
Copy Markdown
Author

@nrfulton quick clarifications -

  1. What’s the best way for expose taint configuration to devs? e.g. when a description includes a user variable like summarise the following {{email_body}}, should taint be inferred automatically or something they can configure?
  2. Would it make sense to have a global strictness setting to toggle between warnings and exceptions for taint violations? Is blocify the best place for this?

@nrfulton
Copy link
Copy Markdown
Member

What’s the best way for expose taint configuration to devs? e.g. when a description includes a user variable like summarise the following {{email_body}}, should taint be inferred automatically or something they can configure?

We should infer automatically where-ever possible. I nthis case, I'm not sure how you would infer taint. I guess you assumption here is that email_boy -- or any user_variable input -- should entail taint?

@ambrishrawat
Copy link
Copy Markdown
Author

Yes, that was the thinking. Making it configurable may make more sense for taint. Any thoughts on the best way to expose that? Would love your take on the code too.

@davidcox
Copy link
Copy Markdown

If there is a tainted variable in the context, everything downstream should get tainted. As for how variables get tainted in the first place, a common way people do this is to define sources, sinks, and (optionally) washers. These are wrappers around interfaces that produce sensitive data (e.g. HR database api), or where it enters an unsafe place (e.g. sending to a UI).

@ambrishrawat ambrishrawat marked this pull request as ready for review November 12, 2025 11:11
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Comment on lines +79 to +80
component = CBlock("user input")
component.mark_tainted() # Sets SecLevel.tainted_by(component)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

  1. CBlocks should be immutable.
  2. Naming a variable Ccmponent and assigning it to a CBlock is confusing.
  3. The cyclic reference here is a bit confusing and invites buggy code. Use tainted_by(None) instead of tainted_by(self) for the root node.
c = CBlock("user input", sec_level=SecLevel.tained_by(None))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated this; tainted_by(None) for root now

component = CBlock("user input")
component.mark_tainted() # Sets SecLevel.tainted_by(component)

if component._meta["_security"].is_tainted():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not c.sec_level?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Defined it as a property and now this works as c.sec_level.is_tainted()

print(f"Original CBlock is tainted: {not tainted_desc.is_safe()}")

# Create session
session = MelleaSession(OllamaModelBackend("llama3.2"))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unless the example critically depends on using a particular model, always use session = start_session() instead. This makes the examples easier to maintain.


# The result should be tainted
print(f"Result is tainted: {not result.is_safe()}")
if not result.is_safe():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should use is_tainted instead of is_safe. The meaning of safe is very ambiguous.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed all instances of is_safe

Returns:
The CBlock or Component that tainted this content, or None
"""
if self.level_type == "tainted_by":
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Especially in a module called security.core, we should avoid use of magic strings.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Created SecLevelType enum

sources.append(action)

# For Components, check their constituent parts for taint
if hasattr(action, 'parts'):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead us something like:

match action:
     case Component...

    case CBlock...

(If type(action) :> Component then check is not necessary because the Component protocol has a parts() method. )

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated it to use match/case

Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@ambrishrawat
Copy link
Copy Markdown
Author

Thanks for the review @nrfulton !
I have incorporated your suggestions. Appreciate another pass when you get the chance

Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@guicho271828
Copy link
Copy Markdown
Contributor

hi ambrish!

@nrfulton nrfulton self-requested a review December 2, 2025 02:15
@mergify
Copy link
Copy Markdown

mergify bot commented Jan 28, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@mergify
Copy link
Copy Markdown

mergify bot commented Feb 1, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@mergify
Copy link
Copy Markdown

mergify bot commented Feb 8, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@mergify
Copy link
Copy Markdown

mergify bot commented Feb 12, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

def wrapper(*args, **kwargs):
# Check all arguments for marked content (tainted or classified)
for arg in args:
if isinstance(arg, TaintChecking):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wondering if there's an issue with here in not looking recursively (ie via taint_sources() )? the args may be ok, but we could have a tainted Component?

Copy link
Copy Markdown
Author

@ambrishrawat ambrishrawat Feb 25, 2026

Choose a reason for hiding this comment

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

Ah I see that, yes need to call taint_sources here and do a recursive check for classified sources as well. I will update this.

@mergify
Copy link
Copy Markdown

mergify bot commented Feb 25, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:



def declassify(cblock: "CBlock") -> "CBlock":
"""Create a declassified version of a CBlock (non-mutating).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only CBlocks or any subclass? If the latter, we return a CBlock which would lose the subclass info?
For example could it be an ImageBlock?
Also note that ImageBlock never calls super().init() which means the value that is being copied does not exist == crash?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Right, this particular declassify is merely illustrative, hence only applicable to CBlocks. One would need bespoke declassification methods for applications to sparsify taint information across the code. That was my initial thinking

@ambrishrawat ambrishrawat requested review from a team and jakelorocco as code owners February 25, 2026 18:12
@mergify
Copy link
Copy Markdown

mergify bot commented Feb 27, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@mergify
Copy link
Copy Markdown

mergify bot commented Mar 11, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@ambrishrawat
Copy link
Copy Markdown
Author

@planetf1 Updated the logic for privileged. It recursively checks for both taint and classified.
cc: @nrfulton @jakelorocco

ambrishrawat and others added 4 commits March 11, 2026 13:59
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@mergify
Copy link
Copy Markdown

mergify bot commented Mar 25, 2026

Merge Protections

Your pull request matches the following merge protections and will not be merged until they are valid.

🟢 Enforce conventional commit

Wonderful, this rule succeeded.

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert|release)(?:\(.+\))?:

@ambrishrawat ambrishrawat requested a review from planetf1 March 25, 2026 11:40
Copy link
Copy Markdown
Contributor

@planetf1 planetf1 left a comment

Choose a reason for hiding this comment

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

I think there are also some

  • uses of Any where the types should be stricter
  • incorrect types
  • missing type annotations

I know we have omissions elsewhere, but it feels that the focus on security here makes it worth paying close attention.

Need to have a fiddle with mypy/py (the default rules are not strict enough) to be sure

sec_level: Any = None,
):
"""Initialize ModelOutputThunk with an optional pre-computed value and metadata."""
super().__init__(value, meta)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
super().__init__(value, meta)

I think this is left over from conflict resolution as it's a double init() ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added sec_level here

List of constituent components. Empty by default; subclasses override
to expose their internal structure.
"""
return []
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We'll now return none -- inconsistent with annotations, and likely to cause a crash?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Corrected

@@ -838,6 +844,11 @@ async def _generate_from_context_with_kv_cache(

output = ModelOutputThunk(None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

left over from conflict since we're assigning a few lines later. And what about assigning _start (not checked if we do that much further down). We'd lose it?

@@ -983,6 +994,11 @@ async def _generate_from_context_standard(

output = ModelOutputThunk(None)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this another dup ? similar -- worth checking for this pattern through the code?

meta: dict[str, Any] | None = None,
parsed_repr: S | None = None,
tool_calls: dict[str, ModelToolCall] | None = None,
sec_level: Any = None,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As per Nathan's comment above there's another example. I think we should be explicit - especially given it's security. May be worth doing a more thorough check on type annotations and use of Any?

nested = _collect_sources_by_predicate(item, None, predicate)
sources.extend(nested)
except Exception:
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what else could happen here? Do we need more checks for other exceptions? Or log something at least? I guess we're trying to be non-intrusive (need to understand code better to suggest exact changes!)


@runtime_checkable
class Component(Protocol, Generic[S]):
class Component(TaintChecking, Protocol, Generic[S]):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

so any contribs/older code outside mellea would be impacted -- should we ensure we capture in release notes/doc as a breaking change

lambda self: getattr(self, "_sec_level", None),
doc="Get the security level for this Component.",
)
setattr(obj, "sec_level", sec_level_prop)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this works on a class but fails on an instance - other parts of the code have checks for class vs instance to do the right thing, but this is omitted here

Signed-off-by: Ambrish Rawat <ambrish.rawat@ie.ibm.com>
@github-actions github-actions bot added the enhancement New feature or request label Mar 25, 2026
@jakelorocco
Copy link
Copy Markdown
Contributor

I've started taking a look at this. Will finish my review soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants